blob: 79eab655301ac17e98b0b813bea44442f147bdf4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000019 */
20/*
Paul Bakker5121ce52009-01-03 21:22:43 +000021 * http://www.ietf.org/rfc/rfc2246.txt
22 * http://www.ietf.org/rfc/rfc4346.txt
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
SimonBd5800b72016-04-26 07:43:27 +010029#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdlib.h>
33#define mbedtls_calloc calloc
34#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010035#endif
36
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020038#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000039#include "mbedtls/debug.h"
40#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050041#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010042#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020043
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020044#include "ssl_invasive.h"
45
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050048#if defined(MBEDTLS_USE_PSA_CRYPTO)
49#include "mbedtls/psa_util.h"
50#include "psa/crypto.h"
51#endif
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Beckercd9dcda2018-08-28 17:18:56 +010057static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020059/*
60 * Start a timer.
61 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020062 */
Hanno Becker0f57a652020-02-05 10:37:26 +000063void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020064{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020065 if( ssl->f_set_timer == NULL )
66 return;
67
68 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
69 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020070}
71
72/*
73 * Return -1 is timer is expired, 0 if it isn't.
74 */
Hanno Becker7876d122020-02-05 10:39:31 +000075int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020078 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079
80 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020081 {
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020084 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020085
86 return( 0 );
87}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088
Hanno Beckercfe45792019-07-03 16:13:00 +010089#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010090static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
91 unsigned char *buf,
92 size_t len,
93 mbedtls_record *rec );
94
Hanno Beckercfe45792019-07-03 16:13:00 +010095int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
96 unsigned char *buf,
97 size_t buflen )
98{
Hanno Becker54229812019-07-12 14:40:00 +010099 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
101 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
102
103 /* We don't support record checking in TLS because
104 * (a) there doesn't seem to be a usecase for it, and
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100105 * (b) In TLS 1.0, CBC record decryption has state
Hanno Becker54229812019-07-12 14:40:00 +0100106 * and we'd need to backup the transform here.
107 */
108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
109 {
110 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
111 goto exit;
112 }
113#if defined(MBEDTLS_SSL_PROTO_DTLS)
114 else
115 {
irwir734f0cf2019-09-26 21:03:24 +0300116 mbedtls_record rec;
117
Hanno Becker54229812019-07-12 14:40:00 +0100118 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
119 if( ret != 0 )
120 {
121 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
122 goto exit;
123 }
124
125 if( ssl->transform_in != NULL )
126 {
127 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
128 if( ret != 0 )
129 {
130 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
131 goto exit;
132 }
133 }
134 }
135#endif /* MBEDTLS_SSL_PROTO_DTLS */
136
137exit:
138 /* On success, we have decrypted the buffer in-place, so make
139 * sure we don't leak any plaintext data. */
140 mbedtls_platform_zeroize( buf, buflen );
141
142 /* For the purpose of this API, treat messages with unexpected CID
143 * as well as such from future epochs as unexpected. */
144 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
145 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
146 {
147 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
148 }
149
150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
151 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100152}
153#endif /* MBEDTLS_SSL_RECORD_CHECKING */
154
Hanno Becker67bc7c32018-08-06 11:33:50 +0100155#define SSL_DONT_FORCE_FLUSH 0
156#define SSL_FORCE_FLUSH 1
157
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100159
Hanno Beckerd5847772018-08-28 10:09:23 +0100160/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100161static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
162 uint8_t slot );
163static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
164static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
165static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
166static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100167static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
168 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100169static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100170
Hanno Becker11682cc2018-08-22 14:41:02 +0100171static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100172{
Hanno Becker89490712020-02-05 10:50:12 +0000173 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000174#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
175 size_t out_buf_len = ssl->out_buf_len;
176#else
177 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
178#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100179
Darryl Greenb33cc762019-11-28 14:29:44 +0000180 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100181 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182
Darryl Greenb33cc762019-11-28 14:29:44 +0000183 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184}
185
Hanno Becker67bc7c32018-08-06 11:33:50 +0100186static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
187{
Hanno Becker11682cc2018-08-22 14:41:02 +0100188 size_t const bytes_written = ssl->out_left;
189 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190
191 /* Double-check that the write-index hasn't gone
192 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100193 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194 {
195 /* Should never happen... */
196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
197 }
198
199 return( (int) ( mtu - bytes_written ) );
200}
201
202static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
203{
Janos Follath865b3eb2019-12-16 11:46:15 +0000204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100205 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400206 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100207
208#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400209 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210
211 if( max_len > mfl )
212 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100213
214 /* By the standard (RFC 6066 Sect. 4), the MFL extension
215 * only limits the maximum record payload size, so in theory
216 * we would be allowed to pack multiple records of payload size
217 * MFL into a single datagram. However, this would mean that there's
218 * no way to explicitly communicate MTU restrictions to the peer.
219 *
220 * The following reduction of max_len makes sure that we never
221 * write datagrams larger than MFL + Record Expansion Overhead.
222 */
223 if( max_len <= ssl->out_left )
224 return( 0 );
225
226 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100227#endif
228
229 ret = ssl_get_remaining_space_in_datagram( ssl );
230 if( ret < 0 )
231 return( ret );
232 remaining = (size_t) ret;
233
234 ret = mbedtls_ssl_get_record_expansion( ssl );
235 if( ret < 0 )
236 return( ret );
237 expansion = (size_t) ret;
238
239 if( remaining <= expansion )
240 return( 0 );
241
242 remaining -= expansion;
243 if( remaining >= max_len )
244 remaining = max_len;
245
246 return( (int) remaining );
247}
248
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249/*
250 * Double the retransmit timeout value, within the allowed range,
251 * returning -1 if the maximum value has already been reached.
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254{
255 uint32_t new_timeout;
256
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200257 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258 return( -1 );
259
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200260 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
261 * in the following way: after the initial transmission and a first
262 * retransmission, back off to a temporary estimated MTU of 508 bytes.
263 * This value is guaranteed to be deliverable (if not guaranteed to be
264 * delivered) of any compliant IPv4 (and IPv6) network, and should work
265 * on most non-IP stacks too. */
266 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400267 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200268 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
270 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200271
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200272 new_timeout = 2 * ssl->handshake->retransmit_timeout;
273
274 /* Avoid arithmetic overflow and range overflow */
275 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200276 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200277 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200278 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279 }
280
281 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283 ssl->handshake->retransmit_timeout ) );
284
285 return( 0 );
286}
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200289{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200290 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292 ssl->handshake->retransmit_timeout ) );
293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
297int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200298 const unsigned char *key_enc, const unsigned char *key_dec,
299 size_t keylen,
300 const unsigned char *iv_enc, const unsigned char *iv_dec,
301 size_t ivlen,
302 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200303 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
305int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
306int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
307int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
308int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
309#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000310
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100311/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200313 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000314
Hanno Beckerccc13d02020-05-04 12:30:04 +0100315#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
316 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100317
318static size_t ssl_compute_padding_length( size_t len,
319 size_t granularity )
320{
321 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
322}
323
Hanno Becker581bc1b2020-05-04 12:20:03 +0100324/* This functions transforms a (D)TLS plaintext fragment and a record content
325 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
326 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
327 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100328 *
329 * struct {
330 * opaque content[DTLSPlaintext.length];
331 * ContentType real_type;
332 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100333 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100334 *
335 * Input:
336 * - `content`: The beginning of the buffer holding the
337 * plaintext to be wrapped.
338 * - `*content_size`: The length of the plaintext in Bytes.
339 * - `max_len`: The number of Bytes available starting from
340 * `content`. This must be `>= *content_size`.
341 * - `rec_type`: The desired record content type.
342 *
343 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100344 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
345 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100346 *
347 * Returns:
348 * - `0` on success.
349 * - A negative error code if `max_len` didn't offer enough space
350 * for the expansion.
351 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100352static int ssl_build_inner_plaintext( unsigned char *content,
353 size_t *content_size,
354 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100355 uint8_t rec_type,
356 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100357{
358 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100359
360 /* Write real content type */
361 if( remaining == 0 )
362 return( -1 );
363 content[ len ] = rec_type;
364 len++;
365 remaining--;
366
367 if( remaining < pad )
368 return( -1 );
369 memset( content + len, 0, pad );
370 len += pad;
371 remaining -= pad;
372
373 *content_size = len;
374 return( 0 );
375}
376
Hanno Becker581bc1b2020-05-04 12:20:03 +0100377/* This function parses a (D)TLSInnerPlaintext structure.
378 * See ssl_build_inner_plaintext() for details. */
379static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100380 size_t *content_size,
381 uint8_t *rec_type )
382{
383 size_t remaining = *content_size;
384
385 /* Determine length of padding by skipping zeroes from the back. */
386 do
387 {
388 if( remaining == 0 )
389 return( -1 );
390 remaining--;
391 } while( content[ remaining ] == 0 );
392
393 *content_size = remaining;
394 *rec_type = content[ remaining ];
395
396 return( 0 );
397}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100398#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
399 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100400
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100401/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100402 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000403static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100404 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100405 mbedtls_record *rec,
406 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000407{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100408 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100409 *
410 * additional_data = seq_num + TLSCompressed.type +
411 * TLSCompressed.version + TLSCompressed.length;
412 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100413 * For the CID extension, this is extended as follows
414 * (quoting draft-ietf-tls-dtls-connection-id-05,
415 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100416 *
417 * additional_data = seq_num + DTLSPlaintext.type +
418 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100419 * cid +
420 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100421 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100422 *
423 * For TLS 1.3, the record sequence number is dropped from the AAD
424 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100425 */
426
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100427 unsigned char *cur = add_data;
428
429#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
430 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
431#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
432 {
433 ((void) minor_ver);
434 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
435 cur += sizeof( rec->ctr );
436 }
437
438 *cur = rec->type;
439 cur++;
440
441 memcpy( cur, rec->ver, sizeof( rec->ver ) );
442 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100443
Hanno Beckera0e20d02019-05-15 14:03:01 +0100444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100445 if( rec->cid_len != 0 )
446 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100447 memcpy( cur, rec->cid, rec->cid_len );
448 cur += rec->cid_len;
449
450 *cur = rec->cid_len;
451 cur++;
452
453 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
454 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
455 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100456 }
457 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100458#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100459 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100460 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
461 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
462 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100463 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100464
465 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000466}
467
Hanno Becker67a37db2020-05-28 16:27:07 +0100468#if defined(MBEDTLS_GCM_C) || \
469 defined(MBEDTLS_CCM_C) || \
470 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100471static int ssl_transform_aead_dynamic_iv_is_explicit(
472 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100473{
Hanno Becker17263802020-05-28 07:05:48 +0100474 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100475}
476
Hanno Becker17263802020-05-28 07:05:48 +0100477/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
478 *
479 * Concretely, this occurs in two variants:
480 *
481 * a) Fixed and dynamic IV lengths add up to total IV length, giving
482 * IV = fixed_iv || dynamic_iv
483 *
Hanno Becker15952812020-06-04 13:31:46 +0100484 * This variant is used in TLS 1.2 when used with GCM or CCM.
485 *
Hanno Becker17263802020-05-28 07:05:48 +0100486 * b) Fixed IV lengths matches total IV length, giving
487 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100488 *
489 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
490 *
491 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100492 *
493 * This function has the precondition that
494 *
495 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
496 *
497 * which has to be ensured by the caller. If this precondition
498 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100499 */
500static void ssl_build_record_nonce( unsigned char *dst_iv,
501 size_t dst_iv_len,
502 unsigned char const *fixed_iv,
503 size_t fixed_iv_len,
504 unsigned char const *dynamic_iv,
505 size_t dynamic_iv_len )
506{
507 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100508
509 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100510 memset( dst_iv, 0, dst_iv_len );
511 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100512
Hanno Becker17263802020-05-28 07:05:48 +0100513 dst_iv += dst_iv_len - dynamic_iv_len;
514 for( i = 0; i < dynamic_iv_len; i++ )
515 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100516}
Hanno Becker67a37db2020-05-28 16:27:07 +0100517#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100518
Hanno Beckera18d1322018-01-03 14:27:32 +0000519int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
520 mbedtls_ssl_transform *transform,
521 mbedtls_record *rec,
522 int (*f_rng)(void *, unsigned char *, size_t),
523 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100526 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000527 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100528 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100529 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000530 size_t post_avail;
531
532 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000533#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200534 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000535 ((void) ssl);
536#endif
537
538 /* The PRNG is used for dynamic IV generation that's used
539 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200540#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000541 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
542 ((void) f_rng);
543 ((void) p_rng);
544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000548 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100549 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
552 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100553 if( rec == NULL
554 || rec->buf == NULL
555 || rec->buf_len < rec->data_offset
556 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100557#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100558 || rec->cid_len != 0
559#endif
560 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000561 {
562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100564 }
565
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000566 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100567 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000569 data, rec->data_len );
570
571 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
572
573 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
574 {
575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
576 (unsigned) rec->data_len,
577 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
579 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100580
Hanno Becker92313402020-05-20 13:58:58 +0100581 /* The following two code paths implement the (D)TLSInnerPlaintext
582 * structure present in TLS 1.3 and DTLS 1.2 + CID.
583 *
584 * See ssl_build_inner_plaintext() for more information.
585 *
586 * Note that this changes `rec->data_len`, and hence
587 * `post_avail` needs to be recalculated afterwards.
588 *
589 * Note also that the two code paths cannot occur simultaneously
590 * since they apply to different versions of the protocol. There
591 * is hence no risk of double-addition of the inner plaintext.
592 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100593#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
594 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
595 {
Hanno Becker13996922020-05-28 16:15:19 +0100596 size_t padding =
597 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100598 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100599 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100600 &rec->data_len,
601 post_avail,
602 rec->type,
603 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100604 {
605 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
606 }
607
608 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
609 }
610#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
611
Hanno Beckera0e20d02019-05-15 14:03:01 +0100612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100613 /*
614 * Add CID information
615 */
616 rec->cid_len = transform->out_cid_len;
617 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
618 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100619
620 if( rec->cid_len != 0 )
621 {
Hanno Becker13996922020-05-28 16:15:19 +0100622 size_t padding =
623 ssl_compute_padding_length( rec->data_len,
624 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100625 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100626 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100627 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100628 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100629 * Note that this changes `rec->data_len`, and hence
630 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100631 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100632 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100633 &rec->data_len,
634 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100635 rec->type,
636 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100637 {
638 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
639 }
640
641 rec->type = MBEDTLS_SSL_MSG_CID;
642 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100643#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100644
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100645 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
646
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100648 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 */
Hanno Becker52344c22018-01-03 15:24:20 +0000650#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( mode == MBEDTLS_MODE_STREAM ||
652 ( mode == MBEDTLS_MODE_CBC
653#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000654 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100655#endif
656 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000658 if( post_avail < transform->maclen )
659 {
660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
661 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
664 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000665 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200666 {
Hanno Becker992b6872017-11-09 18:57:39 +0000667 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
668
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100669 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
670 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000671
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000672 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100673 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000674 mbedtls_md_hmac_update( &transform->md_ctx_enc,
675 data, rec->data_len );
676 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
677 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
678
679 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200680 }
681 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200686 }
687
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000688 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
689 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200690
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000691 rec->data_len += transform->maclen;
692 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100693 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200694 }
Hanno Becker52344c22018-01-03 15:24:20 +0000695#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200697 /*
698 * Encrypt
699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
701 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000706 "including %d bytes of padding",
707 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000709 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
710 transform->iv_enc, transform->ivlen,
711 data, rec->data_len,
712 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200715 return( ret );
716 }
717
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000718 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200722 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 }
Paul Bakker68884e32013-01-07 18:20:04 +0100724 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000726
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200727#if defined(MBEDTLS_GCM_C) || \
728 defined(MBEDTLS_CCM_C) || \
729 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200731 mode == MBEDTLS_MODE_CCM ||
732 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000733 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200735 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100736 unsigned char *dynamic_iv;
737 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100738 int dynamic_iv_is_explicit =
739 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000740
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100741 /* Check that there's space for the authentication tag. */
742 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000743 {
744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
745 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
746 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000747
Paul Bakker68884e32013-01-07 18:20:04 +0100748 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100749 * Build nonce for AEAD encryption.
750 *
751 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
752 * part of the IV is prepended to the ciphertext and
753 * can be chosen freely - in particular, it need not
754 * agree with the record sequence number.
755 * However, since ChaChaPoly as well as all AEAD modes
756 * in TLS 1.3 use the record sequence number as the
757 * dynamic part of the nonce, we uniformly use the
758 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100759 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100760 dynamic_iv = rec->ctr;
761 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200762
Hanno Becker17263802020-05-28 07:05:48 +0100763 ssl_build_record_nonce( iv, sizeof( iv ),
764 transform->iv_enc,
765 transform->fixed_ivlen,
766 dynamic_iv,
767 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100768
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100769 /*
770 * Build additional data for AEAD encryption.
771 * This depends on the TLS version.
772 */
773 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
774 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100775
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200776 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100777 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200778 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100779 dynamic_iv,
780 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000781 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100782 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200784 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000785 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000786
Paul Bakker68884e32013-01-07 18:20:04 +0100787 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200788 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200789 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100791 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000792 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100793 add_data, add_data_len,
794 data, rec->data_len, /* src */
795 data, rec->buf_len - (data - rec->buf), /* dst */
796 &rec->data_len,
797 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200800 return( ret );
801 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000802 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100803 data + rec->data_len - transform->taglen,
804 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100805 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000806 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100807
808 /*
809 * Prefix record content with dynamic IV in case it is explicit.
810 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100811 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100812 {
813 if( rec->data_offset < dynamic_iv_len )
814 {
815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
816 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
817 }
818
819 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
820 rec->data_offset -= dynamic_iv_len;
821 rec->data_len += dynamic_iv_len;
822 }
823
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100824 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000825 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100827#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200828#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000832 size_t padlen, i;
833 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000834
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000835 /* Currently we're always using minimal padding
836 * (up to 255 bytes would be allowed). */
837 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
838 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 padlen = 0;
840
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000841 /* Check there's enough space in the buffer for the padding. */
842 if( post_avail < padlen + 1 )
843 {
844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
845 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
846 }
847
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000849 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000851 rec->data_len += padlen + 1;
852 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000855 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000856 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
857 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000858 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000859 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000860 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000861 if( f_rng == NULL )
862 {
863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
865 }
866
867 if( rec->data_offset < transform->ivlen )
868 {
869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
870 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
871 }
872
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000873 /*
874 * Generate IV
875 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000876 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000877 if( ret != 0 )
878 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000879
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000880 memcpy( data - transform->ivlen, transform->iv_enc,
881 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000882
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000883 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000887 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000888 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200889 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000891 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
892 transform->iv_enc,
893 transform->ivlen,
894 data, rec->data_len,
895 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200898 return( ret );
899 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200900
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000901 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200905 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200906
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100907#if defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000908 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200909 {
910 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100911 * Save IV in TLS1
Paul Bakkercca5b812013-08-31 17:40:26 +0200912 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000913 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
914 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000916 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200917#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000918 {
919 data -= transform->ivlen;
920 rec->data_offset -= transform->ivlen;
921 rec->data_len += transform->ivlen;
922 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100925 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100926 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000927 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
928
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100929 /*
930 * MAC(MAC_write_key, seq_num +
931 * TLSCipherText.type +
932 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100933 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100934 * IV + // except for TLS 1.0
935 * ENC(content + padding + padding_length));
936 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000937
938 if( post_avail < transform->maclen)
939 {
940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
942 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100943
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100944 ssl_extract_add_data_from_record( add_data, &add_data_len,
945 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000948 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100949 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100950
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000951 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100952 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000953 mbedtls_md_hmac_update( &transform->md_ctx_enc,
954 data, rec->data_len );
955 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
956 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100957
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000958 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100959
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000960 rec->data_len += transform->maclen;
961 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100962 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100963 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200966 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200967#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200971 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100973 /* Make extra sure authentication was performed, exactly once */
974 if( auth_done != 1 )
975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100978 }
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982 return( 0 );
983}
984
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200985#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200986/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200987 * Turn a bit into a mask:
988 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
989 * - if bit == 0, return the all-bits 0 mask, aka 0
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200990 *
991 * This function can be used to write constant-time code by replacing branches
992 * with bit operations using masks.
993 *
994 * This function is implemented without using comparison operators, as those
995 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200996 */
997static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
998{
999 /* MSVC has a warning about unary minus on unsigned integer types,
1000 * but this is well-defined and precisely what we want to do here. */
1001#if defined(_MSC_VER)
1002#pragma warning( push )
1003#pragma warning( disable : 4146 )
1004#endif
1005 return -bit;
1006#if defined(_MSC_VER)
1007#pragma warning( pop )
1008#endif
1009}
1010
1011/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001012 * Constant-flow mask generation for "less than" comparison:
1013 * - if x < y, return all bits 1, that is (size_t) -1
1014 * - otherwise, return all bits 0, that is 0
1015 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001016 * This function can be used to write constant-time code by replacing branches
1017 * with bit operations using masks.
1018 *
1019 * This function is implemented without using comparison operators, as those
1020 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001021 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001022static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001023{
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001024 /* This has the most significant bit set if and only if x < y */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001025 const size_t sub = x - y;
1026
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001027 /* sub1 = (x < y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001028 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
1029
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001030 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001031 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001032
1033 return( mask );
1034}
1035
1036/*
1037 * Constant-flow mask generation for "greater or equal" comparison:
1038 * - if x >= y, return all bits 1, that is (size_t) -1
1039 * - otherwise, return all bits 0, that is 0
1040 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001041 * This function can be used to write constant-time code by replacing branches
1042 * with bit operations using masks.
1043 *
1044 * This function is implemented without using comparison operators, as those
1045 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001046 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001047static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001048{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001049 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001050}
1051
1052/*
1053 * Constant-flow boolean "equal" comparison:
1054 * return x == y
1055 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001056 * This function can be used to write constant-time code by replacing branches
1057 * with bit operations - it can be used in conjunction with
1058 * mbedtls_ssl_cf_mask_from_bit().
1059 *
1060 * This function is implemented without using comparison operators, as those
1061 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001062 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001063static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001064{
1065 /* diff = 0 if x == y, non-zero otherwise */
1066 const size_t diff = x ^ y;
1067
1068 /* MSVC has a warning about unary minus on unsigned integer types,
1069 * but this is well-defined and precisely what we want to do here. */
1070#if defined(_MSC_VER)
1071#pragma warning( push )
1072#pragma warning( disable : 4146 )
1073#endif
1074
1075 /* diff_msb's most significant bit is equal to x != y */
1076 const size_t diff_msb = ( diff | -diff );
1077
1078#if defined(_MSC_VER)
1079#pragma warning( pop )
1080#endif
1081
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001082 /* diff1 = (x != y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001083 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1084
1085 return( 1 ^ diff1 );
1086}
1087
1088/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001089 * Constant-flow conditional memcpy:
1090 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1091 * - otherwise, a no-op,
1092 * but with execution flow independent of the values of c1 and c2.
1093 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001094 * This function is implemented without using comparison operators, as those
1095 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001096 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001097static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1098 const unsigned char *src,
1099 size_t len,
1100 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001101{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001102 /* mask = c1 == c2 ? 0xff : 0x00 */
1103 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
Manuel Pégourié-Gonnard2a59fb42020-08-25 11:51:46 +02001104 const unsigned char mask = (unsigned char) mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001105
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001106 /* dst[i] = c1 == c2 ? src[i] : dst[i] */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001107 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001108 dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001109}
1110
1111/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001112 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001113 *
1114 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1115 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001116 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001117MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001118 mbedtls_md_context_t *ctx,
1119 const unsigned char *add_data, size_t add_data_len,
1120 const unsigned char *data, size_t data_len_secret,
1121 size_t min_data_len, size_t max_data_len,
1122 unsigned char *output )
1123{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001124 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001125 * This function breaks the HMAC abstraction and uses the md_clone()
1126 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001127 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001128 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001129 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001130 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001131 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001132 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1133 * minlen, then cloning the context, and for each byte up to maxlen
1134 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001135 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001136 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001137 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001138 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001139 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1140 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001141 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001142 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001143 const unsigned char * const okey = ikey + block_size;
1144 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001145
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001146 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1147 mbedtls_md_context_t aux;
1148 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001150
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001151 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001152
1153#define MD_CHK( func_call ) \
1154 do { \
1155 ret = (func_call); \
1156 if( ret != 0 ) \
1157 goto cleanup; \
1158 } while( 0 )
1159
1160 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001161
1162 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1163 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001164 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1165 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001166
1167 /* For each possible length, compute the hash up to that point */
1168 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001169 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001170 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1171 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001172 /* Keep only the correct inner_hash in the output buffer */
1173 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1174 offset, data_len_secret );
1175
1176 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001177 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001178 }
1179
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001180 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001181 MD_CHK( mbedtls_md_starts( ctx ) );
1182 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1183 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1184 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001185
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001186 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001187 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001188
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001189#undef MD_CHK
1190
1191cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001192 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001193 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001194}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001195
1196/*
1197 * Constant-flow memcpy from variable position in buffer.
1198 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001199 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001200 */
1201MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1202 unsigned char *dst,
1203 const unsigned char *src_base,
1204 size_t offset_secret,
1205 size_t offset_min, size_t offset_max,
1206 size_t len )
1207{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001208 size_t offset;
1209
1210 for( offset = offset_min; offset <= offset_max; offset++ )
1211 {
1212 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1213 offset, offset_secret );
1214 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001215}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001216#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001217
Hanno Becker605949f2019-07-12 08:23:59 +01001218int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001219 mbedtls_ssl_transform *transform,
1220 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001221{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001222 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001224 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001225#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001226 size_t padlen = 0, correct = 1;
1227#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001228 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001229 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001230 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001231
Hanno Beckera18d1322018-01-03 14:27:32 +00001232#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001233 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001234 ((void) ssl);
1235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001238 if( rec == NULL ||
1239 rec->buf == NULL ||
1240 rec->buf_len < rec->data_offset ||
1241 rec->buf_len - rec->data_offset < rec->data_len )
1242 {
1243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001245 }
1246
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001247 data = rec->buf + rec->data_offset;
1248 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Hanno Beckera0e20d02019-05-15 14:03:01 +01001250#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001251 /*
1252 * Match record's CID with incoming CID.
1253 */
Hanno Becker938489a2019-05-08 13:02:22 +01001254 if( rec->cid_len != transform->in_cid_len ||
1255 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1256 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001257 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001258 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001259#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1262 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001263 {
1264 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001265 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1266 transform->iv_dec,
1267 transform->ivlen,
1268 data, rec->data_len,
1269 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001272 return( ret );
1273 }
1274
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001275 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 }
Paul Bakker68884e32013-01-07 18:20:04 +01001281 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001283#if defined(MBEDTLS_GCM_C) || \
1284 defined(MBEDTLS_CCM_C) || \
1285 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001287 mode == MBEDTLS_MODE_CCM ||
1288 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001289 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001290 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001291 unsigned char *dynamic_iv;
1292 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001293
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001294 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001295 * Extract dynamic part of nonce for AEAD decryption.
1296 *
1297 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1298 * part of the IV is prepended to the ciphertext and
1299 * can be chosen freely - in particular, it need not
1300 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001301 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001302 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001303 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001304 {
1305 if( rec->data_len < dynamic_iv_len )
1306 {
1307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) ",
1308 rec->data_len,
1309 dynamic_iv_len ) );
1310 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1311 }
1312 dynamic_iv = data;
1313
1314 data += dynamic_iv_len;
1315 rec->data_offset += dynamic_iv_len;
1316 rec->data_len -= dynamic_iv_len;
1317 }
Hanno Becker17263802020-05-28 07:05:48 +01001318 else
1319 {
1320 dynamic_iv = rec->ctr;
1321 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001322
1323 /* Check that there's space for the authentication tag. */
1324 if( rec->data_len < transform->taglen )
1325 {
Christian von Arnim883d3042020-12-01 11:58:29 +01001326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < taglen (%d) ",
1327 rec->data_len,
1328 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001330 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001331 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001332
Hanno Beckerdf8be222020-05-21 15:30:57 +01001333 /*
1334 * Prepare nonce from dynamic and static parts.
1335 */
Hanno Becker17263802020-05-28 07:05:48 +01001336 ssl_build_record_nonce( iv, sizeof( iv ),
1337 transform->iv_dec,
1338 transform->fixed_ivlen,
1339 dynamic_iv,
1340 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001341
Hanno Beckerdf8be222020-05-21 15:30:57 +01001342 /*
1343 * Build additional data for AEAD encryption.
1344 * This depends on the TLS version.
1345 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001346 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1347 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001348 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001349 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001350
Hanno Beckerd96a6522019-07-10 13:55:25 +01001351 /* Because of the check above, we know that there are
1352 * explicit_iv_len Bytes preceeding data, and taglen
1353 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001354 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001355 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001356
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001357 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001358 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001359 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001360
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001361 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001362 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001363 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001364 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001365 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001366 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001367 data, rec->data_len + transform->taglen, /* src */
1368 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001369 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1374 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001375
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001376 return( ret );
1377 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001378 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001379
Hanno Beckerd96a6522019-07-10 13:55:25 +01001380 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001381 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001385 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001389#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001392 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001393
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 /*
Paul Bakker45829992013-01-03 14:52:21 +01001395 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001398 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1399 {
1400 /* The ciphertext is prefixed with the CBC IV. */
1401 minlen += transform->ivlen;
1402 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001403#endif
Paul Bakker45829992013-01-03 14:52:21 +01001404
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001405 /* Size considerations:
1406 *
1407 * - The CBC cipher text must not be empty and hence
1408 * at least of size transform->ivlen.
1409 *
1410 * Together with the potential IV-prefix, this explains
1411 * the first of the two checks below.
1412 *
1413 * - The record must contain a MAC, either in plain or
1414 * encrypted, depending on whether Encrypt-then-MAC
1415 * is used or not.
1416 * - If it is, the message contains the IV-prefix,
1417 * the CBC ciphertext, and the MAC.
1418 * - If it is not, the padded plaintext, and hence
1419 * the CBC ciphertext, has at least length maclen + 1
1420 * because there is at least the padding length byte.
1421 *
1422 * As the CBC ciphertext is not empty, both cases give the
1423 * lower bound minlen + maclen + 1 on the record size, which
1424 * we test for in the second check below.
1425 */
1426 if( rec->data_len < minlen + transform->ivlen ||
1427 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001430 "+ 1 ) ( + expl IV )", rec->data_len,
1431 transform->ivlen,
1432 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001434 }
1435
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001436 /*
1437 * Authenticate before decrypt if enabled
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001440 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001441 {
Hanno Becker992b6872017-11-09 18:57:39 +00001442 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001445
Hanno Beckerd96a6522019-07-10 13:55:25 +01001446 /* Update data_len in tandem with add_data.
1447 *
1448 * The subtraction is safe because of the previous check
1449 * data_len >= minlen + maclen + 1.
1450 *
1451 * Afterwards, we know that data + data_len is followed by at
1452 * least maclen Bytes, which justifies the call to
1453 * mbedtls_ssl_safer_memcmp() below.
1454 *
1455 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001456 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001457 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1458 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001459
Hanno Beckerd96a6522019-07-10 13:55:25 +01001460 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001461 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1462 add_data_len );
1463 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1464 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001465 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1466 data, rec->data_len );
1467 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1468 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001469
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001470 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1471 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001472 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001473 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001474
Hanno Beckerd96a6522019-07-10 13:55:25 +01001475 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001476 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1477 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001481 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001482 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001485
1486 /*
1487 * Check length sanity
1488 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001489
1490 /* We know from above that data_len > minlen >= 0,
1491 * so the following check in particular implies that
1492 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001493 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001496 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001498 }
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001501 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001502 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001503 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001504 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001506 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001507 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001508
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001509 data += transform->ivlen;
1510 rec->data_offset += transform->ivlen;
1511 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514
Hanno Beckerd96a6522019-07-10 13:55:25 +01001515 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1516
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001517 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1518 transform->iv_dec, transform->ivlen,
1519 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001522 return( ret );
1523 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001524
Hanno Beckerd96a6522019-07-10 13:55:25 +01001525 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001526 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001530 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001531
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001532#if defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001533 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001534 {
1535 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001536 * Save IV in TLS1, where CBC decryption of consecutive
Hanno Beckerd96a6522019-07-10 13:55:25 +01001537 * records is equivalent to CBC decryption of the concatenation
1538 * of the records; in other words, IVs are maintained across
1539 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001540 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001541 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1542 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001546 /* Safe since data_len >= minlen + maclen + 1, so after having
1547 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001548 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1549 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001550 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001551
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001552 if( auth_done == 1 )
1553 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001554 const size_t mask = mbedtls_ssl_cf_mask_ge(
1555 rec->data_len,
1556 padlen + 1 );
1557 correct &= mask;
1558 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001559 }
1560 else
Paul Bakker45829992013-01-03 14:52:21 +01001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001563 if( rec->data_len < transform->maclen + padlen + 1 )
1564 {
1565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1566 rec->data_len,
1567 transform->maclen,
1568 padlen + 1 ) );
1569 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001570#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001571
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001572 const size_t mask = mbedtls_ssl_cf_mask_ge(
1573 rec->data_len,
1574 transform->maclen + padlen + 1 );
1575 correct &= mask;
1576 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001579 padlen++;
1580
1581 /* Regardless of the validity of the padding,
1582 * we have data_len >= padlen here. */
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1585 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001586 /* The padding check involves a series of up to 256
1587 * consecutive memory reads at the end of the record
1588 * plaintext buffer. In order to hide the length and
1589 * validity of the padding, always perform exactly
1590 * `min(256,plaintext_len)` reads (but take into account
1591 * only the last `padlen` bytes for the padding check). */
1592 size_t pad_count = 0;
1593 volatile unsigned char* const check = data;
1594
1595 /* Index of first padding byte; it has been ensured above
1596 * that the subtraction is safe. */
1597 size_t const padding_idx = rec->data_len - padlen;
1598 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1599 size_t const start_idx = rec->data_len - num_checks;
1600 size_t idx;
1601
1602 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001604 /* pad_count += (idx >= padding_idx) &&
1605 * (check[idx] == padlen - 1);
1606 */
1607 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1608 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1609 padlen - 1 );
1610 pad_count += mask & equal;
1611 }
1612 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001615 if( padlen > 0 && correct == 0 )
1616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001617#endif
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001618 padlen &= mbedtls_ssl_cf_mask_from_bit( correct );
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1621 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001622
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001623 /* If the padding was found to be invalid, padlen == 0
1624 * and the subtraction is safe. If the padding was found valid,
1625 * padlen hasn't been changed and the previous assertion
1626 * data_len >= padlen still holds. */
1627 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001629 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001630#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001636#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001638 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001639#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
1641 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001642 * Authenticate if not done yet.
1643 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 */
Hanno Becker52344c22018-01-03 15:24:20 +00001645#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001646 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 {
Hanno Becker992b6872017-11-09 18:57:39 +00001648 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001649 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001650
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001651 /* If the initial value of padlen was such that
1652 * data_len < maclen + padlen + 1, then padlen
1653 * got reset to 1, and the initial check
1654 * data_len >= minlen + maclen + 1
1655 * guarantees that at this point we still
1656 * have at least data_len >= maclen.
1657 *
1658 * If the initial value of padlen was such that
1659 * data_len >= maclen + padlen + 1, then we have
1660 * subtracted either padlen + 1 (if the padding was correct)
1661 * or 0 (if the padding was incorrect) since then,
1662 * hence data_len >= maclen in any case.
1663 */
1664 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001665 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1666 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1669 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001670 /*
1671 * The next two sizes are the minimum and maximum values of
1672 * data_len over all padlen values.
1673 *
1674 * They're independent of padlen, since we previously did
1675 * data_len -= padlen.
1676 *
1677 * Note that max_len + maclen is never more than the buffer
1678 * length, as we previously did in_msglen -= maclen too.
1679 */
1680 const size_t max_len = rec->data_len + padlen;
1681 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1682
1683 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1684 add_data, add_data_len,
1685 data, rec->data_len, min_len, max_len,
1686 mac_expect );
1687 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1690 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001692
1693 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1694 rec->data_len,
1695 min_len, max_len,
1696 transform->maclen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1698 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001701 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001702 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001703#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001705 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001706 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_DEBUG_ALL)
1709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001710#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 correct = 0;
1712 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001713 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001715
1716 /*
1717 * Finally check the correct flag
1718 */
1719 if( correct == 0 )
1720 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001721#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001722
1723 /* Make extra sure authentication was performed, exactly once */
1724 if( auth_done != 1 )
1725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Hanno Beckerccc13d02020-05-04 12:30:04 +01001730#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1731 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1732 {
1733 /* Remove inner padding and infer true content type. */
1734 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1735 &rec->type );
1736
1737 if( ret != 0 )
1738 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1739 }
1740#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1741
Hanno Beckera0e20d02019-05-15 14:03:01 +01001742#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001743 if( rec->cid_len != 0 )
1744 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001745 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1746 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001747 if( ret != 0 )
1748 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1749 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001750#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
1754 return( 0 );
1755}
1756
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001757#undef MAC_NONE
1758#undef MAC_PLAINTEXT
1759#undef MAC_CIPHERTEXT
1760
Paul Bakker5121ce52009-01-03 21:22:43 +00001761/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001762 * Fill the input message buffer by appending data to it.
1763 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001764 *
1765 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1766 * available (from this read and/or a previous one). Otherwise, an error code
1767 * is returned (possibly EOF or WANT_READ).
1768 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001769 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1770 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1771 * since we always read a whole datagram at once.
1772 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001773 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001774 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777{
Janos Follath865b3eb2019-12-16 11:46:15 +00001778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001779 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001780#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1781 size_t in_buf_len = ssl->in_buf_len;
1782#else
1783 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1784#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001788 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001791 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001793 }
1794
Darryl Greenb33cc762019-11-28 14:29:44 +00001795 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001802 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001804 uint32_t timeout;
1805
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001806 /*
1807 * The point is, we need to always read a full datagram at once, so we
1808 * sometimes read more then requested, and handle the additional data.
1809 * It could be the rest of the current record (while fetching the
1810 * header) and/or some other records in the same datagram.
1811 */
1812
1813 /*
1814 * Move to the next record in the already read datagram if applicable
1815 */
1816 if( ssl->next_record_offset != 0 )
1817 {
1818 if( ssl->in_left < ssl->next_record_offset )
1819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001822 }
1823
1824 ssl->in_left -= ssl->next_record_offset;
1825
1826 if( ssl->in_left != 0 )
1827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001829 ssl->next_record_offset ) );
1830 memmove( ssl->in_hdr,
1831 ssl->in_hdr + ssl->next_record_offset,
1832 ssl->in_left );
1833 }
1834
1835 ssl->next_record_offset = 0;
1836 }
1837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001840
1841 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001842 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001843 */
1844 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001847 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001848 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001849
1850 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001851 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001852 * are not at the beginning of a new record, the caller did something
1853 * wrong.
1854 */
1855 if( ssl->in_left != 0 )
1856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001859 }
1860
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001861 /*
1862 * Don't even try to read if time's out already.
1863 * This avoids by-passing the timer when repeatedly receiving messages
1864 * that will end up being dropped.
1865 */
Hanno Becker7876d122020-02-05 10:39:31 +00001866 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001867 {
1868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001869 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001870 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001871 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001872 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001873 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001876 timeout = ssl->handshake->retransmit_timeout;
1877 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001878 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001881
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001882 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001883 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1884 timeout );
1885 else
1886 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001889
1890 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001892 }
1893
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001894 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001897 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001900 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001901 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001904 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001905 }
1906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001910 return( ret );
1911 }
1912
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001913 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001916 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001918 {
Hanno Becker786300f2020-02-05 10:46:40 +00001919 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001920 {
Hanno Becker786300f2020-02-05 10:46:40 +00001921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1922 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001923 return( ret );
1924 }
1925
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001926 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001929 }
1930
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 if( ret < 0 )
1932 return( ret );
1933
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001934 ssl->in_left = ret;
1935 }
1936 else
1937#endif
1938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001940 ssl->in_left, nb_want ) );
1941
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001942 while( ssl->in_left < nb_want )
1943 {
1944 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001945
Hanno Becker7876d122020-02-05 10:39:31 +00001946 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001947 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1948 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001949 {
1950 if( ssl->f_recv_timeout != NULL )
1951 {
1952 ret = ssl->f_recv_timeout( ssl->p_bio,
1953 ssl->in_hdr + ssl->in_left, len,
1954 ssl->conf->read_timeout );
1955 }
1956 else
1957 {
1958 ret = ssl->f_recv( ssl->p_bio,
1959 ssl->in_hdr + ssl->in_left, len );
1960 }
1961 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001964 ssl->in_left, nb_want ) );
1965 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001966
1967 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001969
1970 if( ret < 0 )
1971 return( ret );
1972
makise-homuraaf9513b2020-08-24 18:26:27 +03001973 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001974 {
Darryl Green11999bb2018-03-13 15:22:58 +00001975 MBEDTLS_SSL_DEBUG_MSG( 1,
1976 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07001977 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1979 }
1980
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001981 ssl->in_left += ret;
1982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 return( 0 );
1988}
1989
1990/*
1991 * Flush any data not yet written
1992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001994{
Janos Follath865b3eb2019-12-16 11:46:15 +00001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001996 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002000 if( ssl->f_send == NULL )
2001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002003 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002005 }
2006
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002007 /* Avoid incrementing counter if data is flushed */
2008 if( ssl->out_left == 0 )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002011 return( 0 );
2012 }
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 while( ssl->out_left > 0 )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002017 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Hanno Becker2b1e3542018-08-06 11:19:13 +01002019 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002020 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
2024 if( ret <= 0 )
2025 return( ret );
2026
makise-homuraaf9513b2020-08-24 18:26:27 +03002027 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002028 {
Darryl Green11999bb2018-03-13 15:22:58 +00002029 MBEDTLS_SSL_DEBUG_MSG( 1,
2030 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002031 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002032 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2033 }
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 ssl->out_left -= ret;
2036 }
2037
Hanno Becker2b1e3542018-08-06 11:19:13 +01002038#if defined(MBEDTLS_SSL_PROTO_DTLS)
2039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002040 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002041 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002042 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002043 else
2044#endif
2045 {
2046 ssl->out_hdr = ssl->out_buf + 8;
2047 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002048 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
2052 return( 0 );
2053}
2054
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002055/*
2056 * Functions to handle the DTLS retransmission state machine
2057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002059/*
2060 * Append current handshake message to current outgoing flight
2061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2066 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2067 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002068
2069 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002070 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002071 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002074 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002075 }
2076
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002077 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002078 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002081 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002082 }
2083
2084 /* Copy current handshake message with headers */
2085 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2086 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002087 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002088 msg->next = NULL;
2089
2090 /* Append to the current flight */
2091 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002092 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002093 else
2094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002096 while( cur->next != NULL )
2097 cur = cur->next;
2098 cur->next = msg;
2099 }
2100
Hanno Becker3b235902018-08-06 09:54:53 +01002101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002102 return( 0 );
2103}
2104
2105/*
2106 * Free the current flight of handshake messages
2107 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002108void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_ssl_flight_item *cur = flight;
2111 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002112
2113 while( cur != NULL )
2114 {
2115 next = cur->next;
2116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_free( cur->p );
2118 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002119
2120 cur = next;
2121 }
2122}
2123
2124/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002125 * Swap transform_out and out_ctr with the alternative ones
2126 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002127static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002130 unsigned char tmp_out_ctr[8];
2131
2132 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002135 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002139
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002140 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002141 tmp_transform = ssl->transform_out;
2142 ssl->transform_out = ssl->handshake->alt_transform_out;
2143 ssl->handshake->alt_transform_out = tmp_transform;
2144
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002145 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002146 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2147 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002148 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002149
2150 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002151 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2154 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002155 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002156 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2157 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2160 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002161 }
2162 }
2163#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002164
2165 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002166}
2167
2168/*
2169 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002170 */
2171int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2172{
2173 int ret = 0;
2174
2175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2176
2177 ret = mbedtls_ssl_flight_transmit( ssl );
2178
2179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2180
2181 return( ret );
2182}
2183
2184/*
2185 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002186 *
2187 * Need to remember the current message in case flush_output returns
2188 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002189 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002190 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002191int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002192{
Janos Follath865b3eb2019-12-16 11:46:15 +00002193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002197 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199
2200 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002201 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002202 ret = ssl_swap_epochs( ssl );
2203 if( ret != 0 )
2204 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002208
2209 while( ssl->handshake->cur_msg != NULL )
2210 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002211 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002212 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002213
Hanno Beckere1dcb032018-08-17 16:47:58 +01002214 int const is_finished =
2215 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2216 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2217
Hanno Becker04da1892018-08-14 13:22:10 +01002218 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2219 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2220
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002221 /* Swap epochs before sending Finished: we can't do it after
2222 * sending ChangeCipherSpec, in case write returns WANT_READ.
2223 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002224 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002225 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002227 ret = ssl_swap_epochs( ssl );
2228 if( ret != 0 )
2229 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002230 }
2231
Hanno Becker67bc7c32018-08-06 11:33:50 +01002232 ret = ssl_get_remaining_payload_in_datagram( ssl );
2233 if( ret < 0 )
2234 return( ret );
2235 max_frag_len = (size_t) ret;
2236
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002237 /* CCS is copied as is, while HS messages may need fragmentation */
2238 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2239 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002240 if( max_frag_len == 0 )
2241 {
2242 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2243 return( ret );
2244
2245 continue;
2246 }
2247
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002248 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002249 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002250 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002251
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002252 /* Update position inside current message */
2253 ssl->handshake->cur_msg_p += cur->len;
2254 }
2255 else
2256 {
2257 const unsigned char * const p = ssl->handshake->cur_msg_p;
2258 const size_t hs_len = cur->len - 12;
2259 const size_t frag_off = p - ( cur->p + 12 );
2260 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002261 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002262
Hanno Beckere1dcb032018-08-17 16:47:58 +01002263 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002264 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002265 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002266 {
2267 ret = ssl_swap_epochs( ssl );
2268 if( ret != 0 )
2269 return( ret );
2270 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002271
Hanno Becker67bc7c32018-08-06 11:33:50 +01002272 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2273 return( ret );
2274
2275 continue;
2276 }
2277 max_hs_frag_len = max_frag_len - 12;
2278
2279 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2280 max_hs_frag_len : rem_len;
2281
2282 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002283 {
2284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002285 (unsigned) cur_hs_frag_len,
2286 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002287 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002288
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002289 /* Messages are stored with handshake headers as if not fragmented,
2290 * copy beginning of headers then fill fragmentation fields.
2291 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2292 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002293
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002294 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2295 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2296 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2297
Hanno Becker67bc7c32018-08-06 11:33:50 +01002298 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2299 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2300 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002301
2302 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2303
Hanno Becker3f7b9732018-08-28 09:53:25 +01002304 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002305 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2306 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002307 ssl->out_msgtype = cur->type;
2308
2309 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002310 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002311 }
2312
2313 /* If done with the current message move to the next one if any */
2314 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2315 {
2316 if( cur->next != NULL )
2317 {
2318 ssl->handshake->cur_msg = cur->next;
2319 ssl->handshake->cur_msg_p = cur->next->p + 12;
2320 }
2321 else
2322 {
2323 ssl->handshake->cur_msg = NULL;
2324 ssl->handshake->cur_msg_p = NULL;
2325 }
2326 }
2327
2328 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002329 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002332 return( ret );
2333 }
2334 }
2335
Hanno Becker67bc7c32018-08-06 11:33:50 +01002336 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2337 return( ret );
2338
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002339 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2341 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002342 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002345 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002346 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002347
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002349
2350 return( 0 );
2351}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002352
2353/*
2354 * To be called when the last message of an incoming flight is received.
2355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002357{
2358 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002359 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002360 ssl->handshake->flight = NULL;
2361 ssl->handshake->cur_msg = NULL;
2362
2363 /* The next incoming flight will start with this msg_seq */
2364 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2365
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002366 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002367 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002368
Hanno Becker0271f962018-08-16 13:23:47 +01002369 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002370 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002371
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002372 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002373 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2376 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002379 }
2380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002382}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002383
2384/*
2385 * To be called when the last message of an outgoing flight is send.
2386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002388{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002389 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002390 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2393 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002396 }
2397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002401
Paul Bakker5121ce52009-01-03 21:22:43 +00002402/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002403 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002405
2406/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002407 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002408 *
2409 * - fill in handshake headers
2410 * - update handshake checksum
2411 * - DTLS: save message for resending
2412 * - then pass to the record layer
2413 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002414 * DTLS: except for HelloRequest, messages are only queued, and will only be
2415 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002416 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002417 * Inputs:
2418 * - ssl->out_msglen: 4 + actual handshake message len
2419 * (4 is the size of handshake headers for TLS)
2420 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2421 * - ssl->out_msg + 4: the handshake message body
2422 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002423 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002424 * - ssl->out_msglen: the length of the record contents
2425 * (including handshake headers but excluding record headers)
2426 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002427 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002428int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002429{
Janos Follath865b3eb2019-12-16 11:46:15 +00002430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002431 const size_t hs_len = ssl->out_msglen - 4;
2432 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2435
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002436 /*
2437 * Sanity checks
2438 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002439 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002440 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2441 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002446 /* Whenever we send anything different from a
2447 * HelloRequest we should be in a handshake - double check. */
2448 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2449 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002450 ssl->handshake == NULL )
2451 {
2452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002457 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002458 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002460 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002463 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002464#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002465
Hanno Beckerb50a2532018-08-06 11:52:54 +01002466 /* Double-check that we did not exceed the bounds
2467 * of the outgoing record buffer.
2468 * This should never fail as the various message
2469 * writing functions must obey the bounds of the
2470 * outgoing record buffer, but better be safe.
2471 *
2472 * Note: We deliberately do not check for the MTU or MFL here.
2473 */
2474 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2475 {
2476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2477 "size %u, maximum %u",
2478 (unsigned) ssl->out_msglen,
2479 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2481 }
2482
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002483 /*
2484 * Fill handshake headers
2485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002488 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2489 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2490 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002492 /*
2493 * DTLS has additional fields in the Handshake layer,
2494 * between the length field and the actual payload:
2495 * uint16 message_seq;
2496 * uint24 fragment_offset;
2497 * uint24 fragment_length;
2498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002501 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002502 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002503 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002504 {
2505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2506 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002507 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002508 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2510 }
2511
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002512 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002513 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002514
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002515 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002516 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002517 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002518 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2519 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2520 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002521 }
2522 else
2523 {
2524 ssl->out_msg[4] = 0;
2525 ssl->out_msg[5] = 0;
2526 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002527
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002528 /* Handshake hashes are computed without fragmentation,
2529 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002530 memset( ssl->out_msg + 6, 0x00, 3 );
2531 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002534
Hanno Becker0207e532018-08-28 10:28:28 +01002535 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002536 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2537 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002540 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002542 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002543 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2544 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002545 {
2546 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002549 return( ret );
2550 }
2551 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002552 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002553#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002554 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002555 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002556 {
2557 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2558 return( ret );
2559 }
2560 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002561
2562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2563
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002564 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002565}
2566
2567/*
2568 * Record layer functions
2569 */
2570
2571/*
2572 * Write current record.
2573 *
2574 * Uses:
2575 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2576 * - ssl->out_msglen: length of the record content (excl headers)
2577 * - ssl->out_msg: record content
2578 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002579int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002580{
2581 int ret, done = 0;
2582 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002583 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002584
2585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2588 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 ret = mbedtls_ssl_hw_record_write( ssl );
2593 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2596 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002597 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002598
2599 if( ret == 0 )
2600 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002603 if( !done )
2604 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002605 unsigned i;
2606 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002607#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2608 size_t out_buf_len = ssl->out_buf_len;
2609#else
2610 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2611#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002612 /* Skip writing the record content type to after the encryption,
2613 * as it may change when using the CID extension. */
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002616 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002617
Hanno Becker19859472018-08-06 09:40:20 +01002618 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002619 ssl->out_len[0] = (unsigned char)( len >> 8 );
2620 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002621
Paul Bakker48916f92012-09-16 19:57:18 +00002622 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002623 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002624 mbedtls_record rec;
2625
2626 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002627 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002628 rec.data_len = ssl->out_msglen;
2629 rec.data_offset = ssl->out_msg - rec.buf;
2630
2631 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2632 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2633 ssl->conf->transport, rec.ver );
2634 rec.type = ssl->out_msgtype;
2635
Hanno Beckera0e20d02019-05-15 14:03:01 +01002636#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002637 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002638 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002639#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002640
Hanno Beckera18d1322018-01-03 14:27:32 +00002641 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002642 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002645 return( ret );
2646 }
2647
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002648 if( rec.data_offset != 0 )
2649 {
2650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2652 }
2653
Hanno Becker6430faf2019-05-08 11:57:13 +01002654 /* Update the record content type and CID. */
2655 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002656#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002657 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002658#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002659 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2661 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002662 }
2663
Hanno Becker5903de42019-05-03 14:46:38 +01002664 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002665
2666#if defined(MBEDTLS_SSL_PROTO_DTLS)
2667 /* In case of DTLS, double-check that we don't exceed
2668 * the remaining space in the datagram. */
2669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2670 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002671 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002672 if( ret < 0 )
2673 return( ret );
2674
2675 if( protected_record_size > (size_t) ret )
2676 {
2677 /* Should never happen */
2678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2679 }
2680 }
2681#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002682
Hanno Becker6430faf2019-05-08 11:57:13 +01002683 /* Now write the potentially updated record content type. */
2684 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002687 "version = [%d:%d], msglen = %d",
2688 ssl->out_hdr[0], ssl->out_hdr[1],
2689 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002692 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002693
2694 ssl->out_left += protected_record_size;
2695 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002696 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002697
Hanno Beckerdd772292020-02-05 10:38:31 +00002698 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002699 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2700 break;
2701
2702 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002703 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002704 {
2705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2706 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
2709
Hanno Becker67bc7c32018-08-06 11:33:50 +01002710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2712 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002713 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002714 size_t remaining;
2715 ret = ssl_get_remaining_payload_in_datagram( ssl );
2716 if( ret < 0 )
2717 {
2718 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2719 ret );
2720 return( ret );
2721 }
2722
2723 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002724 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002725 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002726 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002727 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002728 else
2729 {
Hanno Becker513815a2018-08-20 11:56:09 +01002730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002731 }
2732 }
2733#endif /* MBEDTLS_SSL_PROTO_DTLS */
2734
2735 if( ( flush == SSL_FORCE_FLUSH ) &&
2736 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 return( ret );
2740 }
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
2744 return( 0 );
2745}
2746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002748
2749static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2750{
2751 if( ssl->in_msglen < ssl->in_hslen ||
2752 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2753 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2754 {
2755 return( 1 );
2756 }
2757 return( 0 );
2758}
Hanno Becker44650b72018-08-16 12:51:11 +01002759
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002760static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002761{
2762 return( ( ssl->in_msg[9] << 16 ) |
2763 ( ssl->in_msg[10] << 8 ) |
2764 ssl->in_msg[11] );
2765}
2766
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002767static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002768{
2769 return( ( ssl->in_msg[6] << 16 ) |
2770 ( ssl->in_msg[7] << 8 ) |
2771 ssl->in_msg[8] );
2772}
2773
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002774static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002775{
2776 uint32_t msg_len, frag_off, frag_len;
2777
2778 msg_len = ssl_get_hs_total_len( ssl );
2779 frag_off = ssl_get_hs_frag_off( ssl );
2780 frag_len = ssl_get_hs_frag_len( ssl );
2781
2782 if( frag_off > msg_len )
2783 return( -1 );
2784
2785 if( frag_len > msg_len - frag_off )
2786 return( -1 );
2787
2788 if( frag_len + 12 > ssl->in_msglen )
2789 return( -1 );
2790
2791 return( 0 );
2792}
2793
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002794/*
2795 * Mark bits in bitmask (used for DTLS HS reassembly)
2796 */
2797static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2798{
2799 unsigned int start_bits, end_bits;
2800
2801 start_bits = 8 - ( offset % 8 );
2802 if( start_bits != 8 )
2803 {
2804 size_t first_byte_idx = offset / 8;
2805
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002806 /* Special case */
2807 if( len <= start_bits )
2808 {
2809 for( ; len != 0; len-- )
2810 mask[first_byte_idx] |= 1 << ( start_bits - len );
2811
2812 /* Avoid potential issues with offset or len becoming invalid */
2813 return;
2814 }
2815
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002816 offset += start_bits; /* Now offset % 8 == 0 */
2817 len -= start_bits;
2818
2819 for( ; start_bits != 0; start_bits-- )
2820 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2821 }
2822
2823 end_bits = len % 8;
2824 if( end_bits != 0 )
2825 {
2826 size_t last_byte_idx = ( offset + len ) / 8;
2827
2828 len -= end_bits; /* Now len % 8 == 0 */
2829
2830 for( ; end_bits != 0; end_bits-- )
2831 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2832 }
2833
2834 memset( mask + offset / 8, 0xFF, len / 8 );
2835}
2836
2837/*
2838 * Check that bitmask is full
2839 */
2840static int ssl_bitmask_check( unsigned char *mask, size_t len )
2841{
2842 size_t i;
2843
2844 for( i = 0; i < len / 8; i++ )
2845 if( mask[i] != 0xFF )
2846 return( -1 );
2847
2848 for( i = 0; i < len % 8; i++ )
2849 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2850 return( -1 );
2851
2852 return( 0 );
2853}
2854
Hanno Becker56e205e2018-08-16 09:06:12 +01002855/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002856static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002857 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002858{
Hanno Becker56e205e2018-08-16 09:06:12 +01002859 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002860
Hanno Becker56e205e2018-08-16 09:06:12 +01002861 alloc_len = 12; /* Handshake header */
2862 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002863
Hanno Beckerd07df862018-08-16 09:14:58 +01002864 if( add_bitmap )
2865 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002866
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002867 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002868}
Hanno Becker56e205e2018-08-16 09:06:12 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002871
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002872static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002873{
2874 return( ( ssl->in_msg[1] << 16 ) |
2875 ( ssl->in_msg[2] << 8 ) |
2876 ssl->in_msg[3] );
2877}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002878
Simon Butcher99000142016-10-13 17:21:01 +01002879int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002884 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002886 }
2887
Hanno Becker12555c62018-08-16 12:47:53 +01002888 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002891 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002892 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002895 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002896 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002898 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002899
Hanno Becker44650b72018-08-16 12:51:11 +01002900 if( ssl_check_hs_header( ssl ) != 0 )
2901 {
2902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2904 }
2905
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002906 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002907 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2908 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2909 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2910 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002911 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002912 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2913 {
2914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2915 recv_msg_seq,
2916 ssl->handshake->in_msg_seq ) );
2917 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2918 }
2919
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002920 /* Retransmit only on last message from previous flight, to avoid
2921 * too many retransmissions.
2922 * Besides, No sane server ever retransmits HelloVerifyRequest */
2923 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002927 "message_seq = %d, start_of_flight = %d",
2928 recv_msg_seq,
2929 ssl->handshake->in_flight_start_seq ) );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002934 return( ret );
2935 }
2936 }
2937 else
2938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002940 "message_seq = %d, expected = %d",
2941 recv_msg_seq,
2942 ssl->handshake->in_msg_seq ) );
2943 }
2944
Hanno Becker90333da2017-10-10 11:27:13 +01002945 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002946 }
2947 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002948
Hanno Becker6d97ef52018-08-16 13:09:04 +01002949 /* Message reassembly is handled alongside buffering of future
2950 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002951 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002952 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002953 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002956 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002957 }
2958 }
2959 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002961 /* With TLS we don't handle fragmentation (for now) */
2962 if( ssl->in_msglen < ssl->in_hslen )
2963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2965 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002966 }
2967
Simon Butcher99000142016-10-13 17:21:01 +01002968 return( 0 );
2969}
2970
2971void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2972{
Hanno Becker0271f962018-08-16 13:23:47 +01002973 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002974
Hanno Becker0271f962018-08-16 13:23:47 +01002975 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002976 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002977 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002978 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002979
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002980 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002983 ssl->handshake != NULL )
2984 {
Hanno Becker0271f962018-08-16 13:23:47 +01002985 unsigned offset;
2986 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002987
Hanno Becker0271f962018-08-16 13:23:47 +01002988 /* Increment handshake sequence number */
2989 hs->in_msg_seq++;
2990
2991 /*
2992 * Clear up handshake buffering and reassembly structure.
2993 */
2994
2995 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002996 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002997
2998 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002999 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3000 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003001 offset++, hs_buf++ )
3002 {
3003 *hs_buf = *(hs_buf + 1);
3004 }
3005
3006 /* Create a fresh last entry */
3007 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003008 }
3009#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003010}
3011
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003012/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003013 * DTLS anti-replay: RFC 6347 4.1.2.6
3014 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003015 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3016 * Bit n is set iff record number in_window_top - n has been seen.
3017 *
3018 * Usually, in_window_top is the last record number seen and the lsb of
3019 * in_window is set. The only exception is the initial state (record number 0
3020 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003023void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003024{
3025 ssl->in_window_top = 0;
3026 ssl->in_window = 0;
3027}
3028
3029static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3030{
3031 return( ( (uint64_t) buf[0] << 40 ) |
3032 ( (uint64_t) buf[1] << 32 ) |
3033 ( (uint64_t) buf[2] << 24 ) |
3034 ( (uint64_t) buf[3] << 16 ) |
3035 ( (uint64_t) buf[4] << 8 ) |
3036 ( (uint64_t) buf[5] ) );
3037}
3038
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003039static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3040{
Janos Follath865b3eb2019-12-16 11:46:15 +00003041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003042 unsigned char *original_in_ctr;
3043
3044 // save original in_ctr
3045 original_in_ctr = ssl->in_ctr;
3046
3047 // use counter from record
3048 ssl->in_ctr = record_in_ctr;
3049
3050 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3051
3052 // restore the counter
3053 ssl->in_ctr = original_in_ctr;
3054
3055 return ret;
3056}
3057
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003058/*
3059 * Return 0 if sequence number is acceptable, -1 otherwise
3060 */
Hanno Becker0183d692019-07-12 08:50:37 +01003061int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003062{
3063 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3064 uint64_t bit;
3065
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003066 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003067 return( 0 );
3068
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003069 if( rec_seqnum > ssl->in_window_top )
3070 return( 0 );
3071
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003072 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003073
3074 if( bit >= 64 )
3075 return( -1 );
3076
3077 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3078 return( -1 );
3079
3080 return( 0 );
3081}
3082
3083/*
3084 * Update replay window on new validated record
3085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003087{
3088 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3089
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003090 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003091 return;
3092
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003093 if( rec_seqnum > ssl->in_window_top )
3094 {
3095 /* Update window_top and the contents of the window */
3096 uint64_t shift = rec_seqnum - ssl->in_window_top;
3097
3098 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003099 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003100 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003101 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003102 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003103 ssl->in_window |= 1;
3104 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003105
3106 ssl->in_window_top = rec_seqnum;
3107 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003108 else
3109 {
3110 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003111 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003112
3113 if( bit < 64 ) /* Always true, but be extra sure */
3114 ssl->in_window |= (uint64_t) 1 << bit;
3115 }
3116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003118
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003119#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003120/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003121 * Without any SSL context, check if a datagram looks like a ClientHello with
3122 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003123 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003124 *
3125 * - if cookie is valid, return 0
3126 * - if ClientHello looks superficially valid but cookie is not,
3127 * fill obuf and set olen, then
3128 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3129 * - otherwise return a specific error code
3130 */
3131static int ssl_check_dtls_clihlo_cookie(
3132 mbedtls_ssl_cookie_write_t *f_cookie_write,
3133 mbedtls_ssl_cookie_check_t *f_cookie_check,
3134 void *p_cookie,
3135 const unsigned char *cli_id, size_t cli_id_len,
3136 const unsigned char *in, size_t in_len,
3137 unsigned char *obuf, size_t buf_len, size_t *olen )
3138{
3139 size_t sid_len, cookie_len;
3140 unsigned char *p;
3141
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003142 /*
3143 * Structure of ClientHello with record and handshake headers,
3144 * and expected values. We don't need to check a lot, more checks will be
3145 * done when actually parsing the ClientHello - skipping those checks
3146 * avoids code duplication and does not make cookie forging any easier.
3147 *
3148 * 0-0 ContentType type; copied, must be handshake
3149 * 1-2 ProtocolVersion version; copied
3150 * 3-4 uint16 epoch; copied, must be 0
3151 * 5-10 uint48 sequence_number; copied
3152 * 11-12 uint16 length; (ignored)
3153 *
3154 * 13-13 HandshakeType msg_type; (ignored)
3155 * 14-16 uint24 length; (ignored)
3156 * 17-18 uint16 message_seq; copied
3157 * 19-21 uint24 fragment_offset; copied, must be 0
3158 * 22-24 uint24 fragment_length; (ignored)
3159 *
3160 * 25-26 ProtocolVersion client_version; (ignored)
3161 * 27-58 Random random; (ignored)
3162 * 59-xx SessionID session_id; 1 byte len + sid_len content
3163 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3164 * ...
3165 *
3166 * Minimum length is 61 bytes.
3167 */
3168 if( in_len < 61 ||
3169 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3170 in[3] != 0 || in[4] != 0 ||
3171 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3172 {
3173 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3174 }
3175
3176 sid_len = in[59];
3177 if( sid_len > in_len - 61 )
3178 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3179
3180 cookie_len = in[60 + sid_len];
3181 if( cookie_len > in_len - 60 )
3182 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3183
3184 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3185 cli_id, cli_id_len ) == 0 )
3186 {
3187 /* Valid cookie */
3188 return( 0 );
3189 }
3190
3191 /*
3192 * If we get here, we've got an invalid cookie, let's prepare HVR.
3193 *
3194 * 0-0 ContentType type; copied
3195 * 1-2 ProtocolVersion version; copied
3196 * 3-4 uint16 epoch; copied
3197 * 5-10 uint48 sequence_number; copied
3198 * 11-12 uint16 length; olen - 13
3199 *
3200 * 13-13 HandshakeType msg_type; hello_verify_request
3201 * 14-16 uint24 length; olen - 25
3202 * 17-18 uint16 message_seq; copied
3203 * 19-21 uint24 fragment_offset; copied
3204 * 22-24 uint24 fragment_length; olen - 25
3205 *
3206 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3207 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3208 *
3209 * Minimum length is 28.
3210 */
3211 if( buf_len < 28 )
3212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3213
3214 /* Copy most fields and adapt others */
3215 memcpy( obuf, in, 25 );
3216 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3217 obuf[25] = 0xfe;
3218 obuf[26] = 0xff;
3219
3220 /* Generate and write actual cookie */
3221 p = obuf + 28;
3222 if( f_cookie_write( p_cookie,
3223 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3224 {
3225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3226 }
3227
3228 *olen = p - obuf;
3229
3230 /* Go back and fill length fields */
3231 obuf[27] = (unsigned char)( *olen - 28 );
3232
3233 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3234 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3235 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3236
3237 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3238 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3239
3240 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3241}
3242
3243/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003244 * Handle possible client reconnect with the same UDP quadruplet
3245 * (RFC 6347 Section 4.2.8).
3246 *
3247 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3248 * that looks like a ClientHello.
3249 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003250 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003251 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003252 * - if the input looks like a ClientHello with a valid cookie,
3253 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003254 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003255 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003256 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003257 * This function is called (through ssl_check_client_reconnect()) when an
3258 * unexpected record is found in ssl_get_next_record(), which will discard the
3259 * record if we return 0, and bubble up the return value otherwise (this
3260 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3261 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003262 */
3263static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3264{
Janos Follath865b3eb2019-12-16 11:46:15 +00003265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003266 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003267
Hanno Becker2fddd372019-07-10 14:37:41 +01003268 if( ssl->conf->f_cookie_write == NULL ||
3269 ssl->conf->f_cookie_check == NULL )
3270 {
3271 /* If we can't use cookies to verify reachability of the peer,
3272 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3274 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003275 return( 0 );
3276 }
3277
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003278 ret = ssl_check_dtls_clihlo_cookie(
3279 ssl->conf->f_cookie_write,
3280 ssl->conf->f_cookie_check,
3281 ssl->conf->p_cookie,
3282 ssl->cli_id, ssl->cli_id_len,
3283 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003284 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003285
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003286 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3287
3288 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003289 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003290 int send_ret;
3291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3292 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3293 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003294 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003295 * If the error is permanent we'll catch it later,
3296 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003297 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3298 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3299 (void) send_ret;
3300
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003301 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003302 }
3303
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003304 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003305 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003307 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003308 {
3309 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3310 return( ret );
3311 }
3312
3313 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003314 }
3315
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003316 return( ret );
3317}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003318#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003319
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003320static int ssl_check_record_type( uint8_t record_type )
3321{
3322 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3323 record_type != MBEDTLS_SSL_MSG_ALERT &&
3324 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3325 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3326 {
3327 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3328 }
3329
3330 return( 0 );
3331}
3332
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003333/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003334 * ContentType type;
3335 * ProtocolVersion version;
3336 * uint16 epoch; // DTLS only
3337 * uint48 sequence_number; // DTLS only
3338 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003339 *
3340 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003341 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003342 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3343 *
3344 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003345 * 1. proceed with the record if this function returns 0
3346 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3347 * 3. return CLIENT_RECONNECT if this function return that value
3348 * 4. drop the whole datagram if this function returns anything else.
3349 * Point 2 is needed when the peer is resending, and we have already received
3350 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003351 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003352static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003353 unsigned char *buf,
3354 size_t len,
3355 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003356{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003357 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
Hanno Beckere5e7e782019-07-11 12:29:35 +01003359 size_t const rec_hdr_type_offset = 0;
3360 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003361
Hanno Beckere5e7e782019-07-11 12:29:35 +01003362 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3363 rec_hdr_type_len;
3364 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Hanno Beckere5e7e782019-07-11 12:29:35 +01003366 size_t const rec_hdr_ctr_len = 8;
3367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003368 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003369 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3370 rec_hdr_version_len;
3371
Hanno Beckera0e20d02019-05-15 14:03:01 +01003372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003373 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3374 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003375 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003376#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3377#endif /* MBEDTLS_SSL_PROTO_DTLS */
3378
3379 size_t rec_hdr_len_offset; /* To be determined */
3380 size_t const rec_hdr_len_len = 2;
3381
3382 /*
3383 * Check minimum lengths for record header.
3384 */
3385
3386#if defined(MBEDTLS_SSL_PROTO_DTLS)
3387 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3388 {
3389 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3390 }
3391 else
3392#endif /* MBEDTLS_SSL_PROTO_DTLS */
3393 {
3394 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3395 }
3396
3397 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3398 {
3399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3400 (unsigned) len,
3401 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3402 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3403 }
3404
3405 /*
3406 * Parse and validate record content type
3407 */
3408
3409 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003410
3411 /* Check record content type */
3412#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3413 rec->cid_len = 0;
3414
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003416 ssl->conf->cid_len != 0 &&
3417 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003418 {
3419 /* Shift pointers to account for record header including CID
3420 * struct {
3421 * ContentType special_type = tls12_cid;
3422 * ProtocolVersion version;
3423 * uint16 epoch;
3424 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003425 * opaque cid[cid_length]; // Additional field compared to
3426 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003427 * uint16 length;
3428 * opaque enc_content[DTLSCiphertext.length];
3429 * } DTLSCiphertext;
3430 */
3431
3432 /* So far, we only support static CID lengths
3433 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003434 rec_hdr_cid_len = ssl->conf->cid_len;
3435 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003436
Hanno Beckere5e7e782019-07-11 12:29:35 +01003437 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003438 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3440 (unsigned) len,
3441 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003443 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003444
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003445 /* configured CID len is guaranteed at most 255, see
3446 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3447 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003448 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003449 }
3450 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003452 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003453 if( ssl_check_record_type( rec->type ) )
3454 {
Hanno Becker54229812019-07-12 14:40:00 +01003455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3456 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003457 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3458 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003459 }
3460
Hanno Beckere5e7e782019-07-11 12:29:35 +01003461 /*
3462 * Parse and validate record version
3463 */
3464
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003465 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3466 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003467 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3468 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003469 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003470
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003471 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3474 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 }
3476
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003477 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3480 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 }
3482
Hanno Beckere5e7e782019-07-11 12:29:35 +01003483 /*
3484 * Parse/Copy record sequence number.
3485 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003486
Hanno Beckere5e7e782019-07-11 12:29:35 +01003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
3488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003489 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003490 /* Copy explicit record sequence number from input buffer. */
3491 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3492 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003493 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003494 else
3495#endif /* MBEDTLS_SSL_PROTO_DTLS */
3496 {
3497 /* Copy implicit record sequence number from SSL context structure. */
3498 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3499 }
Paul Bakker40e46942009-01-03 21:51:57 +00003500
Hanno Beckere5e7e782019-07-11 12:29:35 +01003501 /*
3502 * Parse record length.
3503 */
3504
Hanno Beckere5e7e782019-07-11 12:29:35 +01003505 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003506 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3507 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003508 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003511 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003512 rec->type,
3513 major_ver, minor_ver, rec->data_len ) );
3514
3515 rec->buf = buf;
3516 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003517
Hanno Beckerd417cc92019-07-26 08:20:27 +01003518 if( rec->data_len == 0 )
3519 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003521 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003522 * DTLS-related tests.
3523 * Check epoch before checking length constraint because
3524 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3525 * message gets duplicated before the corresponding Finished message,
3526 * the second ChangeCipherSpec should be discarded because it belongs
3527 * to an old epoch, but not because its length is shorter than
3528 * the minimum record length for packets using the new record transform.
3529 * Note that these two kinds of failures are handled differently,
3530 * as an unexpected record is silently skipped but an invalid
3531 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003532 */
3533#if defined(MBEDTLS_SSL_PROTO_DTLS)
3534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3535 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003536 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003537
Hanno Becker955a5c92019-07-10 17:12:07 +01003538 /* Check that the datagram is large enough to contain a record
3539 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003540 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003541 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3543 (unsigned) len,
3544 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3546 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003547
Hanno Becker37cfe732019-07-10 17:20:01 +01003548 /* Records from other, non-matching epochs are silently discarded.
3549 * (The case of same-port Client reconnects must be considered in
3550 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003551 if( rec_epoch != ssl->in_epoch )
3552 {
3553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3554 "expected %d, received %d",
3555 ssl->in_epoch, rec_epoch ) );
3556
Hanno Becker552f7472019-07-19 10:59:12 +01003557 /* Records from the next epoch are considered for buffering
3558 * (concretely: early Finished messages). */
3559 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003560 {
Hanno Becker552f7472019-07-19 10:59:12 +01003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3562 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003563 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003564
Hanno Becker2fddd372019-07-10 14:37:41 +01003565 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003566 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003567#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003568 /* For records from the correct epoch, check whether their
3569 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003570 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3571 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003572 {
3573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3574 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3575 }
3576#endif
3577 }
3578#endif /* MBEDTLS_SSL_PROTO_DTLS */
3579
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003580 return( 0 );
3581}
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Hanno Becker2fddd372019-07-10 14:37:41 +01003584#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3585static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3586{
3587 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3588
3589 /*
3590 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3591 * access the first byte of record content (handshake type), as we
3592 * have an active transform (possibly iv_len != 0), so use the
3593 * fact that the record header len is 13 instead.
3594 */
3595 if( rec_epoch == 0 &&
3596 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3597 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3598 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3599 ssl->in_left > 13 &&
3600 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3601 {
3602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3603 "from the same port" ) );
3604 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 }
3606
3607 return( 0 );
3608}
Hanno Becker2fddd372019-07-10 14:37:41 +01003609#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003611/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003612 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003613 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003614static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3615 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003616{
3617 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003620 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3623 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ret = mbedtls_ssl_hw_record_read( ssl );
3628 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3631 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003632 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003633
3634 if( ret == 0 )
3635 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003638 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003640 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003641
Hanno Beckera18d1322018-01-03 14:27:32 +00003642 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003643 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003646
Hanno Beckera0e20d02019-05-15 14:03:01 +01003647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003648 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3649 ssl->conf->ignore_unexpected_cid
3650 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3651 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003653 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003654 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003655#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003656
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 return( ret );
3658 }
3659
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003660 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003661 {
3662 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003663 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003664 }
3665
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003666 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003667 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003668
Hanno Beckera0e20d02019-05-15 14:03:01 +01003669#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003670 /* We have already checked the record content type
3671 * in ssl_parse_record_header(), failing or silently
3672 * dropping the record in the case of an unknown type.
3673 *
3674 * Since with the use of CIDs, the record content type
3675 * might change during decryption, re-check the record
3676 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003677 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003678 {
3679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3681 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003682#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003683
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003684 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003685 {
3686#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3687 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003688 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003689 {
3690 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3693 }
3694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3695
3696 ssl->nb_zero++;
3697
3698 /*
3699 * Three or more empty messages may be a DoS attack
3700 * (excessive CPU consumption).
3701 */
3702 if( ssl->nb_zero > 3 )
3703 {
3704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003705 "messages, possible DoS attack" ) );
3706 /* Treat the records as if they were not properly authenticated,
3707 * thereby failing the connection if we see more than allowed
3708 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003709 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3710 }
3711 }
3712 else
3713 ssl->nb_zero = 0;
3714
3715#if defined(MBEDTLS_SSL_PROTO_DTLS)
3716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3717 {
3718 ; /* in_ctr read from peer, not maintained internally */
3719 }
3720 else
3721#endif
3722 {
3723 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003724 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003725 if( ++ssl->in_ctr[i - 1] != 0 )
3726 break;
3727
3728 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003729 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003730 {
3731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3732 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3733 }
3734 }
3735
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 }
3737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003742 }
3743#endif
3744
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003745 /* Check actual (decrypted) record content length against
3746 * configured maximum. */
3747 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3751 }
3752
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003753 return( 0 );
3754}
3755
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003756/*
3757 * Read a record.
3758 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003759 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3760 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3761 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003762 */
Hanno Becker1097b342018-08-15 14:09:41 +01003763
3764/* Helper functions for mbedtls_ssl_read_record(). */
3765static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003766static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3767static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003768
Hanno Becker327c93b2018-08-15 13:56:18 +01003769int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003770 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003771{
Janos Follath865b3eb2019-12-16 11:46:15 +00003772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003775
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003776 if( ssl->keep_current_message == 0 )
3777 {
3778 do {
Simon Butcher99000142016-10-13 17:21:01 +01003779
Hanno Becker26994592018-08-15 14:14:59 +01003780 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003781 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003782 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003783
Hanno Beckere74d5562018-08-15 14:26:08 +01003784 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003785 {
Hanno Becker40f50842018-08-15 14:48:01 +01003786#if defined(MBEDTLS_SSL_PROTO_DTLS)
3787 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003788
Hanno Becker40f50842018-08-15 14:48:01 +01003789 /* We only check for buffered messages if the
3790 * current datagram is fully consumed. */
3791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003792 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003793 {
Hanno Becker40f50842018-08-15 14:48:01 +01003794 if( ssl_load_buffered_message( ssl ) == 0 )
3795 have_buffered = 1;
3796 }
3797
3798 if( have_buffered == 0 )
3799#endif /* MBEDTLS_SSL_PROTO_DTLS */
3800 {
3801 ret = ssl_get_next_record( ssl );
3802 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3803 continue;
3804
3805 if( ret != 0 )
3806 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003807 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003808 return( ret );
3809 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003810 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003811 }
3812
3813 ret = mbedtls_ssl_handle_message_type( ssl );
3814
Hanno Becker40f50842018-08-15 14:48:01 +01003815#if defined(MBEDTLS_SSL_PROTO_DTLS)
3816 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3817 {
3818 /* Buffer future message */
3819 ret = ssl_buffer_message( ssl );
3820 if( ret != 0 )
3821 return( ret );
3822
3823 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3824 }
3825#endif /* MBEDTLS_SSL_PROTO_DTLS */
3826
Hanno Becker90333da2017-10-10 11:27:13 +01003827 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3828 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003829
3830 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003831 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003832 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003833 return( ret );
3834 }
3835
Hanno Becker327c93b2018-08-15 13:56:18 +01003836 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003837 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003838 {
3839 mbedtls_ssl_update_handshake_status( ssl );
3840 }
Simon Butcher99000142016-10-13 17:21:01 +01003841 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003842 else
Simon Butcher99000142016-10-13 17:21:01 +01003843 {
Hanno Becker02f59072018-08-15 14:00:24 +01003844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003845 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003846 }
3847
3848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3849
3850 return( 0 );
3851}
3852
Hanno Becker40f50842018-08-15 14:48:01 +01003853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003854static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003855{
Hanno Becker40f50842018-08-15 14:48:01 +01003856 if( ssl->in_left > ssl->next_record_offset )
3857 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003858
Hanno Becker40f50842018-08-15 14:48:01 +01003859 return( 0 );
3860}
3861
3862static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3863{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003864 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003865 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003866 int ret = 0;
3867
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003868 if( hs == NULL )
3869 return( -1 );
3870
Hanno Beckere00ae372018-08-20 09:39:42 +01003871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3872
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003873 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3874 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3875 {
3876 /* Check if we have seen a ChangeCipherSpec before.
3877 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003878 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003879 {
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3881 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003882 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003883 }
3884
Hanno Becker39b8bc92018-08-28 17:17:13 +01003885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003886 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3887 ssl->in_msglen = 1;
3888 ssl->in_msg[0] = 1;
3889
3890 /* As long as they are equal, the exact value doesn't matter. */
3891 ssl->in_left = 0;
3892 ssl->next_record_offset = 0;
3893
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003894 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003895 goto exit;
3896 }
Hanno Becker37f95322018-08-16 13:55:32 +01003897
Hanno Beckerb8f50142018-08-28 10:01:34 +01003898#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003899 /* Debug only */
3900 {
3901 unsigned offset;
3902 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3903 {
3904 hs_buf = &hs->buffering.hs[offset];
3905 if( hs_buf->is_valid == 1 )
3906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3908 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003909 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003910 }
3911 }
3912 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003913#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003914
3915 /* Check if we have buffered and/or fully reassembled the
3916 * next handshake message. */
3917 hs_buf = &hs->buffering.hs[0];
3918 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3919 {
3920 /* Synthesize a record containing the buffered HS message. */
3921 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3922 ( hs_buf->data[2] << 8 ) |
3923 hs_buf->data[3];
3924
3925 /* Double-check that we haven't accidentally buffered
3926 * a message that doesn't fit into the input buffer. */
3927 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3928 {
3929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932
3933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3934 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3935 hs_buf->data, msg_len + 12 );
3936
3937 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3938 ssl->in_hslen = msg_len + 12;
3939 ssl->in_msglen = msg_len + 12;
3940 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3941
3942 ret = 0;
3943 goto exit;
3944 }
3945 else
3946 {
3947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3948 hs->in_msg_seq ) );
3949 }
3950
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003951 ret = -1;
3952
3953exit:
3954
3955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3956 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003957}
3958
Hanno Beckera02b0b42018-08-21 17:20:27 +01003959static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3960 size_t desired )
3961{
3962 int offset;
3963 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3965 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003966
Hanno Becker01315ea2018-08-21 17:22:17 +01003967 /* Get rid of future records epoch first, if such exist. */
3968 ssl_free_buffered_record( ssl );
3969
3970 /* Check if we have enough space available now. */
3971 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3972 hs->buffering.total_bytes_buffered ) )
3973 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003975 return( 0 );
3976 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003977
Hanno Becker4f432ad2018-08-28 10:02:32 +01003978 /* We don't have enough space to buffer the next expected handshake
3979 * message. Remove buffers used for future messages to gain space,
3980 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003981 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3982 offset >= 0; offset-- )
3983 {
3984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3985 offset ) );
3986
Hanno Beckerb309b922018-08-23 13:18:05 +01003987 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003988
3989 /* Check if we have enough space available now. */
3990 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3991 hs->buffering.total_bytes_buffered ) )
3992 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003994 return( 0 );
3995 }
3996 }
3997
3998 return( -1 );
3999}
4000
Hanno Becker40f50842018-08-15 14:48:01 +01004001static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4002{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004003 int ret = 0;
4004 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4005
4006 if( hs == NULL )
4007 return( 0 );
4008
4009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4010
4011 switch( ssl->in_msgtype )
4012 {
4013 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004015
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004016 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004017 break;
4018
4019 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004020 {
4021 unsigned recv_msg_seq_offset;
4022 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4023 mbedtls_ssl_hs_buffer *hs_buf;
4024 size_t msg_len = ssl->in_hslen - 12;
4025
4026 /* We should never receive an old handshake
4027 * message - double-check nonetheless. */
4028 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4029 {
4030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4032 }
4033
4034 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4035 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4036 {
4037 /* Silently ignore -- message too far in the future */
4038 MBEDTLS_SSL_DEBUG_MSG( 2,
4039 ( "Ignore future HS message with sequence number %u, "
4040 "buffering window %u - %u",
4041 recv_msg_seq, ssl->handshake->in_msg_seq,
4042 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4043
4044 goto exit;
4045 }
4046
4047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4048 recv_msg_seq, recv_msg_seq_offset ) );
4049
4050 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4051
4052 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004053 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004054 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004055 size_t reassembly_buf_sz;
4056
Hanno Becker37f95322018-08-16 13:55:32 +01004057 hs_buf->is_fragmented =
4058 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4059
4060 /* We copy the message back into the input buffer
4061 * after reassembly, so check that it's not too large.
4062 * This is an implementation-specific limitation
4063 * and not one from the standard, hence it is not
4064 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004065 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004066 {
4067 /* Ignore message */
4068 goto exit;
4069 }
4070
Hanno Beckere0b150f2018-08-21 15:51:03 +01004071 /* Check if we have enough space to buffer the message. */
4072 if( hs->buffering.total_bytes_buffered >
4073 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4074 {
4075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4077 }
4078
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004079 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4080 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004081
4082 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4083 hs->buffering.total_bytes_buffered ) )
4084 {
4085 if( recv_msg_seq_offset > 0 )
4086 {
4087 /* If we can't buffer a future message because
4088 * of space limitations -- ignore. */
4089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4090 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4091 (unsigned) hs->buffering.total_bytes_buffered ) );
4092 goto exit;
4093 }
Hanno Beckere1801392018-08-21 16:51:05 +01004094 else
4095 {
4096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4097 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4098 (unsigned) hs->buffering.total_bytes_buffered ) );
4099 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004100
Hanno Beckera02b0b42018-08-21 17:20:27 +01004101 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004102 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
4104 (unsigned) msg_len,
4105 (unsigned) reassembly_buf_sz,
4106 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004107 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004108 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4109 goto exit;
4110 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004111 }
4112
4113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4114 msg_len ) );
4115
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004116 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4117 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004118 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004119 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004120 goto exit;
4121 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004122 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004123
4124 /* Prepare final header: copy msg_type, length and message_seq,
4125 * then add standardised fragment_offset and fragment_length */
4126 memcpy( hs_buf->data, ssl->in_msg, 6 );
4127 memset( hs_buf->data + 6, 0, 3 );
4128 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4129
4130 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004131
4132 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004133 }
4134 else
4135 {
4136 /* Make sure msg_type and length are consistent */
4137 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4138 {
4139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4140 /* Ignore */
4141 goto exit;
4142 }
4143 }
4144
Hanno Becker4422bbb2018-08-20 09:40:19 +01004145 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004146 {
4147 size_t frag_len, frag_off;
4148 unsigned char * const msg = hs_buf->data + 12;
4149
4150 /*
4151 * Check and copy current fragment
4152 */
4153
4154 /* Validation of header fields already done in
4155 * mbedtls_ssl_prepare_handshake_record(). */
4156 frag_off = ssl_get_hs_frag_off( ssl );
4157 frag_len = ssl_get_hs_frag_len( ssl );
4158
4159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4160 frag_off, frag_len ) );
4161 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4162
4163 if( hs_buf->is_fragmented )
4164 {
4165 unsigned char * const bitmask = msg + msg_len;
4166 ssl_bitmask_set( bitmask, frag_off, frag_len );
4167 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4168 msg_len ) == 0 );
4169 }
4170 else
4171 {
4172 hs_buf->is_complete = 1;
4173 }
4174
4175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4176 hs_buf->is_complete ? "" : "not yet " ) );
4177 }
4178
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004179 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004180 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004181
4182 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004183 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004184 break;
4185 }
4186
4187exit:
4188
4189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4190 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004191}
4192#endif /* MBEDTLS_SSL_PROTO_DTLS */
4193
Hanno Becker1097b342018-08-15 14:09:41 +01004194static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004195{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004196 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004197 * Consume last content-layer message and potentially
4198 * update in_msglen which keeps track of the contents'
4199 * consumption state.
4200 *
4201 * (1) Handshake messages:
4202 * Remove last handshake message, move content
4203 * and adapt in_msglen.
4204 *
4205 * (2) Alert messages:
4206 * Consume whole record content, in_msglen = 0.
4207 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004208 * (3) Change cipher spec:
4209 * Consume whole record content, in_msglen = 0.
4210 *
4211 * (4) Application data:
4212 * Don't do anything - the record layer provides
4213 * the application data as a stream transport
4214 * and consumes through mbedtls_ssl_read only.
4215 *
4216 */
4217
4218 /* Case (1): Handshake messages */
4219 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004220 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004221 /* Hard assertion to be sure that no application data
4222 * is in flight, as corrupting ssl->in_msglen during
4223 * ssl->in_offt != NULL is fatal. */
4224 if( ssl->in_offt != NULL )
4225 {
4226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4228 }
4229
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004230 /*
4231 * Get next Handshake message in the current record
4232 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004233
Hanno Becker4a810fb2017-05-24 16:27:30 +01004234 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004235 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004236 * current handshake content: If DTLS handshake
4237 * fragmentation is used, that's the fragment
4238 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004239 * size here is faulty and should be changed at
4240 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004241 * (2) While it doesn't seem to cause problems, one
4242 * has to be very careful not to assume that in_hslen
4243 * is always <= in_msglen in a sensible communication.
4244 * Again, it's wrong for DTLS handshake fragmentation.
4245 * The following check is therefore mandatory, and
4246 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004247 * Additionally, ssl->in_hslen might be arbitrarily out of
4248 * bounds after handling a DTLS message with an unexpected
4249 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004250 */
4251 if( ssl->in_hslen < ssl->in_msglen )
4252 {
4253 ssl->in_msglen -= ssl->in_hslen;
4254 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4255 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004256
Hanno Becker4a810fb2017-05-24 16:27:30 +01004257 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4258 ssl->in_msg, ssl->in_msglen );
4259 }
4260 else
4261 {
4262 ssl->in_msglen = 0;
4263 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004264
Hanno Becker4a810fb2017-05-24 16:27:30 +01004265 ssl->in_hslen = 0;
4266 }
4267 /* Case (4): Application data */
4268 else if( ssl->in_offt != NULL )
4269 {
4270 return( 0 );
4271 }
4272 /* Everything else (CCS & Alerts) */
4273 else
4274 {
4275 ssl->in_msglen = 0;
4276 }
4277
Hanno Becker1097b342018-08-15 14:09:41 +01004278 return( 0 );
4279}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004280
Hanno Beckere74d5562018-08-15 14:26:08 +01004281static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4282{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004283 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004284 return( 1 );
4285
4286 return( 0 );
4287}
4288
Hanno Becker5f066e72018-08-16 14:56:31 +01004289#if defined(MBEDTLS_SSL_PROTO_DTLS)
4290
4291static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4292{
4293 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4294 if( hs == NULL )
4295 return;
4296
Hanno Becker01315ea2018-08-21 17:22:17 +01004297 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004298 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004299 hs->buffering.total_bytes_buffered -=
4300 hs->buffering.future_record.len;
4301
4302 mbedtls_free( hs->buffering.future_record.data );
4303 hs->buffering.future_record.data = NULL;
4304 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004305}
4306
4307static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4308{
4309 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4310 unsigned char * rec;
4311 size_t rec_len;
4312 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004313#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4314 size_t in_buf_len = ssl->in_buf_len;
4315#else
4316 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4317#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004318 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4319 return( 0 );
4320
4321 if( hs == NULL )
4322 return( 0 );
4323
Hanno Becker5f066e72018-08-16 14:56:31 +01004324 rec = hs->buffering.future_record.data;
4325 rec_len = hs->buffering.future_record.len;
4326 rec_epoch = hs->buffering.future_record.epoch;
4327
4328 if( rec == NULL )
4329 return( 0 );
4330
Hanno Becker4cb782d2018-08-20 11:19:05 +01004331 /* Only consider loading future records if the
4332 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004333 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004334 return( 0 );
4335
Hanno Becker5f066e72018-08-16 14:56:31 +01004336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4337
4338 if( rec_epoch != ssl->in_epoch )
4339 {
4340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4341 goto exit;
4342 }
4343
4344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4345
4346 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004347 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004348 {
4349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4351 }
4352
4353 memcpy( ssl->in_hdr, rec, rec_len );
4354 ssl->in_left = rec_len;
4355 ssl->next_record_offset = 0;
4356
4357 ssl_free_buffered_record( ssl );
4358
4359exit:
4360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4361 return( 0 );
4362}
4363
Hanno Becker519f15d2019-07-11 12:43:20 +01004364static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4365 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004366{
4367 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004368
4369 /* Don't buffer future records outside handshakes. */
4370 if( hs == NULL )
4371 return( 0 );
4372
4373 /* Only buffer handshake records (we are only interested
4374 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004375 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004376 return( 0 );
4377
4378 /* Don't buffer more than one future epoch record. */
4379 if( hs->buffering.future_record.data != NULL )
4380 return( 0 );
4381
Hanno Becker01315ea2018-08-21 17:22:17 +01004382 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004383 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004384 hs->buffering.total_bytes_buffered ) )
4385 {
4386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Becker519f15d2019-07-11 12:43:20 +01004387 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004388 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004389 return( 0 );
4390 }
4391
Hanno Becker5f066e72018-08-16 14:56:31 +01004392 /* Buffer record */
4393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4394 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004395 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004396
4397 /* ssl_parse_record_header() only considers records
4398 * of the next epoch as candidates for buffering. */
4399 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004400 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004401
4402 hs->buffering.future_record.data =
4403 mbedtls_calloc( 1, hs->buffering.future_record.len );
4404 if( hs->buffering.future_record.data == NULL )
4405 {
4406 /* If we run out of RAM trying to buffer a
4407 * record from the next epoch, just ignore. */
4408 return( 0 );
4409 }
4410
Hanno Becker519f15d2019-07-11 12:43:20 +01004411 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004412
Hanno Becker519f15d2019-07-11 12:43:20 +01004413 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004414 return( 0 );
4415}
4416
4417#endif /* MBEDTLS_SSL_PROTO_DTLS */
4418
Hanno Beckere74d5562018-08-15 14:26:08 +01004419static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004420{
Janos Follath865b3eb2019-12-16 11:46:15 +00004421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004422 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004423
Hanno Becker5f066e72018-08-16 14:56:31 +01004424#if defined(MBEDTLS_SSL_PROTO_DTLS)
4425 /* We might have buffered a future record; if so,
4426 * and if the epoch matches now, load it.
4427 * On success, this call will set ssl->in_left to
4428 * the length of the buffered record, so that
4429 * the calls to ssl_fetch_input() below will
4430 * essentially be no-ops. */
4431 ret = ssl_load_buffered_record( ssl );
4432 if( ret != 0 )
4433 return( ret );
4434#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004435
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004436 /* Ensure that we have enough space available for the default form
4437 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4438 * with no space for CIDs counted in). */
4439 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4440 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004443 return( ret );
4444 }
4445
Hanno Beckere5e7e782019-07-11 12:29:35 +01004446 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4447 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004451 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004452 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4453 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004454 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004455 if( ret != 0 )
4456 return( ret );
4457
4458 /* Fall through to handling of unexpected records */
4459 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4460 }
4461
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004462 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4463 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004464#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004465 /* Reset in pointers to default state for TLS/DTLS records,
4466 * assuming no CID and no offset between record content and
4467 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004468 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004469
Hanno Becker7ae20e02019-07-12 08:33:49 +01004470 /* Setup internal message pointers from record structure. */
4471 ssl->in_msgtype = rec.type;
4472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4473 ssl->in_len = ssl->in_cid + rec.cid_len;
4474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4475 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4476 ssl->in_msglen = rec.data_len;
4477
Hanno Becker2fddd372019-07-10 14:37:41 +01004478 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004479 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004480 if( ret != 0 )
4481 return( ret );
4482#endif
4483
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004484 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004485 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004486
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4488 "(header)" ) );
4489 }
4490 else
4491 {
4492 /* Skip invalid record and the rest of the datagram */
4493 ssl->next_record_offset = 0;
4494 ssl->in_left = 0;
4495
4496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4497 "(header)" ) );
4498 }
4499
4500 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004501 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004502 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004503 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004504#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004505 {
4506 return( ret );
4507 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004508 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004512 {
Hanno Beckera8814792019-07-10 15:01:45 +01004513 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004514 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004515 if( ssl->next_record_offset < ssl->in_left )
4516 {
4517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4518 }
4519 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004520 else
4521#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004522 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004523 /*
4524 * Fetch record contents from underlying transport.
4525 */
Hanno Beckera3175662019-07-11 12:50:29 +01004526 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004527 if( ret != 0 )
4528 {
4529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4530 return( ret );
4531 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004532
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004534 }
4535
4536 /*
4537 * Decrypt record contents.
4538 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004539
Hanno Beckerfdf66042019-07-11 13:07:45 +01004540 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004544 {
4545 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004546 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004547 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004548 /* Except when waiting for Finished as a bad mac here
4549 * probably means something went wrong in the handshake
4550 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4551 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4552 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4553 {
4554#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4555 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4556 {
4557 mbedtls_ssl_send_alert_message( ssl,
4558 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4559 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4560 }
4561#endif
4562 return( ret );
4563 }
4564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004566 if( ssl->conf->badmac_limit != 0 &&
4567 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4570 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004571 }
4572#endif
4573
Hanno Becker4a810fb2017-05-24 16:27:30 +01004574 /* As above, invalid records cause
4575 * dismissal of the whole datagram. */
4576
4577 ssl->next_record_offset = 0;
4578 ssl->in_left = 0;
4579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004581 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004582 }
4583
4584 return( ret );
4585 }
4586 else
4587#endif
4588 {
4589 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4591 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 mbedtls_ssl_send_alert_message( ssl,
4594 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4595 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004596 }
4597#endif
4598 return( ret );
4599 }
4600 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601
Hanno Becker44d89b22019-07-12 09:40:44 +01004602
4603 /* Reset in pointers to default state for TLS/DTLS records,
4604 * assuming no CID and no offset between record content and
4605 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004606 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004607#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4608 ssl->in_len = ssl->in_cid + rec.cid_len;
4609#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004610 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004611
Hanno Becker8685c822019-07-12 09:37:30 +01004612 /* The record content type may change during decryption,
4613 * so re-read it. */
4614 ssl->in_msgtype = rec.type;
4615 /* Also update the input buffer, because unfortunately
4616 * the server-side ssl_parse_client_hello() reparses the
4617 * record header when receiving a ClientHello initiating
4618 * a renegotiation. */
4619 ssl->in_hdr[0] = rec.type;
4620 ssl->in_msg = rec.buf + rec.data_offset;
4621 ssl->in_msglen = rec.data_len;
4622 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4623 ssl->in_len[1] = (unsigned char)( rec.data_len );
4624
Simon Butcher99000142016-10-13 17:21:01 +01004625 return( 0 );
4626}
4627
4628int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4629{
Janos Follath865b3eb2019-12-16 11:46:15 +00004630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004631
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004632 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633 * Handle particular types of records
4634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
Simon Butcher99000142016-10-13 17:21:01 +01004637 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4638 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004639 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 }
4642
Hanno Beckere678eaa2018-08-21 14:57:46 +01004643 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004644 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004645 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004646 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4648 ssl->in_msglen ) );
4649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004650 }
4651
Hanno Beckere678eaa2018-08-21 14:57:46 +01004652 if( ssl->in_msg[0] != 1 )
4653 {
4654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4655 ssl->in_msg[0] ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4657 }
4658
4659#if defined(MBEDTLS_SSL_PROTO_DTLS)
4660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4661 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4662 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4663 {
4664 if( ssl->handshake == NULL )
4665 {
4666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4667 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4668 }
4669
4670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4671 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4672 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004673#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004674 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004678 if( ssl->in_msglen != 2 )
4679 {
4680 /* Note: Standard allows for more than one 2 byte alert
4681 to be packed in a single message, but Mbed TLS doesn't
4682 currently support this. */
4683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4684 ssl->in_msglen ) );
4685 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4686 }
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 ssl->in_msg[0], ssl->in_msg[1] ) );
4690
4691 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004692 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004697 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 }
4700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4702 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4705 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004707
4708#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4709 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4710 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4711 {
Hanno Becker90333da2017-10-10 11:27:13 +01004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004713 /* Will be handled when trying to parse ServerHello */
4714 return( 0 );
4715 }
4716#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004717 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004718 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 }
4720
Hanno Beckerc76c6192017-06-06 10:03:17 +01004721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004723 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004724 /* Drop unexpected ApplicationData records,
4725 * except at the beginning of renegotiations */
4726 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4727 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4728#if defined(MBEDTLS_SSL_RENEGOTIATION)
4729 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4730 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004731#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004732 )
4733 {
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4735 return( MBEDTLS_ERR_SSL_NON_FATAL );
4736 }
4737
4738 if( ssl->handshake != NULL &&
4739 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4740 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004741 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004742 }
4743 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004744#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004745
Paul Bakker5121ce52009-01-03 21:22:43 +00004746 return( 0 );
4747}
4748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004750{
irwir6c0da642019-09-26 21:07:41 +03004751 return( mbedtls_ssl_send_alert_message( ssl,
4752 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4753 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004754}
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004757 unsigned char level,
4758 unsigned char message )
4759{
Janos Follath865b3eb2019-12-16 11:46:15 +00004760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004761
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004762 if( ssl == NULL || ssl->conf == NULL )
4763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004769 ssl->out_msglen = 2;
4770 ssl->out_msg[0] = level;
4771 ssl->out_msg[1] = message;
4772
Hanno Becker67bc7c32018-08-06 11:33:50 +01004773 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004776 return( ret );
4777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004779
4780 return( 0 );
4781}
4782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004784{
Janos Follath865b3eb2019-12-16 11:46:15 +00004785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 ssl->out_msglen = 1;
4791 ssl->out_msg[0] = 1;
4792
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 ssl->state++;
4794
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004795 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004798 return( ret );
4799 }
4800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
4803 return( 0 );
4804}
4805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004807{
Janos Follath865b3eb2019-12-16 11:46:15 +00004808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Hanno Becker327c93b2018-08-15 13:56:18 +01004812 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 return( ret );
4816 }
4817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004821 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4822 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 }
4825
Hanno Beckere678eaa2018-08-21 14:57:46 +01004826 /* CCS records are only accepted if they have length 1 and content '1',
4827 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004828
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004829 /*
4830 * Switch to our negotiated transform and session parameters for inbound
4831 * data.
4832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004834 ssl->transform_in = ssl->transform_negotiate;
4835 ssl->session_in = ssl->session_negotiate;
4836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004838 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004841 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004842#endif
4843
4844 /* Increment epoch */
4845 if( ++ssl->in_epoch == 0 )
4846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004848 /* This is highly unlikely to happen for legitimate reasons, so
4849 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004851 }
4852 }
4853 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004855 memset( ssl->in_ctr, 0, 8 );
4856
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004857 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4860 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4866 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004868 }
4869 }
4870#endif
4871
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 ssl->state++;
4873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004875
4876 return( 0 );
4877}
4878
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004879/* Once ssl->out_hdr as the address of the beginning of the
4880 * next outgoing record is set, deduce the other pointers.
4881 *
4882 * Note: For TLS, we save the implicit record sequence number
4883 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4884 * and the caller has to make sure there's space for this.
4885 */
4886
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004887static size_t ssl_transform_get_explicit_iv_len(
4888 mbedtls_ssl_transform const *transform )
4889{
4890 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
4891 return( 0 );
4892
4893 return( transform->ivlen - transform->fixed_ivlen );
4894}
4895
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004896void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4897 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004898{
4899#if defined(MBEDTLS_SSL_PROTO_DTLS)
4900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4901 {
4902 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004904 ssl->out_cid = ssl->out_ctr + 8;
4905 ssl->out_len = ssl->out_cid;
4906 if( transform != NULL )
4907 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004908#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004909 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004911 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004912 }
4913 else
4914#endif
4915 {
4916 ssl->out_ctr = ssl->out_hdr - 8;
4917 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004918#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004919 ssl->out_cid = ssl->out_len;
4920#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004921 ssl->out_iv = ssl->out_hdr + 5;
4922 }
4923
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004924 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004925 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004926 if( transform != NULL )
4927 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004928}
4929
4930/* Once ssl->in_hdr as the address of the beginning of the
4931 * next incoming record is set, deduce the other pointers.
4932 *
4933 * Note: For TLS, we save the implicit record sequence number
4934 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4935 * and the caller has to make sure there's space for this.
4936 */
4937
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004938void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004939{
Hanno Becker79594fd2019-05-08 09:38:41 +01004940 /* This function sets the pointers to match the case
4941 * of unprotected TLS/DTLS records, with both ssl->in_iv
4942 * and ssl->in_msg pointing to the beginning of the record
4943 * content.
4944 *
4945 * When decrypting a protected record, ssl->in_msg
4946 * will be shifted to point to the beginning of the
4947 * record plaintext.
4948 */
4949
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004950#if defined(MBEDTLS_SSL_PROTO_DTLS)
4951 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4952 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004953 /* This sets the header pointers to match records
4954 * without CID. When we receive a record containing
4955 * a CID, the fields are shifted accordingly in
4956 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004957 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004958#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004959 ssl->in_cid = ssl->in_ctr + 8;
4960 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004961#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004962 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004963#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004964 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004965 }
4966 else
4967#endif
4968 {
4969 ssl->in_ctr = ssl->in_hdr - 8;
4970 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004971#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004972 ssl->in_cid = ssl->in_len;
4973#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004974 ssl->in_iv = ssl->in_hdr + 5;
4975 }
4976
Hanno Becker79594fd2019-05-08 09:38:41 +01004977 /* This will be adjusted at record decryption time. */
4978 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004979}
4980
Paul Bakker5121ce52009-01-03 21:22:43 +00004981/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004982 * Setup an SSL context
4983 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004984
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004985void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004986{
4987 /* Set the incoming and outgoing record pointers. */
4988#if defined(MBEDTLS_SSL_PROTO_DTLS)
4989 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4990 {
4991 ssl->out_hdr = ssl->out_buf;
4992 ssl->in_hdr = ssl->in_buf;
4993 }
4994 else
4995#endif /* MBEDTLS_SSL_PROTO_DTLS */
4996 {
4997 ssl->out_hdr = ssl->out_buf + 8;
4998 ssl->in_hdr = ssl->in_buf + 8;
4999 }
5000
5001 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005002 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5003 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005004}
5005
Paul Bakker5121ce52009-01-03 21:22:43 +00005006/*
5007 * SSL get accessors
5008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005010{
5011 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5012}
5013
Hanno Becker8b170a02017-10-10 11:51:19 +01005014int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5015{
5016 /*
5017 * Case A: We're currently holding back
5018 * a message for further processing.
5019 */
5020
5021 if( ssl->keep_current_message == 1 )
5022 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005024 return( 1 );
5025 }
5026
5027 /*
5028 * Case B: Further records are pending in the current datagram.
5029 */
5030
5031#if defined(MBEDTLS_SSL_PROTO_DTLS)
5032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5033 ssl->in_left > ssl->next_record_offset )
5034 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005036 return( 1 );
5037 }
5038#endif /* MBEDTLS_SSL_PROTO_DTLS */
5039
5040 /*
5041 * Case C: A handshake message is being processed.
5042 */
5043
Hanno Becker8b170a02017-10-10 11:51:19 +01005044 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5045 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005047 return( 1 );
5048 }
5049
5050 /*
5051 * Case D: An application data message is being processed
5052 */
5053 if( ssl->in_offt != NULL )
5054 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005056 return( 1 );
5057 }
5058
5059 /*
5060 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005061 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005062 * we implement support for multiple alerts in single records.
5063 */
5064
5065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5066 return( 0 );
5067}
5068
Paul Bakker43ca69c2011-01-15 17:35:19 +00005069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005071{
Hanno Becker3136ede2018-08-17 15:28:19 +01005072 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005074 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005075
Hanno Becker5903de42019-05-03 14:46:38 +01005076 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5077
Hanno Becker78640902018-08-13 16:35:15 +01005078 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005079 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083 case MBEDTLS_MODE_GCM:
5084 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005085 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005087 transform_expansion = transform->minlen;
5088 break;
5089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005091
5092 block_size = mbedtls_cipher_get_block_size(
5093 &transform->cipher_ctx_enc );
5094
Hanno Becker3136ede2018-08-17 15:28:19 +01005095 /* Expansion due to the addition of the MAC. */
5096 transform_expansion += transform->maclen;
5097
5098 /* Expansion due to the addition of CBC padding;
5099 * Theoretically up to 256 bytes, but we never use
5100 * more than the block size of the underlying cipher. */
5101 transform_expansion += block_size;
5102
5103 /* For TLS 1.1 or higher, an explicit IV is added
5104 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005105#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5106 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005107 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005108#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005109
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005110 break;
5111
5112 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005115 }
5116
Hanno Beckera0e20d02019-05-15 14:03:01 +01005117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005118 if( transform->out_cid_len != 0 )
5119 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005120#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005121
Hanno Becker5903de42019-05-03 14:46:38 +01005122 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005123}
5124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005126/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005127 * Check record counters and renegotiate if they're above the limit.
5128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005130{
Hanno Beckerdd772292020-02-05 10:38:31 +00005131 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005132 int in_ctr_cmp;
5133 int out_ctr_cmp;
5134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5136 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005137 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005138 {
5139 return( 0 );
5140 }
5141
Andres AG2196c7f2016-12-15 17:01:16 +00005142 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5143 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005144 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005145 ssl->conf->renego_period + ep_len, 8 - ep_len );
5146
5147 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005148 {
5149 return( 0 );
5150 }
5151
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005156
5157/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005158 * Receive application data decrypted from the SSL layer
5159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005161{
Janos Follath865b3eb2019-12-16 11:46:15 +00005162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005163 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005164
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005165 if( ssl == NULL || ssl->conf == NULL )
5166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005171 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005174 return( ret );
5175
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005176 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005178 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005179 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005180 return( ret );
5181 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005182 }
5183#endif
5184
Hanno Becker4a810fb2017-05-24 16:27:30 +01005185 /*
5186 * Check if renegotiation is necessary and/or handshake is
5187 * in process. If yes, perform/continue, and fall through
5188 * if an unexpected packet is received while the client
5189 * is waiting for the ServerHello.
5190 *
5191 * (There is no equivalent to the last condition on
5192 * the server-side as it is not treated as within
5193 * a handshake while waiting for the ClientHello
5194 * after a renegotiation request.)
5195 */
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005198 ret = ssl_check_ctr_renegotiate( ssl );
5199 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5200 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005203 return( ret );
5204 }
5205#endif
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005210 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5211 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 return( ret );
5215 }
5216 }
5217
Hanno Beckere41158b2017-10-23 13:30:32 +01005218 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005219 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005221 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005222 if( ssl->f_get_timer != NULL &&
5223 ssl->f_get_timer( ssl->p_timer ) == -1 )
5224 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005225 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005226 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005227
Hanno Becker327c93b2018-08-15 13:56:18 +01005228 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005230 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5231 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005232
Hanno Becker4a810fb2017-05-24 16:27:30 +01005233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5234 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005235 }
5236
5237 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005239 {
5240 /*
5241 * OpenSSL sends empty messages to randomize the IV
5242 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005243 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005246 return( 0 );
5247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 return( ret );
5250 }
5251 }
5252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005256
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 /*
5258 * - For client-side, expect SERVER_HELLO_REQUEST.
5259 * - For server-side, expect CLIENT_HELLO.
5260 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5261 */
5262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005266 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005269
5270 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005273 {
5274 continue;
5275 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005278 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005279#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005280
Hanno Becker4a810fb2017-05-24 16:27:30 +01005281#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005282 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005286
5287 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005289 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005290 {
5291 continue;
5292 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005295 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005296#endif /* MBEDTLS_SSL_SRV_C */
5297
Hanno Becker21df7f92017-10-17 11:03:26 +01005298#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005299 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005300 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5301 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5302 ssl->conf->allow_legacy_renegotiation ==
5303 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5304 {
5305 /*
5306 * Accept renegotiation request
5307 */
Paul Bakker48916f92012-09-16 19:57:18 +00005308
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005309 /* DTLS clients need to know renego is server-initiated */
5310#if defined(MBEDTLS_SSL_PROTO_DTLS)
5311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5312 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5313 {
5314 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5315 }
5316#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005317 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005318 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5319 ret != 0 )
5320 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5322 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005323 return( ret );
5324 }
5325 }
5326 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005327#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005328 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005329 /*
5330 * Refuse renegotiation
5331 */
5332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5336 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5337 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5340 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5341 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005342 {
5343 return( ret );
5344 }
Paul Bakker48916f92012-09-16 19:57:18 +00005345 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5348 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005352 }
Paul Bakker48916f92012-09-16 19:57:18 +00005353 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005354
Hanno Becker90333da2017-10-10 11:27:13 +01005355 /* At this point, we don't know whether the renegotiation has been
5356 * completed or not. The cases to consider are the following:
5357 * 1) The renegotiation is complete. In this case, no new record
5358 * has been read yet.
5359 * 2) The renegotiation is incomplete because the client received
5360 * an application data record while awaiting the ServerHello.
5361 * 3) The renegotiation is incomplete because the client received
5362 * a non-handshake, non-application data message while awaiting
5363 * the ServerHello.
5364 * In each of these case, looping will be the proper action:
5365 * - For 1), the next iteration will read a new record and check
5366 * if it's application data.
5367 * - For 2), the loop condition isn't satisfied as application data
5368 * is present, hence continue is the same as break
5369 * - For 3), the loop condition is satisfied and read_record
5370 * will re-deliver the message that was held back by the client
5371 * when expecting the ServerHello.
5372 */
5373 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005374 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005375#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005377 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005378 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005379 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005380 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005383 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005385 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005386 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5391 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005394 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005395 }
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5400 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 }
5402
5403 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005404
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005405 /* We're going to return something now, cancel timer,
5406 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005408 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005409
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005410#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005411 /* If we requested renego but received AppData, resend HelloRequest.
5412 * Do it now, after setting in_offt, to avoid taking this branch
5413 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005415 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005417 {
Hanno Becker786300f2020-02-05 10:46:40 +00005418 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005419 {
Hanno Becker786300f2020-02-05 10:46:40 +00005420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5421 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005422 return( ret );
5423 }
5424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005427 }
5428
5429 n = ( len < ssl->in_msglen )
5430 ? len : ssl->in_msglen;
5431
5432 memcpy( buf, ssl->in_offt, n );
5433 ssl->in_msglen -= n;
5434
gabor-mezei-arma3214132020-07-15 10:55:00 +02005435 /* Zeroising the plaintext buffer to erase unused application data
5436 from the memory. */
5437 mbedtls_platform_zeroize( ssl->in_offt, n );
5438
Paul Bakker5121ce52009-01-03 21:22:43 +00005439 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005440 {
5441 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005443 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 /* more data available */
5448 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005452
Paul Bakker23986e52011-04-24 08:57:21 +00005453 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005454}
5455
5456/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005457 * Send application data to be encrypted by the SSL layer, taking care of max
5458 * fragment length and buffer size.
5459 *
5460 * According to RFC 5246 Section 6.2.1:
5461 *
5462 * Zero-length fragments of Application data MAY be sent as they are
5463 * potentially useful as a traffic analysis countermeasure.
5464 *
5465 * Therefore, it is possible that the input message length is 0 and the
5466 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005468static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005469 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005471 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5472 const size_t max_len = (size_t) ret;
5473
5474 if( ret < 0 )
5475 {
5476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5477 return( ret );
5478 }
5479
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005480 if( len > max_len )
5481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005486 "maximum fragment length: %d > %d",
5487 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005489 }
5490 else
5491#endif
5492 len = max_len;
5493 }
Paul Bakker887bd502011-06-08 13:10:54 +00005494
Paul Bakker5121ce52009-01-03 21:22:43 +00005495 if( ssl->out_left != 0 )
5496 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005497 /*
5498 * The user has previously tried to send the data and
5499 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5500 * written. In this case, we expect the high-level write function
5501 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 return( ret );
5507 }
5508 }
Paul Bakker887bd502011-06-08 13:10:54 +00005509 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005510 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005511 /*
5512 * The user is trying to send a message the first time, so we need to
5513 * copy the data into the internal buffers and setup the data structure
5514 * to keep track of partial writes
5515 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005516 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005518 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005519
Hanno Becker67bc7c32018-08-06 11:33:50 +01005520 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005523 return( ret );
5524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 }
5526
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005527 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005528}
5529
5530/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005531 * Write application data, doing 1/n-1 splitting if necessary.
5532 *
5533 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005534 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005535 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005538static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005539 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005540{
Janos Follath865b3eb2019-12-16 11:46:15 +00005541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005542
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005543 if( ssl->conf->cbc_record_splitting ==
5544 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005545 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5547 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5548 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005549 {
5550 return( ssl_write_real( ssl, buf, len ) );
5551 }
5552
5553 if( ssl->split_done == 0 )
5554 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005555 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005556 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005557 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005558 }
5559
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005560 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5561 return( ret );
5562 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005563
5564 return( ret + 1 );
5565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005567
5568/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005569 * Write application data (public-facing wrapper)
5570 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005571int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005572{
Janos Follath865b3eb2019-12-16 11:46:15 +00005573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005574
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005576
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005577 if( ssl == NULL || ssl->conf == NULL )
5578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5579
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005580#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005581 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5582 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005584 return( ret );
5585 }
5586#endif
5587
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005588 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005589 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005590 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005591 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005593 return( ret );
5594 }
5595 }
5596
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005597#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005598 ret = ssl_write_split( ssl, buf, len );
5599#else
5600 ret = ssl_write_real( ssl, buf, len );
5601#endif
5602
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005604
5605 return( ret );
5606}
5607
5608/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 * Notify the peer that the connection is being closed
5610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005612{
Janos Follath865b3eb2019-12-16 11:46:15 +00005613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005614
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005615 if( ssl == NULL || ssl->conf == NULL )
5616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005620 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5626 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5627 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005630 return( ret );
5631 }
5632 }
5633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005635
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005636 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637}
5638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005640{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005641 if( transform == NULL )
5642 return;
5643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5645 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005646
Hanno Beckerd56ed242018-01-03 15:32:51 +00005647#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 mbedtls_md_free( &transform->md_ctx_enc );
5649 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005650#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005651
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005652 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005653}
5654
Hanno Becker0271f962018-08-16 13:23:47 +01005655#if defined(MBEDTLS_SSL_PROTO_DTLS)
5656
Hanno Becker533ab5f2020-02-05 10:49:13 +00005657void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005658{
5659 unsigned offset;
5660 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5661
5662 if( hs == NULL )
5663 return;
5664
Hanno Becker283f5ef2018-08-24 09:34:47 +01005665 ssl_free_buffered_record( ssl );
5666
Hanno Becker0271f962018-08-16 13:23:47 +01005667 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005668 ssl_buffering_free_slot( ssl, offset );
5669}
5670
5671static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5672 uint8_t slot )
5673{
5674 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5675 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005676
5677 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5678 return;
5679
Hanno Beckere605b192018-08-21 15:59:07 +01005680 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005681 {
Hanno Beckere605b192018-08-21 15:59:07 +01005682 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005683 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005684 mbedtls_free( hs_buf->data );
5685 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005686 }
5687}
5688
5689#endif /* MBEDTLS_SSL_PROTO_DTLS */
5690
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005691/*
5692 * Convert version numbers to/from wire format
5693 * and, for DTLS, to/from TLS equivalent.
5694 *
5695 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005696 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005697 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5698 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005701 unsigned char ver[2] )
5702{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703#if defined(MBEDTLS_SSL_PROTO_DTLS)
5704 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005707 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5708
5709 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5710 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5711 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005712 else
5713#else
5714 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005715#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005716 {
5717 ver[0] = (unsigned char) major;
5718 ver[1] = (unsigned char) minor;
5719 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005720}
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005723 const unsigned char ver[2] )
5724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#if defined(MBEDTLS_SSL_PROTO_DTLS)
5726 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005727 {
5728 *major = 255 - ver[0] + 2;
5729 *minor = 255 - ver[1] + 1;
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005732 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5733 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005734 else
5735#else
5736 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005737#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005738 {
5739 *major = ver[0];
5740 *minor = ver[1];
5741 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005742}
5743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744#endif /* MBEDTLS_SSL_TLS_C */