blob: 075345d36f7af04cbe182b26f995b8cb62338b75 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000019 */
20/*
21 * The SSL 3.0 specification was drafted by Netscape in 1996,
22 * and became an IETF standard in 1999.
23 *
24 * http://wp.netscape.com/eng/ssl3/
25 * http://www.ietf.org/rfc/rfc2246.txt
26 * http://www.ietf.org/rfc/rfc4346.txt
27 */
28
Gilles Peskinedb09ef62020-06-03 01:43:33 +020029#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
SimonBd5800b72016-04-26 07:43:27 +010033#if defined(MBEDTLS_PLATFORM_C)
34#include "mbedtls/platform.h"
35#else
36#include <stdlib.h>
37#define mbedtls_calloc calloc
38#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010039#endif
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020042#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/debug.h"
44#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010046#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020047
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020048#include "ssl_invasive.h"
49
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "mbedtls/psa_util.h"
54#include "psa/crypto.h"
55#endif
56
Janos Follath23bdca02016-10-07 14:47:14 +010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020059#endif
60
Hanno Beckercd9dcda2018-08-28 17:18:56 +010061static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020063/*
64 * Start a timer.
65 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020066 */
Hanno Becker0f57a652020-02-05 10:37:26 +000067void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020068{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020069 if( ssl->f_set_timer == NULL )
70 return;
71
72 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
73 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074}
75
76/*
77 * Return -1 is timer is expired, 0 if it isn't.
78 */
Hanno Becker7876d122020-02-05 10:39:31 +000079int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020081 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020082 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083
84 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089
90 return( 0 );
91}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092
Hanno Beckercfe45792019-07-03 16:13:00 +010093#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010094static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
95 unsigned char *buf,
96 size_t len,
97 mbedtls_record *rec );
98
Hanno Beckercfe45792019-07-03 16:13:00 +010099int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
100 unsigned char *buf,
101 size_t buflen )
102{
Hanno Becker54229812019-07-12 14:40:00 +0100103 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
105 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
106
107 /* We don't support record checking in TLS because
108 * (a) there doesn't seem to be a usecase for it, and
109 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
110 * and we'd need to backup the transform here.
111 */
112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
113 {
114 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
115 goto exit;
116 }
117#if defined(MBEDTLS_SSL_PROTO_DTLS)
118 else
119 {
irwir734f0cf2019-09-26 21:03:24 +0300120 mbedtls_record rec;
121
Hanno Becker54229812019-07-12 14:40:00 +0100122 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
123 if( ret != 0 )
124 {
125 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
126 goto exit;
127 }
128
129 if( ssl->transform_in != NULL )
130 {
131 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
132 if( ret != 0 )
133 {
134 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
135 goto exit;
136 }
137 }
138 }
139#endif /* MBEDTLS_SSL_PROTO_DTLS */
140
141exit:
142 /* On success, we have decrypted the buffer in-place, so make
143 * sure we don't leak any plaintext data. */
144 mbedtls_platform_zeroize( buf, buflen );
145
146 /* For the purpose of this API, treat messages with unexpected CID
147 * as well as such from future epochs as unexpected. */
148 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
149 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
150 {
151 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
152 }
153
154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
155 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100156}
157#endif /* MBEDTLS_SSL_RECORD_CHECKING */
158
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159#define SSL_DONT_FORCE_FLUSH 0
160#define SSL_FORCE_FLUSH 1
161
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100163
Hanno Beckerd5847772018-08-28 10:09:23 +0100164/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100165static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
166 uint8_t slot );
167static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
168static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
169static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
170static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100171static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
172 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100173static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100174
Hanno Becker11682cc2018-08-22 14:41:02 +0100175static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100176{
Hanno Becker89490712020-02-05 10:50:12 +0000177 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000178#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
179 size_t out_buf_len = ssl->out_buf_len;
180#else
181 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
182#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100183
Darryl Greenb33cc762019-11-28 14:29:44 +0000184 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100185 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186
Darryl Greenb33cc762019-11-28 14:29:44 +0000187 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100188}
189
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
191{
Hanno Becker11682cc2018-08-22 14:41:02 +0100192 size_t const bytes_written = ssl->out_left;
193 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194
195 /* Double-check that the write-index hasn't gone
196 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100197 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100198 {
199 /* Should never happen... */
200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
201 }
202
203 return( (int) ( mtu - bytes_written ) );
204}
205
206static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
207{
Janos Follath865b3eb2019-12-16 11:46:15 +0000208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400210 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211
212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400213 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 if( max_len > mfl )
216 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100217
218 /* By the standard (RFC 6066 Sect. 4), the MFL extension
219 * only limits the maximum record payload size, so in theory
220 * we would be allowed to pack multiple records of payload size
221 * MFL into a single datagram. However, this would mean that there's
222 * no way to explicitly communicate MTU restrictions to the peer.
223 *
224 * The following reduction of max_len makes sure that we never
225 * write datagrams larger than MFL + Record Expansion Overhead.
226 */
227 if( max_len <= ssl->out_left )
228 return( 0 );
229
230 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231#endif
232
233 ret = ssl_get_remaining_space_in_datagram( ssl );
234 if( ret < 0 )
235 return( ret );
236 remaining = (size_t) ret;
237
238 ret = mbedtls_ssl_get_record_expansion( ssl );
239 if( ret < 0 )
240 return( ret );
241 expansion = (size_t) ret;
242
243 if( remaining <= expansion )
244 return( 0 );
245
246 remaining -= expansion;
247 if( remaining >= max_len )
248 remaining = max_len;
249
250 return( (int) remaining );
251}
252
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200253/*
254 * Double the retransmit timeout value, within the allowed range,
255 * returning -1 if the maximum value has already been reached.
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258{
259 uint32_t new_timeout;
260
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200261 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262 return( -1 );
263
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200264 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
265 * in the following way: after the initial transmission and a first
266 * retransmission, back off to a temporary estimated MTU of 508 bytes.
267 * This value is guaranteed to be deliverable (if not guaranteed to be
268 * delivered) of any compliant IPv4 (and IPv6) network, and should work
269 * on most non-IP stacks too. */
270 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400271 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200272 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
274 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200275
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200276 new_timeout = 2 * ssl->handshake->retransmit_timeout;
277
278 /* Avoid arithmetic overflow and range overflow */
279 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200280 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200282 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283 }
284
285 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200287 ssl->handshake->retransmit_timeout ) );
288
289 return( 0 );
290}
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200294 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 ssl->handshake->retransmit_timeout ) );
297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
301int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200302 const unsigned char *key_enc, const unsigned char *key_dec,
303 size_t keylen,
304 const unsigned char *iv_enc, const unsigned char *iv_dec,
305 size_t ivlen,
306 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200307 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
309int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
311int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
312int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
313#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000314
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100315/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200317 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000318
Hanno Beckerccc13d02020-05-04 12:30:04 +0100319#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
320 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100321
322static size_t ssl_compute_padding_length( size_t len,
323 size_t granularity )
324{
325 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
326}
327
Hanno Becker581bc1b2020-05-04 12:20:03 +0100328/* This functions transforms a (D)TLS plaintext fragment and a record content
329 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
330 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
331 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100332 *
333 * struct {
334 * opaque content[DTLSPlaintext.length];
335 * ContentType real_type;
336 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100337 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100338 *
339 * Input:
340 * - `content`: The beginning of the buffer holding the
341 * plaintext to be wrapped.
342 * - `*content_size`: The length of the plaintext in Bytes.
343 * - `max_len`: The number of Bytes available starting from
344 * `content`. This must be `>= *content_size`.
345 * - `rec_type`: The desired record content type.
346 *
347 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100348 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
349 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100350 *
351 * Returns:
352 * - `0` on success.
353 * - A negative error code if `max_len` didn't offer enough space
354 * for the expansion.
355 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100356static int ssl_build_inner_plaintext( unsigned char *content,
357 size_t *content_size,
358 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100359 uint8_t rec_type,
360 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100361{
362 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100363
364 /* Write real content type */
365 if( remaining == 0 )
366 return( -1 );
367 content[ len ] = rec_type;
368 len++;
369 remaining--;
370
371 if( remaining < pad )
372 return( -1 );
373 memset( content + len, 0, pad );
374 len += pad;
375 remaining -= pad;
376
377 *content_size = len;
378 return( 0 );
379}
380
Hanno Becker581bc1b2020-05-04 12:20:03 +0100381/* This function parses a (D)TLSInnerPlaintext structure.
382 * See ssl_build_inner_plaintext() for details. */
383static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100384 size_t *content_size,
385 uint8_t *rec_type )
386{
387 size_t remaining = *content_size;
388
389 /* Determine length of padding by skipping zeroes from the back. */
390 do
391 {
392 if( remaining == 0 )
393 return( -1 );
394 remaining--;
395 } while( content[ remaining ] == 0 );
396
397 *content_size = remaining;
398 *rec_type = content[ remaining ];
399
400 return( 0 );
401}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
403 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100404
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100405/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100406 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000407static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100408 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100409 mbedtls_record *rec,
410 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000411{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100412 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100413 *
414 * additional_data = seq_num + TLSCompressed.type +
415 * TLSCompressed.version + TLSCompressed.length;
416 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100417 * For the CID extension, this is extended as follows
418 * (quoting draft-ietf-tls-dtls-connection-id-05,
419 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100420 *
421 * additional_data = seq_num + DTLSPlaintext.type +
422 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100423 * cid +
424 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100425 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100426 *
427 * For TLS 1.3, the record sequence number is dropped from the AAD
428 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100429 */
430
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100431 unsigned char *cur = add_data;
432
433#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
434 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
435#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
436 {
437 ((void) minor_ver);
438 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
439 cur += sizeof( rec->ctr );
440 }
441
442 *cur = rec->type;
443 cur++;
444
445 memcpy( cur, rec->ver, sizeof( rec->ver ) );
446 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100447
Hanno Beckera0e20d02019-05-15 14:03:01 +0100448#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100449 if( rec->cid_len != 0 )
450 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100451 memcpy( cur, rec->cid, rec->cid_len );
452 cur += rec->cid_len;
453
454 *cur = rec->cid_len;
455 cur++;
456
457 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
458 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
459 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100460 }
461 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100463 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100464 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
465 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
466 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100467 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100468
469 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000470}
471
Hanno Becker9d062f92020-02-07 10:26:36 +0000472#if defined(MBEDTLS_SSL_PROTO_SSL3)
473
474#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
475
476/*
477 * SSLv3.0 MAC functions
478 */
479static void ssl_mac( mbedtls_md_context_t *md_ctx,
480 const unsigned char *secret,
481 const unsigned char *buf, size_t len,
482 const unsigned char *ctr, int type,
483 unsigned char out[SSL3_MAC_MAX_BYTES] )
484{
485 unsigned char header[11];
486 unsigned char padding[48];
487 int padlen;
488 int md_size = mbedtls_md_get_size( md_ctx->md_info );
489 int md_type = mbedtls_md_get_type( md_ctx->md_info );
490
491 /* Only MD5 and SHA-1 supported */
492 if( md_type == MBEDTLS_MD_MD5 )
493 padlen = 48;
494 else
495 padlen = 40;
496
497 memcpy( header, ctr, 8 );
498 header[ 8] = (unsigned char) type;
499 header[ 9] = (unsigned char)( len >> 8 );
500 header[10] = (unsigned char)( len );
501
502 memset( padding, 0x36, padlen );
503 mbedtls_md_starts( md_ctx );
504 mbedtls_md_update( md_ctx, secret, md_size );
505 mbedtls_md_update( md_ctx, padding, padlen );
506 mbedtls_md_update( md_ctx, header, 11 );
507 mbedtls_md_update( md_ctx, buf, len );
508 mbedtls_md_finish( md_ctx, out );
509
510 memset( padding, 0x5C, padlen );
511 mbedtls_md_starts( md_ctx );
512 mbedtls_md_update( md_ctx, secret, md_size );
513 mbedtls_md_update( md_ctx, padding, padlen );
514 mbedtls_md_update( md_ctx, out, md_size );
515 mbedtls_md_finish( md_ctx, out );
516}
517#endif /* MBEDTLS_SSL_PROTO_SSL3 */
518
Hanno Becker67a37db2020-05-28 16:27:07 +0100519#if defined(MBEDTLS_GCM_C) || \
520 defined(MBEDTLS_CCM_C) || \
521 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100522static int ssl_transform_aead_dynamic_iv_is_explicit(
523 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100524{
Hanno Becker17263802020-05-28 07:05:48 +0100525 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100526}
527
Hanno Becker17263802020-05-28 07:05:48 +0100528/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
529 *
530 * Concretely, this occurs in two variants:
531 *
532 * a) Fixed and dynamic IV lengths add up to total IV length, giving
533 * IV = fixed_iv || dynamic_iv
534 *
Hanno Becker15952812020-06-04 13:31:46 +0100535 * This variant is used in TLS 1.2 when used with GCM or CCM.
536 *
Hanno Becker17263802020-05-28 07:05:48 +0100537 * b) Fixed IV lengths matches total IV length, giving
538 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100539 *
540 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
541 *
542 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100543 *
544 * This function has the precondition that
545 *
546 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
547 *
548 * which has to be ensured by the caller. If this precondition
549 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100550 */
551static void ssl_build_record_nonce( unsigned char *dst_iv,
552 size_t dst_iv_len,
553 unsigned char const *fixed_iv,
554 size_t fixed_iv_len,
555 unsigned char const *dynamic_iv,
556 size_t dynamic_iv_len )
557{
558 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100559
560 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100561 memset( dst_iv, 0, dst_iv_len );
562 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100563
Hanno Becker17263802020-05-28 07:05:48 +0100564 dst_iv += dst_iv_len - dynamic_iv_len;
565 for( i = 0; i < dynamic_iv_len; i++ )
566 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100567}
Hanno Becker67a37db2020-05-28 16:27:07 +0100568#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100569
Hanno Beckera18d1322018-01-03 14:27:32 +0000570int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
571 mbedtls_ssl_transform *transform,
572 mbedtls_record *rec,
573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100577 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000578 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100579 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100580 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000581 size_t post_avail;
582
583 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000584#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200585 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000586 ((void) ssl);
587#endif
588
589 /* The PRNG is used for dynamic IV generation that's used
590 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200591#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000592 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
593 ((void) f_rng);
594 ((void) p_rng);
595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000599 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100600 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
603 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100604 if( rec == NULL
605 || rec->buf == NULL
606 || rec->buf_len < rec->data_offset
607 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100608#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100609 || rec->cid_len != 0
610#endif
611 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000612 {
613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100615 }
616
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000617 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100618 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000620 data, rec->data_len );
621
622 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
623
624 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
625 {
626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
627 (unsigned) rec->data_len,
628 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
630 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100631
Hanno Becker92313402020-05-20 13:58:58 +0100632 /* The following two code paths implement the (D)TLSInnerPlaintext
633 * structure present in TLS 1.3 and DTLS 1.2 + CID.
634 *
635 * See ssl_build_inner_plaintext() for more information.
636 *
637 * Note that this changes `rec->data_len`, and hence
638 * `post_avail` needs to be recalculated afterwards.
639 *
640 * Note also that the two code paths cannot occur simultaneously
641 * since they apply to different versions of the protocol. There
642 * is hence no risk of double-addition of the inner plaintext.
643 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100644#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
645 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
646 {
Hanno Becker13996922020-05-28 16:15:19 +0100647 size_t padding =
648 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100649 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100650 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100651 &rec->data_len,
652 post_avail,
653 rec->type,
654 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100655 {
656 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
657 }
658
659 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
660 }
661#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
662
Hanno Beckera0e20d02019-05-15 14:03:01 +0100663#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100664 /*
665 * Add CID information
666 */
667 rec->cid_len = transform->out_cid_len;
668 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
669 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100670
671 if( rec->cid_len != 0 )
672 {
Hanno Becker13996922020-05-28 16:15:19 +0100673 size_t padding =
674 ssl_compute_padding_length( rec->data_len,
675 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100676 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100677 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100678 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100679 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100680 * Note that this changes `rec->data_len`, and hence
681 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100682 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100683 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100684 &rec->data_len,
685 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100686 rec->type,
687 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100688 {
689 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
690 }
691
692 rec->type = MBEDTLS_SSL_MSG_CID;
693 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100694#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100695
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100696 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
697
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100699 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 */
Hanno Becker52344c22018-01-03 15:24:20 +0000701#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( mode == MBEDTLS_MODE_STREAM ||
703 ( mode == MBEDTLS_MODE_CBC
704#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000705 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100706#endif
707 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000709 if( post_avail < transform->maclen )
710 {
711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
712 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
713 }
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000716 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200717 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000718 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000719 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
720 data, rec->data_len, rec->ctr, rec->type, mac );
721 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200722 }
723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
726 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000727 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200728 {
Hanno Becker992b6872017-11-09 18:57:39 +0000729 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
730
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100731 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
732 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000733
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000734 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100735 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000736 mbedtls_md_hmac_update( &transform->md_ctx_enc,
737 data, rec->data_len );
738 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
739 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
740
741 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200742 }
743 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200744#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200748 }
749
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000750 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
751 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200752
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000753 rec->data_len += transform->maclen;
754 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100755 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200756 }
Hanno Becker52344c22018-01-03 15:24:20 +0000757#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200759 /*
760 * Encrypt
761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
763 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000766 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000768 "including %d bytes of padding",
769 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000771 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
772 transform->iv_enc, transform->ivlen,
773 data, rec->data_len,
774 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200777 return( ret );
778 }
779
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000780 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200784 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 }
Paul Bakker68884e32013-01-07 18:20:04 +0100786 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000788
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200789#if defined(MBEDTLS_GCM_C) || \
790 defined(MBEDTLS_CCM_C) || \
791 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200793 mode == MBEDTLS_MODE_CCM ||
794 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000795 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200797 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100798 unsigned char *dynamic_iv;
799 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100800 int dynamic_iv_is_explicit =
801 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000802
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100803 /* Check that there's space for the authentication tag. */
804 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000805 {
806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
807 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
808 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000809
Paul Bakker68884e32013-01-07 18:20:04 +0100810 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100811 * Build nonce for AEAD encryption.
812 *
813 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
814 * part of the IV is prepended to the ciphertext and
815 * can be chosen freely - in particular, it need not
816 * agree with the record sequence number.
817 * However, since ChaChaPoly as well as all AEAD modes
818 * in TLS 1.3 use the record sequence number as the
819 * dynamic part of the nonce, we uniformly use the
820 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100821 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100822 dynamic_iv = rec->ctr;
823 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200824
Hanno Becker17263802020-05-28 07:05:48 +0100825 ssl_build_record_nonce( iv, sizeof( iv ),
826 transform->iv_enc,
827 transform->fixed_ivlen,
828 dynamic_iv,
829 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100830
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100831 /*
832 * Build additional data for AEAD encryption.
833 * This depends on the TLS version.
834 */
835 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
836 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100837
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200838 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100839 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200840 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100841 dynamic_iv,
842 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000843 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100844 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200846 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000847 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000848
Paul Bakker68884e32013-01-07 18:20:04 +0100849 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200850 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200851 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200853 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000854 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100855 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000856 data, rec->data_len, /* source */
857 data, &rec->data_len, /* destination */
858 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200861 return( ret );
862 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000863 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
864 data + rec->data_len, transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100865 /* Account for authentication tag. */
866 rec->data_len += transform->taglen;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000867 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100868
869 /*
870 * Prefix record content with dynamic IV in case it is explicit.
871 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100872 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100873 {
874 if( rec->data_offset < dynamic_iv_len )
875 {
876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
877 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
878 }
879
880 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
881 rec->data_offset -= dynamic_iv_len;
882 rec->data_len += dynamic_iv_len;
883 }
884
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100885 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000886 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100888#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200889#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000893 size_t padlen, i;
894 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000895
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000896 /* Currently we're always using minimal padding
897 * (up to 255 bytes would be allowed). */
898 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
899 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 padlen = 0;
901
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000902 /* Check there's enough space in the buffer for the padding. */
903 if( post_avail < padlen + 1 )
904 {
905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
906 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
907 }
908
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000910 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000912 rec->data_len += padlen + 1;
913 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000916 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000917 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
918 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000919 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000920 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000921 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000922 if( f_rng == NULL )
923 {
924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
926 }
927
928 if( rec->data_offset < transform->ivlen )
929 {
930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
931 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
932 }
933
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000934 /*
935 * Generate IV
936 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000937 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000938 if( ret != 0 )
939 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000940
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000941 memcpy( data - transform->ivlen, transform->iv_enc,
942 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000943
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000948 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000949 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200950 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000952 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
953 transform->iv_enc,
954 transform->ivlen,
955 data, rec->data_len,
956 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200959 return( ret );
960 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200961
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000962 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200966 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000969 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200970 {
971 /*
972 * Save IV in SSL3 and TLS1
973 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000974 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
975 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000977 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200978#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000979 {
980 data -= transform->ivlen;
981 rec->data_offset -= transform->ivlen;
982 rec->data_len += transform->ivlen;
983 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100986 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100987 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000988 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
989
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100990 /*
991 * MAC(MAC_write_key, seq_num +
992 * TLSCipherText.type +
993 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100994 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100995 * IV + // except for TLS 1.0
996 * ENC(content + padding + padding_length));
997 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000998
999 if( post_avail < transform->maclen)
1000 {
1001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1002 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1003 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001004
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001005 ssl_extract_add_data_from_record( add_data, &add_data_len,
1006 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +01001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001009 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001010 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001011
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001012 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001013 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001014 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1015 data, rec->data_len );
1016 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1017 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001018
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001019 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001020
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001021 rec->data_len += transform->maclen;
1022 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001023 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001027 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001028#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001034 /* Make extra sure authentication was performed, exactly once */
1035 if( auth_done != 1 )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001039 }
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
1043 return( 0 );
1044}
1045
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001046#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001047/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001048 * Turn a bit into a mask:
1049 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
1050 * - if bit == 0, return the all-bits 0 mask, aka 0
1051 */
1052static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
1053{
1054 /* MSVC has a warning about unary minus on unsigned integer types,
1055 * but this is well-defined and precisely what we want to do here. */
1056#if defined(_MSC_VER)
1057#pragma warning( push )
1058#pragma warning( disable : 4146 )
1059#endif
1060 return -bit;
1061#if defined(_MSC_VER)
1062#pragma warning( pop )
1063#endif
1064}
1065
1066/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001067 * Constant-flow mask generation for "less than" comparison:
1068 * - if x < y, return all bits 1, that is (size_t) -1
1069 * - otherwise, return all bits 0, that is 0
1070 *
1071 * Use only bit operations to avoid branches that could be used by some
1072 * compilers on some platforms to translate comparison operators.
1073 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001074static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001075{
1076 /* This has the msb set if and only if x < y */
1077 const size_t sub = x - y;
1078
1079 /* sub1 = (x < y) in {0, 1} */
1080 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
1081
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001082 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001083 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001084
1085 return( mask );
1086}
1087
1088/*
1089 * Constant-flow mask generation for "greater or equal" comparison:
1090 * - if x >= y, return all bits 1, that is (size_t) -1
1091 * - otherwise, return all bits 0, that is 0
1092 *
1093 * Use only bit operations to avoid branches that could be used by some
1094 * compilers on some platforms to translate comparison operators.
1095 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001096static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001097{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001098 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001099}
1100
1101/*
1102 * Constant-flow boolean "equal" comparison:
1103 * return x == y
1104 *
1105 * Use only bit operations to avoid branches that could be used by some
1106 * compilers on some platforms to translate comparison operators.
1107 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001108static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001109{
1110 /* diff = 0 if x == y, non-zero otherwise */
1111 const size_t diff = x ^ y;
1112
1113 /* MSVC has a warning about unary minus on unsigned integer types,
1114 * but this is well-defined and precisely what we want to do here. */
1115#if defined(_MSC_VER)
1116#pragma warning( push )
1117#pragma warning( disable : 4146 )
1118#endif
1119
1120 /* diff_msb's most significant bit is equal to x != y */
1121 const size_t diff_msb = ( diff | -diff );
1122
1123#if defined(_MSC_VER)
1124#pragma warning( pop )
1125#endif
1126
1127 /* diff1 = (x != y) in {0, 1} */
1128 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1129
1130 return( 1 ^ diff1 );
1131}
1132
1133/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001134 * Constant-flow conditional memcpy:
1135 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1136 * - otherwise, a no-op,
1137 * but with execution flow independent of the values of c1 and c2.
1138 *
1139 * Use only bit operations to avoid branches that could be used by some
1140 * compilers on some platforms to translate comparison operators.
1141 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001142static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1143 const unsigned char *src,
1144 size_t len,
1145 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001146{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001147 /* mask = c1 == c2 ? 0xff : 0x00 */
1148 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
1149 const unsigned char mask = mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001150
1151 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
1152 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001153 dst[i] = ( dst[i] & ~mask ) | ( src[i] & mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001154}
1155
1156/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001157 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001158 *
1159 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1160 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001161 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001162MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001163 mbedtls_md_context_t *ctx,
1164 const unsigned char *add_data, size_t add_data_len,
1165 const unsigned char *data, size_t data_len_secret,
1166 size_t min_data_len, size_t max_data_len,
1167 unsigned char *output )
1168{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001169 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001170 * This function breaks the HMAC abstraction and uses the md_clone()
1171 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001172 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001173 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001174 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001175 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001176 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001177 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1178 * minlen, then cloning the context, and for each byte up to maxlen
1179 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001180 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001181 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001182 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001183 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001184 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1185 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001186 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001187 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001188 const unsigned char * const okey = ikey + block_size;
1189 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001190
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001191 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1192 mbedtls_md_context_t aux;
1193 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001195
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001196 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001197
1198#define MD_CHK( func_call ) \
1199 do { \
1200 ret = (func_call); \
1201 if( ret != 0 ) \
1202 goto cleanup; \
1203 } while( 0 )
1204
1205 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001206
1207 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1208 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001209 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1210 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001211
1212 /* For each possible length, compute the hash up to that point */
1213 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001214 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001215 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1216 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001217 /* Keep only the correct inner_hash in the output buffer */
1218 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1219 offset, data_len_secret );
1220
1221 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001222 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001223 }
1224
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001225 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001226 MD_CHK( mbedtls_md_starts( ctx ) );
1227 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1228 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1229 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001230
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001231 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001232 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001233
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001234#undef MD_CHK
1235
1236cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001237 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001238 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001239}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001240
1241/*
1242 * Constant-flow memcpy from variable position in buffer.
1243 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001244 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001245 */
1246MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1247 unsigned char *dst,
1248 const unsigned char *src_base,
1249 size_t offset_secret,
1250 size_t offset_min, size_t offset_max,
1251 size_t len )
1252{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001253 size_t offset;
1254
1255 for( offset = offset_min; offset <= offset_max; offset++ )
1256 {
1257 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1258 offset, offset_secret );
1259 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001260}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001261#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001262
Hanno Becker605949f2019-07-12 08:23:59 +01001263int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001264 mbedtls_ssl_transform *transform,
1265 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001267 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001269 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001270#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001271 size_t padlen = 0, correct = 1;
1272#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001273 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001274 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001275 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001276
Hanno Beckera18d1322018-01-03 14:27:32 +00001277#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001278 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001279 ((void) ssl);
1280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001283 if( rec == NULL ||
1284 rec->buf == NULL ||
1285 rec->buf_len < rec->data_offset ||
1286 rec->buf_len - rec->data_offset < rec->data_len )
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001290 }
1291
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001292 data = rec->buf + rec->data_offset;
1293 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Hanno Beckera0e20d02019-05-15 14:03:01 +01001295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001296 /*
1297 * Match record's CID with incoming CID.
1298 */
Hanno Becker938489a2019-05-08 13:02:22 +01001299 if( rec->cid_len != transform->in_cid_len ||
1300 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1301 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001302 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001303 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001304#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1307 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001308 {
1309 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001310 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1311 transform->iv_dec,
1312 transform->ivlen,
1313 data, rec->data_len,
1314 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001317 return( ret );
1318 }
1319
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001320 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
Paul Bakker68884e32013-01-07 18:20:04 +01001326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001328#if defined(MBEDTLS_GCM_C) || \
1329 defined(MBEDTLS_CCM_C) || \
1330 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001332 mode == MBEDTLS_MODE_CCM ||
1333 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001334 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001335 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001336 unsigned char *dynamic_iv;
1337 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001338
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001339 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001340 * Extract dynamic part of nonce for AEAD decryption.
1341 *
1342 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1343 * part of the IV is prepended to the ciphertext and
1344 * can be chosen freely - in particular, it need not
1345 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001346 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001347 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001348 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001349 {
1350 if( rec->data_len < dynamic_iv_len )
1351 {
1352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) ",
1353 rec->data_len,
1354 dynamic_iv_len ) );
1355 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1356 }
1357 dynamic_iv = data;
1358
1359 data += dynamic_iv_len;
1360 rec->data_offset += dynamic_iv_len;
1361 rec->data_len -= dynamic_iv_len;
1362 }
Hanno Becker17263802020-05-28 07:05:48 +01001363 else
1364 {
1365 dynamic_iv = rec->ctr;
1366 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001367
1368 /* Check that there's space for the authentication tag. */
1369 if( rec->data_len < transform->taglen )
1370 {
1371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < taglen (%d) " ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001373 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001374 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001375
Hanno Beckerdf8be222020-05-21 15:30:57 +01001376 /*
1377 * Prepare nonce from dynamic and static parts.
1378 */
Hanno Becker17263802020-05-28 07:05:48 +01001379 ssl_build_record_nonce( iv, sizeof( iv ),
1380 transform->iv_dec,
1381 transform->fixed_ivlen,
1382 dynamic_iv,
1383 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001384
Hanno Beckerdf8be222020-05-21 15:30:57 +01001385 /*
1386 * Build additional data for AEAD encryption.
1387 * This depends on the TLS version.
1388 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001389 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1390 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001391 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001392 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001393
Hanno Beckerd96a6522019-07-10 13:55:25 +01001394 /* Because of the check above, we know that there are
1395 * explicit_iv_len Bytes preceeding data, and taglen
1396 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001397 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001398 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001399
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001400 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001401 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001402 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001403
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001404 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001405 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001406 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001407 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1408 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001409 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001410 data, rec->data_len,
1411 data, &olen,
1412 data + rec->data_len,
1413 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1418 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001419
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001420 return( ret );
1421 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001422 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423
Hanno Beckerd96a6522019-07-10 13:55:25 +01001424 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001425 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001433#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 /*
Paul Bakker45829992013-01-03 14:52:21 +01001439 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001442 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1443 {
1444 /* The ciphertext is prefixed with the CBC IV. */
1445 minlen += transform->ivlen;
1446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001447#endif
Paul Bakker45829992013-01-03 14:52:21 +01001448
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001449 /* Size considerations:
1450 *
1451 * - The CBC cipher text must not be empty and hence
1452 * at least of size transform->ivlen.
1453 *
1454 * Together with the potential IV-prefix, this explains
1455 * the first of the two checks below.
1456 *
1457 * - The record must contain a MAC, either in plain or
1458 * encrypted, depending on whether Encrypt-then-MAC
1459 * is used or not.
1460 * - If it is, the message contains the IV-prefix,
1461 * the CBC ciphertext, and the MAC.
1462 * - If it is not, the padded plaintext, and hence
1463 * the CBC ciphertext, has at least length maclen + 1
1464 * because there is at least the padding length byte.
1465 *
1466 * As the CBC ciphertext is not empty, both cases give the
1467 * lower bound minlen + maclen + 1 on the record size, which
1468 * we test for in the second check below.
1469 */
1470 if( rec->data_len < minlen + transform->ivlen ||
1471 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001474 "+ 1 ) ( + expl IV )", rec->data_len,
1475 transform->ivlen,
1476 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001478 }
1479
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001480 /*
1481 * Authenticate before decrypt if enabled
1482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001484 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001485 {
Hanno Becker992b6872017-11-09 18:57:39 +00001486 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001489
Hanno Beckerd96a6522019-07-10 13:55:25 +01001490 /* Update data_len in tandem with add_data.
1491 *
1492 * The subtraction is safe because of the previous check
1493 * data_len >= minlen + maclen + 1.
1494 *
1495 * Afterwards, we know that data + data_len is followed by at
1496 * least maclen Bytes, which justifies the call to
1497 * mbedtls_ssl_safer_memcmp() below.
1498 *
1499 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001500 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001501 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1502 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001503
Hanno Beckerd96a6522019-07-10 13:55:25 +01001504 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001505 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1506 add_data_len );
1507 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1508 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001509 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1510 data, rec->data_len );
1511 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1512 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001513
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001514 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1515 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001516 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001517 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001518
Hanno Beckerd96a6522019-07-10 13:55:25 +01001519 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001520 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1521 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001525 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001526 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001529
1530 /*
1531 * Check length sanity
1532 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001533
1534 /* We know from above that data_len > minlen >= 0,
1535 * so the following check in particular implies that
1536 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001537 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001540 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001542 }
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001546 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001547 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001548 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001549 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001550 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001551 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001552
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001553 data += transform->ivlen;
1554 rec->data_offset += transform->ivlen;
1555 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001558
Hanno Beckerd96a6522019-07-10 13:55:25 +01001559 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1560
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001561 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1562 transform->iv_dec, transform->ivlen,
1563 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001566 return( ret );
1567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001568
Hanno Beckerd96a6522019-07-10 13:55:25 +01001569 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001570 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001577 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001578 {
1579 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001580 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1581 * records is equivalent to CBC decryption of the concatenation
1582 * of the records; in other words, IVs are maintained across
1583 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001584 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001585 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1586 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001590 /* Safe since data_len >= minlen + maclen + 1, so after having
1591 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001592 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1593 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001594 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001595
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001596 if( auth_done == 1 )
1597 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001598 const size_t mask = mbedtls_ssl_cf_mask_ge(
1599 rec->data_len,
1600 padlen + 1 );
1601 correct &= mask;
1602 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001603 }
1604 else
Paul Bakker45829992013-01-03 14:52:21 +01001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001607 if( rec->data_len < transform->maclen + padlen + 1 )
1608 {
1609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1610 rec->data_len,
1611 transform->maclen,
1612 padlen + 1 ) );
1613 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001614#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001615
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001616 const size_t mask = mbedtls_ssl_cf_mask_ge(
1617 rec->data_len,
1618 transform->maclen + padlen + 1 );
1619 correct &= mask;
1620 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001623 padlen++;
1624
1625 /* Regardless of the validity of the padding,
1626 * we have data_len >= padlen here. */
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001629 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001631 /* This is the SSL 3.0 path, we don't have to worry about Lucky
1632 * 13, because there's a strictly worse padding attack built in
1633 * the protocol (known as part of POODLE), so branches are OK. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001634 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#if defined(MBEDTLS_SSL_DEBUG_ALL)
1637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001638 "should be no more than %d",
1639 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001640#endif
Paul Bakker45829992013-01-03 14:52:21 +01001641 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
1643 }
1644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1646#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1647 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001648 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001650 /* The padding check involves a series of up to 256
1651 * consecutive memory reads at the end of the record
1652 * plaintext buffer. In order to hide the length and
1653 * validity of the padding, always perform exactly
1654 * `min(256,plaintext_len)` reads (but take into account
1655 * only the last `padlen` bytes for the padding check). */
1656 size_t pad_count = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001657 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001659 /* Index of first padding byte; it has been ensured above
1660 * that the subtraction is safe. */
1661 size_t const padding_idx = rec->data_len - padlen;
1662 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1663 size_t const start_idx = rec->data_len - num_checks;
1664 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001665
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001666 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001667 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001668 /* pad_count += (idx >= padding_idx) &&
1669 * (chech[idx] == padlen - 1);
1670 */
1671 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1672 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1673 padlen - 1 );
1674 pad_count += mask & equal;
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001675 }
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001676 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001679 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001681#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001682 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001684 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1686 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001690 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001691
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001692 /* If the padding was found to be invalid, padlen == 0
1693 * and the subtraction is safe. If the padding was found valid,
1694 * padlen hasn't been changed and the previous assertion
1695 * data_len >= padlen still holds. */
1696 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001698 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001699#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001703 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001705#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001707 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
1710 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001711 * Authenticate if not done yet.
1712 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 */
Hanno Becker52344c22018-01-03 15:24:20 +00001714#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001715 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001716 {
Hanno Becker992b6872017-11-09 18:57:39 +00001717 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001718 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001719
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001720 /* If the initial value of padlen was such that
1721 * data_len < maclen + padlen + 1, then padlen
1722 * got reset to 1, and the initial check
1723 * data_len >= minlen + maclen + 1
1724 * guarantees that at this point we still
1725 * have at least data_len >= maclen.
1726 *
1727 * If the initial value of padlen was such that
1728 * data_len >= maclen + padlen + 1, then we have
1729 * subtracted either padlen + 1 (if the padding was correct)
1730 * or 0 (if the padding was incorrect) since then,
1731 * hence data_len >= maclen in any case.
1732 */
1733 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001734 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1735 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001738 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001740 ssl_mac( &transform->md_ctx_dec,
1741 transform->mac_dec,
1742 data, rec->data_len,
1743 rec->ctr, rec->type,
1744 mac_expect );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001745 memcpy( mac_peer, data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001746 }
1747 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1749#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1750 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001751 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 {
1753 /*
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001754 * The next two sizes are the minimum and maximum values of
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001755 * data_len over all padlen values.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001756 *
1757 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001758 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001759 *
1760 * Note that max_len + maclen is never more than the buffer
1761 * length, as we previously did in_msglen -= maclen too.
1762 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001763 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001764 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1765
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001766 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1767 add_data, add_data_len,
1768 data, rec->data_len, min_len, max_len,
1769 mac_expect );
1770 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02001771 {
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1773 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02001774 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001775
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001776 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1777 rec->data_len,
1778 min_len, max_len,
1779 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 }
1781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1783 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001789#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001790 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001791 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001792#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001794 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001795 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_SSL_DEBUG_ALL)
1798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001799#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001800 correct = 0;
1801 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001802 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001804
1805 /*
1806 * Finally check the correct flag
1807 */
1808 if( correct == 0 )
1809 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001810#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001811
1812 /* Make extra sure authentication was performed, exactly once */
1813 if( auth_done != 1 )
1814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001817 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Hanno Beckerccc13d02020-05-04 12:30:04 +01001819#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1820 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1821 {
1822 /* Remove inner padding and infer true content type. */
1823 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1824 &rec->type );
1825
1826 if( ret != 0 )
1827 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1828 }
1829#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1830
Hanno Beckera0e20d02019-05-15 14:03:01 +01001831#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001832 if( rec->cid_len != 0 )
1833 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001834 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1835 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001836 if( ret != 0 )
1837 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1838 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
1843 return( 0 );
1844}
1845
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001846#undef MAC_NONE
1847#undef MAC_PLAINTEXT
1848#undef MAC_CIPHERTEXT
1849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851/*
1852 * Compression/decompression functions
1853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001855{
Janos Follath865b3eb2019-12-16 11:46:15 +00001856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001857 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001858 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001859 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001860 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001861#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1862 size_t out_buf_len = ssl->out_buf_len;
1863#else
1864 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1865#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001868
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001869 if( len_pre == 0 )
1870 return( 0 );
1871
Paul Bakker2770fbd2012-07-03 13:30:23 +00001872 memcpy( msg_pre, ssl->out_msg, len_pre );
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001875 ssl->out_msglen ) );
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001878 ssl->out_msg, ssl->out_msglen );
1879
Paul Bakker48916f92012-09-16 19:57:18 +00001880 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1881 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1882 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001883 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001884
Paul Bakker48916f92012-09-16 19:57:18 +00001885 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001886 if( ret != Z_OK )
1887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1889 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001890 }
1891
Darryl Greenb33cc762019-11-28 14:29:44 +00001892 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001893 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001896 ssl->out_msglen ) );
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899 ssl->out_msg, ssl->out_msglen );
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001902
1903 return( 0 );
1904}
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907{
Janos Follath865b3eb2019-12-16 11:46:15 +00001908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001910 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001911 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001912 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001913#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1914 size_t in_buf_len = ssl->in_buf_len;
1915#else
1916 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1917#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001920
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001921 if( len_pre == 0 )
1922 return( 0 );
1923
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924 memcpy( msg_pre, ssl->in_msg, len_pre );
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927 ssl->in_msglen ) );
1928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001930 ssl->in_msg, ssl->in_msglen );
1931
Paul Bakker48916f92012-09-16 19:57:18 +00001932 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1933 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1934 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001935 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001936
Paul Bakker48916f92012-09-16 19:57:18 +00001937 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001938 if( ret != Z_OK )
1939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1941 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942 }
1943
Darryl Greenb33cc762019-11-28 14:29:44 +00001944 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001945 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001948 ssl->in_msglen ) );
1949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001951 ssl->in_msg, ssl->in_msglen );
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001954
1955 return( 0 );
1956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001960 * Fill the input message buffer by appending data to it.
1961 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001962 *
1963 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1964 * available (from this read and/or a previous one). Otherwise, an error code
1965 * is returned (possibly EOF or WANT_READ).
1966 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001967 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1968 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1969 * since we always read a whole datagram at once.
1970 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001971 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001972 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001975{
Janos Follath865b3eb2019-12-16 11:46:15 +00001976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001977 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001978#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1979 size_t in_buf_len = ssl->in_buf_len;
1980#else
1981 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001986 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001989 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001991 }
1992
Darryl Greenb33cc762019-11-28 14:29:44 +00001993 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001997 }
1998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002002 uint32_t timeout;
2003
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002004 /* Just to be sure */
2005 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2006 {
2007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2008 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2010 }
2011
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002012 /*
2013 * The point is, we need to always read a full datagram at once, so we
2014 * sometimes read more then requested, and handle the additional data.
2015 * It could be the rest of the current record (while fetching the
2016 * header) and/or some other records in the same datagram.
2017 */
2018
2019 /*
2020 * Move to the next record in the already read datagram if applicable
2021 */
2022 if( ssl->next_record_offset != 0 )
2023 {
2024 if( ssl->in_left < ssl->next_record_offset )
2025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002028 }
2029
2030 ssl->in_left -= ssl->next_record_offset;
2031
2032 if( ssl->in_left != 0 )
2033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002035 ssl->next_record_offset ) );
2036 memmove( ssl->in_hdr,
2037 ssl->in_hdr + ssl->next_record_offset,
2038 ssl->in_left );
2039 }
2040
2041 ssl->next_record_offset = 0;
2042 }
2043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002046
2047 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002048 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002049 */
2050 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002053 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002054 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002055
2056 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002057 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002058 * are not at the beginning of a new record, the caller did something
2059 * wrong.
2060 */
2061 if( ssl->in_left != 0 )
2062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002065 }
2066
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002067 /*
2068 * Don't even try to read if time's out already.
2069 * This avoids by-passing the timer when repeatedly receiving messages
2070 * that will end up being dropped.
2071 */
Hanno Becker7876d122020-02-05 10:39:31 +00002072 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002073 {
2074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002075 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002076 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002077 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002078 {
Darryl Greenb33cc762019-11-28 14:29:44 +00002079 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002082 timeout = ssl->handshake->retransmit_timeout;
2083 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002084 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002087
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002088 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002089 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2090 timeout );
2091 else
2092 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002095
2096 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002098 }
2099
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002100 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00002103 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002106 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002107 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002110 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002111 }
2112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002116 return( ret );
2117 }
2118
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002119 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002122 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002124 {
Hanno Becker786300f2020-02-05 10:46:40 +00002125 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002126 {
Hanno Becker786300f2020-02-05 10:46:40 +00002127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
2128 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002129 return( ret );
2130 }
2131
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002132 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002135 }
2136
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 if( ret < 0 )
2138 return( ret );
2139
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002140 ssl->in_left = ret;
2141 }
2142 else
2143#endif
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002146 ssl->in_left, nb_want ) );
2147
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002148 while( ssl->in_left < nb_want )
2149 {
2150 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002151
Hanno Becker7876d122020-02-05 10:39:31 +00002152 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002153 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2154 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002155 {
2156 if( ssl->f_recv_timeout != NULL )
2157 {
2158 ret = ssl->f_recv_timeout( ssl->p_bio,
2159 ssl->in_hdr + ssl->in_left, len,
2160 ssl->conf->read_timeout );
2161 }
2162 else
2163 {
2164 ret = ssl->f_recv( ssl->p_bio,
2165 ssl->in_hdr + ssl->in_left, len );
2166 }
2167 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002170 ssl->in_left, nb_want ) );
2171 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002172
2173 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002175
2176 if( ret < 0 )
2177 return( ret );
2178
makise-homuraaf9513b2020-08-24 18:26:27 +03002179 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002180 {
Darryl Green11999bb2018-03-13 15:22:58 +00002181 MBEDTLS_SSL_DEBUG_MSG( 1,
2182 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002183 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2185 }
2186
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002187 ssl->in_left += ret;
2188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 }
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
2193 return( 0 );
2194}
2195
2196/*
2197 * Flush any data not yet written
2198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200{
Janos Follath865b3eb2019-12-16 11:46:15 +00002201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002202 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002206 if( ssl->f_send == NULL )
2207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002209 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002211 }
2212
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002213 /* Avoid incrementing counter if data is flushed */
2214 if( ssl->out_left == 0 )
2215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002217 return( 0 );
2218 }
2219
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 while( ssl->out_left > 0 )
2221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002223 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224
Hanno Becker2b1e3542018-08-06 11:19:13 +01002225 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002226 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
2230 if( ret <= 0 )
2231 return( ret );
2232
makise-homuraaf9513b2020-08-24 18:26:27 +03002233 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002234 {
Darryl Green11999bb2018-03-13 15:22:58 +00002235 MBEDTLS_SSL_DEBUG_MSG( 1,
2236 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002237 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2239 }
2240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 ssl->out_left -= ret;
2242 }
2243
Hanno Becker2b1e3542018-08-06 11:19:13 +01002244#if defined(MBEDTLS_SSL_PROTO_DTLS)
2245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002246 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002247 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002248 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002249 else
2250#endif
2251 {
2252 ssl->out_hdr = ssl->out_buf + 8;
2253 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002254 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 return( 0 );
2259}
2260
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002261/*
2262 * Functions to handle the DTLS retransmission state machine
2263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002265/*
2266 * Append current handshake message to current outgoing flight
2267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2272 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2273 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002274
2275 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002276 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002277 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002280 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002281 }
2282
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002283 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002284 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002287 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002288 }
2289
2290 /* Copy current handshake message with headers */
2291 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2292 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002293 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002294 msg->next = NULL;
2295
2296 /* Append to the current flight */
2297 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002298 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002299 else
2300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002302 while( cur->next != NULL )
2303 cur = cur->next;
2304 cur->next = msg;
2305 }
2306
Hanno Becker3b235902018-08-06 09:54:53 +01002307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002308 return( 0 );
2309}
2310
2311/*
2312 * Free the current flight of handshake messages
2313 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002314void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 mbedtls_ssl_flight_item *cur = flight;
2317 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002318
2319 while( cur != NULL )
2320 {
2321 next = cur->next;
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 mbedtls_free( cur->p );
2324 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002325
2326 cur = next;
2327 }
2328}
2329
2330/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002331 * Swap transform_out and out_ctr with the alternative ones
2332 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002333static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002336 unsigned char tmp_out_ctr[8];
2337
2338 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002341 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002342 }
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002345
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002346 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002347 tmp_transform = ssl->transform_out;
2348 ssl->transform_out = ssl->handshake->alt_transform_out;
2349 ssl->handshake->alt_transform_out = tmp_transform;
2350
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002351 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002352 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2353 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002354 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002355
2356 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002357 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2360 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002361 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002362 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2363 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2366 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002367 }
2368 }
2369#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002370
2371 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002372}
2373
2374/*
2375 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002376 */
2377int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2378{
2379 int ret = 0;
2380
2381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2382
2383 ret = mbedtls_ssl_flight_transmit( ssl );
2384
2385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2386
2387 return( ret );
2388}
2389
2390/*
2391 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002392 *
2393 * Need to remember the current message in case flush_output returns
2394 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002395 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002396 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002397int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002398{
Janos Follath865b3eb2019-12-16 11:46:15 +00002399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002403 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002405
2406 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002407 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002408 ret = ssl_swap_epochs( ssl );
2409 if( ret != 0 )
2410 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002413 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002414
2415 while( ssl->handshake->cur_msg != NULL )
2416 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002417 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002418 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002419
Hanno Beckere1dcb032018-08-17 16:47:58 +01002420 int const is_finished =
2421 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2422 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2423
Hanno Becker04da1892018-08-14 13:22:10 +01002424 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2425 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2426
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002427 /* Swap epochs before sending Finished: we can't do it after
2428 * sending ChangeCipherSpec, in case write returns WANT_READ.
2429 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002430 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002431 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002433 ret = ssl_swap_epochs( ssl );
2434 if( ret != 0 )
2435 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002436 }
2437
Hanno Becker67bc7c32018-08-06 11:33:50 +01002438 ret = ssl_get_remaining_payload_in_datagram( ssl );
2439 if( ret < 0 )
2440 return( ret );
2441 max_frag_len = (size_t) ret;
2442
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002443 /* CCS is copied as is, while HS messages may need fragmentation */
2444 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2445 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002446 if( max_frag_len == 0 )
2447 {
2448 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2449 return( ret );
2450
2451 continue;
2452 }
2453
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002454 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002455 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002456 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002457
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002458 /* Update position inside current message */
2459 ssl->handshake->cur_msg_p += cur->len;
2460 }
2461 else
2462 {
2463 const unsigned char * const p = ssl->handshake->cur_msg_p;
2464 const size_t hs_len = cur->len - 12;
2465 const size_t frag_off = p - ( cur->p + 12 );
2466 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002467 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002468
Hanno Beckere1dcb032018-08-17 16:47:58 +01002469 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002470 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002471 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002472 {
2473 ret = ssl_swap_epochs( ssl );
2474 if( ret != 0 )
2475 return( ret );
2476 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002477
Hanno Becker67bc7c32018-08-06 11:33:50 +01002478 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2479 return( ret );
2480
2481 continue;
2482 }
2483 max_hs_frag_len = max_frag_len - 12;
2484
2485 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2486 max_hs_frag_len : rem_len;
2487
2488 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002489 {
2490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002491 (unsigned) cur_hs_frag_len,
2492 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002493 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002494
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002495 /* Messages are stored with handshake headers as if not fragmented,
2496 * copy beginning of headers then fill fragmentation fields.
2497 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2498 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002499
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002500 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2501 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2502 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2503
Hanno Becker67bc7c32018-08-06 11:33:50 +01002504 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2505 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2506 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002507
2508 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2509
Hanno Becker3f7b9732018-08-28 09:53:25 +01002510 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002511 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2512 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002513 ssl->out_msgtype = cur->type;
2514
2515 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002516 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002517 }
2518
2519 /* If done with the current message move to the next one if any */
2520 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2521 {
2522 if( cur->next != NULL )
2523 {
2524 ssl->handshake->cur_msg = cur->next;
2525 ssl->handshake->cur_msg_p = cur->next->p + 12;
2526 }
2527 else
2528 {
2529 ssl->handshake->cur_msg = NULL;
2530 ssl->handshake->cur_msg_p = NULL;
2531 }
2532 }
2533
2534 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002535 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002538 return( ret );
2539 }
2540 }
2541
Hanno Becker67bc7c32018-08-06 11:33:50 +01002542 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2543 return( ret );
2544
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002545 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2547 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002548 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002551 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002552 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002553
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002555
2556 return( 0 );
2557}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002558
2559/*
2560 * To be called when the last message of an incoming flight is received.
2561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002563{
2564 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002565 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002566 ssl->handshake->flight = NULL;
2567 ssl->handshake->cur_msg = NULL;
2568
2569 /* The next incoming flight will start with this msg_seq */
2570 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2571
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002572 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002573 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002574
Hanno Becker0271f962018-08-16 13:23:47 +01002575 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002576 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002577
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002578 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002579 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2582 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002585 }
2586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002588}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002589
2590/*
2591 * To be called when the last message of an outgoing flight is send.
2592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002594{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002595 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002596 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2599 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002602 }
2603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002607
Paul Bakker5121ce52009-01-03 21:22:43 +00002608/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002609 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002611
2612/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002613 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002614 *
2615 * - fill in handshake headers
2616 * - update handshake checksum
2617 * - DTLS: save message for resending
2618 * - then pass to the record layer
2619 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002620 * DTLS: except for HelloRequest, messages are only queued, and will only be
2621 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002622 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002623 * Inputs:
2624 * - ssl->out_msglen: 4 + actual handshake message len
2625 * (4 is the size of handshake headers for TLS)
2626 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2627 * - ssl->out_msg + 4: the handshake message body
2628 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002629 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002630 * - ssl->out_msglen: the length of the record contents
2631 * (including handshake headers but excluding record headers)
2632 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002633 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002634int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635{
Janos Follath865b3eb2019-12-16 11:46:15 +00002636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002637 const size_t hs_len = ssl->out_msglen - 4;
2638 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2641
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002642 /*
2643 * Sanity checks
2644 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002645 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002646 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2647 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002648 /* In SSLv3, the client might send a NoCertificate alert. */
2649#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2650 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2651 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2652 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2653#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2654 {
2655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2657 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002660 /* Whenever we send anything different from a
2661 * HelloRequest we should be in a handshake - double check. */
2662 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2663 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002664 ssl->handshake == NULL )
2665 {
2666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2667 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002671 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002672 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002674 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002677 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002678#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002679
Hanno Beckerb50a2532018-08-06 11:52:54 +01002680 /* Double-check that we did not exceed the bounds
2681 * of the outgoing record buffer.
2682 * This should never fail as the various message
2683 * writing functions must obey the bounds of the
2684 * outgoing record buffer, but better be safe.
2685 *
2686 * Note: We deliberately do not check for the MTU or MFL here.
2687 */
2688 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2689 {
2690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2691 "size %u, maximum %u",
2692 (unsigned) ssl->out_msglen,
2693 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2695 }
2696
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002697 /*
2698 * Fill handshake headers
2699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002702 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2703 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2704 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002706 /*
2707 * DTLS has additional fields in the Handshake layer,
2708 * between the length field and the actual payload:
2709 * uint16 message_seq;
2710 * uint24 fragment_offset;
2711 * uint24 fragment_length;
2712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002714 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002715 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002716 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002717 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002718 {
2719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2720 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002721 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002722 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2724 }
2725
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002726 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002727 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002728
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002729 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002730 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002731 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002732 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2733 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2734 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002735 }
2736 else
2737 {
2738 ssl->out_msg[4] = 0;
2739 ssl->out_msg[5] = 0;
2740 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002741
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002742 /* Handshake hashes are computed without fragmentation,
2743 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002744 memset( ssl->out_msg + 6, 0x00, 3 );
2745 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002746 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002748
Hanno Becker0207e532018-08-28 10:28:28 +01002749 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002750 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2751 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002754 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002757 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2758 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002759 {
2760 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002763 return( ret );
2764 }
2765 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002766 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002767#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002768 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002769 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002770 {
2771 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2772 return( ret );
2773 }
2774 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002775
2776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2777
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002778 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002779}
2780
2781/*
2782 * Record layer functions
2783 */
2784
2785/*
2786 * Write current record.
2787 *
2788 * Uses:
2789 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2790 * - ssl->out_msglen: length of the record content (excl headers)
2791 * - ssl->out_msg: record content
2792 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002793int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002794{
2795 int ret, done = 0;
2796 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002797 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002798
2799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002802 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002804 {
2805 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002808 return( ret );
2809 }
2810
2811 len = ssl->out_msglen;
2812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2816 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 ret = mbedtls_ssl_hw_record_write( ssl );
2821 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2824 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002825 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002826
2827 if( ret == 0 )
2828 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002831 if( !done )
2832 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002833 unsigned i;
2834 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002835#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2836 size_t out_buf_len = ssl->out_buf_len;
2837#else
2838 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2839#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002840 /* Skip writing the record content type to after the encryption,
2841 * as it may change when using the CID extension. */
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002844 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002845
Hanno Becker19859472018-08-06 09:40:20 +01002846 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002847 ssl->out_len[0] = (unsigned char)( len >> 8 );
2848 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002849
Paul Bakker48916f92012-09-16 19:57:18 +00002850 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002851 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002852 mbedtls_record rec;
2853
2854 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002855 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002856 rec.data_len = ssl->out_msglen;
2857 rec.data_offset = ssl->out_msg - rec.buf;
2858
2859 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2860 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2861 ssl->conf->transport, rec.ver );
2862 rec.type = ssl->out_msgtype;
2863
Hanno Beckera0e20d02019-05-15 14:03:01 +01002864#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002865 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002866 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002867#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002868
Hanno Beckera18d1322018-01-03 14:27:32 +00002869 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002870 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002873 return( ret );
2874 }
2875
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002876 if( rec.data_offset != 0 )
2877 {
2878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2880 }
2881
Hanno Becker6430faf2019-05-08 11:57:13 +01002882 /* Update the record content type and CID. */
2883 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002884#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002885 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002886#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002887 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002888 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2889 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002890 }
2891
Hanno Becker5903de42019-05-03 14:46:38 +01002892 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002893
2894#if defined(MBEDTLS_SSL_PROTO_DTLS)
2895 /* In case of DTLS, double-check that we don't exceed
2896 * the remaining space in the datagram. */
2897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2898 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002899 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002900 if( ret < 0 )
2901 return( ret );
2902
2903 if( protected_record_size > (size_t) ret )
2904 {
2905 /* Should never happen */
2906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2907 }
2908 }
2909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002910
Hanno Becker6430faf2019-05-08 11:57:13 +01002911 /* Now write the potentially updated record content type. */
2912 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002915 "version = [%d:%d], msglen = %d",
2916 ssl->out_hdr[0], ssl->out_hdr[1],
2917 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002920 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002921
2922 ssl->out_left += protected_record_size;
2923 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002924 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002925
Hanno Beckerdd772292020-02-05 10:38:31 +00002926 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002927 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2928 break;
2929
2930 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002931 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002932 {
2933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2934 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
2937
Hanno Becker67bc7c32018-08-06 11:33:50 +01002938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002939 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2940 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002941 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002942 size_t remaining;
2943 ret = ssl_get_remaining_payload_in_datagram( ssl );
2944 if( ret < 0 )
2945 {
2946 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2947 ret );
2948 return( ret );
2949 }
2950
2951 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002952 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002953 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002954 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002955 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002956 else
2957 {
Hanno Becker513815a2018-08-20 11:56:09 +01002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002959 }
2960 }
2961#endif /* MBEDTLS_SSL_PROTO_DTLS */
2962
2963 if( ( flush == SSL_FORCE_FLUSH ) &&
2964 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002967 return( ret );
2968 }
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
2972 return( 0 );
2973}
2974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002976
2977static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2978{
2979 if( ssl->in_msglen < ssl->in_hslen ||
2980 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2981 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2982 {
2983 return( 1 );
2984 }
2985 return( 0 );
2986}
Hanno Becker44650b72018-08-16 12:51:11 +01002987
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002988static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002989{
2990 return( ( ssl->in_msg[9] << 16 ) |
2991 ( ssl->in_msg[10] << 8 ) |
2992 ssl->in_msg[11] );
2993}
2994
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002995static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002996{
2997 return( ( ssl->in_msg[6] << 16 ) |
2998 ( ssl->in_msg[7] << 8 ) |
2999 ssl->in_msg[8] );
3000}
3001
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003002static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003003{
3004 uint32_t msg_len, frag_off, frag_len;
3005
3006 msg_len = ssl_get_hs_total_len( ssl );
3007 frag_off = ssl_get_hs_frag_off( ssl );
3008 frag_len = ssl_get_hs_frag_len( ssl );
3009
3010 if( frag_off > msg_len )
3011 return( -1 );
3012
3013 if( frag_len > msg_len - frag_off )
3014 return( -1 );
3015
3016 if( frag_len + 12 > ssl->in_msglen )
3017 return( -1 );
3018
3019 return( 0 );
3020}
3021
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003022/*
3023 * Mark bits in bitmask (used for DTLS HS reassembly)
3024 */
3025static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3026{
3027 unsigned int start_bits, end_bits;
3028
3029 start_bits = 8 - ( offset % 8 );
3030 if( start_bits != 8 )
3031 {
3032 size_t first_byte_idx = offset / 8;
3033
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003034 /* Special case */
3035 if( len <= start_bits )
3036 {
3037 for( ; len != 0; len-- )
3038 mask[first_byte_idx] |= 1 << ( start_bits - len );
3039
3040 /* Avoid potential issues with offset or len becoming invalid */
3041 return;
3042 }
3043
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003044 offset += start_bits; /* Now offset % 8 == 0 */
3045 len -= start_bits;
3046
3047 for( ; start_bits != 0; start_bits-- )
3048 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3049 }
3050
3051 end_bits = len % 8;
3052 if( end_bits != 0 )
3053 {
3054 size_t last_byte_idx = ( offset + len ) / 8;
3055
3056 len -= end_bits; /* Now len % 8 == 0 */
3057
3058 for( ; end_bits != 0; end_bits-- )
3059 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3060 }
3061
3062 memset( mask + offset / 8, 0xFF, len / 8 );
3063}
3064
3065/*
3066 * Check that bitmask is full
3067 */
3068static int ssl_bitmask_check( unsigned char *mask, size_t len )
3069{
3070 size_t i;
3071
3072 for( i = 0; i < len / 8; i++ )
3073 if( mask[i] != 0xFF )
3074 return( -1 );
3075
3076 for( i = 0; i < len % 8; i++ )
3077 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3078 return( -1 );
3079
3080 return( 0 );
3081}
3082
Hanno Becker56e205e2018-08-16 09:06:12 +01003083/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003084static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003085 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003086{
Hanno Becker56e205e2018-08-16 09:06:12 +01003087 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003088
Hanno Becker56e205e2018-08-16 09:06:12 +01003089 alloc_len = 12; /* Handshake header */
3090 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003091
Hanno Beckerd07df862018-08-16 09:14:58 +01003092 if( add_bitmap )
3093 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003094
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003095 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003096}
Hanno Becker56e205e2018-08-16 09:06:12 +01003097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003099
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003101{
3102 return( ( ssl->in_msg[1] << 16 ) |
3103 ( ssl->in_msg[2] << 8 ) |
3104 ssl->in_msg[3] );
3105}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003106
Simon Butcher99000142016-10-13 17:21:01 +01003107int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003112 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003114 }
3115
Hanno Becker12555c62018-08-16 12:47:53 +01003116 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003119 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003120 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003123 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003124 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003126 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003127
Hanno Becker44650b72018-08-16 12:51:11 +01003128 if( ssl_check_hs_header( ssl ) != 0 )
3129 {
3130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3132 }
3133
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003134 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003135 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3136 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3137 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3138 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003139 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003140 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3141 {
3142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3143 recv_msg_seq,
3144 ssl->handshake->in_msg_seq ) );
3145 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3146 }
3147
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003148 /* Retransmit only on last message from previous flight, to avoid
3149 * too many retransmissions.
3150 * Besides, No sane server ever retransmits HelloVerifyRequest */
3151 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003155 "message_seq = %d, start_of_flight = %d",
3156 recv_msg_seq,
3157 ssl->handshake->in_flight_start_seq ) );
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003162 return( ret );
3163 }
3164 }
3165 else
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003168 "message_seq = %d, expected = %d",
3169 recv_msg_seq,
3170 ssl->handshake->in_msg_seq ) );
3171 }
3172
Hanno Becker90333da2017-10-10 11:27:13 +01003173 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003174 }
3175 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003176
Hanno Becker6d97ef52018-08-16 13:09:04 +01003177 /* Message reassembly is handled alongside buffering of future
3178 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003179 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003180 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003181 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003184 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003185 }
3186 }
3187 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003189 /* With TLS we don't handle fragmentation (for now) */
3190 if( ssl->in_msglen < ssl->in_hslen )
3191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3193 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003194 }
3195
Simon Butcher99000142016-10-13 17:21:01 +01003196 return( 0 );
3197}
3198
3199void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3200{
Hanno Becker0271f962018-08-16 13:23:47 +01003201 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003202
Hanno Becker0271f962018-08-16 13:23:47 +01003203 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003204 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003205 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003206 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003207
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003208 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003211 ssl->handshake != NULL )
3212 {
Hanno Becker0271f962018-08-16 13:23:47 +01003213 unsigned offset;
3214 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003215
Hanno Becker0271f962018-08-16 13:23:47 +01003216 /* Increment handshake sequence number */
3217 hs->in_msg_seq++;
3218
3219 /*
3220 * Clear up handshake buffering and reassembly structure.
3221 */
3222
3223 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003224 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003225
3226 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003227 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3228 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003229 offset++, hs_buf++ )
3230 {
3231 *hs_buf = *(hs_buf + 1);
3232 }
3233
3234 /* Create a fresh last entry */
3235 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003236 }
3237#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003238}
3239
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003240/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003241 * DTLS anti-replay: RFC 6347 4.1.2.6
3242 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003243 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3244 * Bit n is set iff record number in_window_top - n has been seen.
3245 *
3246 * Usually, in_window_top is the last record number seen and the lsb of
3247 * in_window is set. The only exception is the initial state (record number 0
3248 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003251void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003252{
3253 ssl->in_window_top = 0;
3254 ssl->in_window = 0;
3255}
3256
3257static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3258{
3259 return( ( (uint64_t) buf[0] << 40 ) |
3260 ( (uint64_t) buf[1] << 32 ) |
3261 ( (uint64_t) buf[2] << 24 ) |
3262 ( (uint64_t) buf[3] << 16 ) |
3263 ( (uint64_t) buf[4] << 8 ) |
3264 ( (uint64_t) buf[5] ) );
3265}
3266
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003267static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3268{
Janos Follath865b3eb2019-12-16 11:46:15 +00003269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003270 unsigned char *original_in_ctr;
3271
3272 // save original in_ctr
3273 original_in_ctr = ssl->in_ctr;
3274
3275 // use counter from record
3276 ssl->in_ctr = record_in_ctr;
3277
3278 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3279
3280 // restore the counter
3281 ssl->in_ctr = original_in_ctr;
3282
3283 return ret;
3284}
3285
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003286/*
3287 * Return 0 if sequence number is acceptable, -1 otherwise
3288 */
Hanno Becker0183d692019-07-12 08:50:37 +01003289int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003290{
3291 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3292 uint64_t bit;
3293
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003294 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003295 return( 0 );
3296
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003297 if( rec_seqnum > ssl->in_window_top )
3298 return( 0 );
3299
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003300 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003301
3302 if( bit >= 64 )
3303 return( -1 );
3304
3305 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3306 return( -1 );
3307
3308 return( 0 );
3309}
3310
3311/*
3312 * Update replay window on new validated record
3313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003315{
3316 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003319 return;
3320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003321 if( rec_seqnum > ssl->in_window_top )
3322 {
3323 /* Update window_top and the contents of the window */
3324 uint64_t shift = rec_seqnum - ssl->in_window_top;
3325
3326 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003327 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003328 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003329 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003330 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003331 ssl->in_window |= 1;
3332 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003333
3334 ssl->in_window_top = rec_seqnum;
3335 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003336 else
3337 {
3338 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003339 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003340
3341 if( bit < 64 ) /* Always true, but be extra sure */
3342 ssl->in_window |= (uint64_t) 1 << bit;
3343 }
3344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003346
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003347#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003348/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003349 * Without any SSL context, check if a datagram looks like a ClientHello with
3350 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003351 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003352 *
3353 * - if cookie is valid, return 0
3354 * - if ClientHello looks superficially valid but cookie is not,
3355 * fill obuf and set olen, then
3356 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3357 * - otherwise return a specific error code
3358 */
3359static int ssl_check_dtls_clihlo_cookie(
3360 mbedtls_ssl_cookie_write_t *f_cookie_write,
3361 mbedtls_ssl_cookie_check_t *f_cookie_check,
3362 void *p_cookie,
3363 const unsigned char *cli_id, size_t cli_id_len,
3364 const unsigned char *in, size_t in_len,
3365 unsigned char *obuf, size_t buf_len, size_t *olen )
3366{
3367 size_t sid_len, cookie_len;
3368 unsigned char *p;
3369
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003370 /*
3371 * Structure of ClientHello with record and handshake headers,
3372 * and expected values. We don't need to check a lot, more checks will be
3373 * done when actually parsing the ClientHello - skipping those checks
3374 * avoids code duplication and does not make cookie forging any easier.
3375 *
3376 * 0-0 ContentType type; copied, must be handshake
3377 * 1-2 ProtocolVersion version; copied
3378 * 3-4 uint16 epoch; copied, must be 0
3379 * 5-10 uint48 sequence_number; copied
3380 * 11-12 uint16 length; (ignored)
3381 *
3382 * 13-13 HandshakeType msg_type; (ignored)
3383 * 14-16 uint24 length; (ignored)
3384 * 17-18 uint16 message_seq; copied
3385 * 19-21 uint24 fragment_offset; copied, must be 0
3386 * 22-24 uint24 fragment_length; (ignored)
3387 *
3388 * 25-26 ProtocolVersion client_version; (ignored)
3389 * 27-58 Random random; (ignored)
3390 * 59-xx SessionID session_id; 1 byte len + sid_len content
3391 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3392 * ...
3393 *
3394 * Minimum length is 61 bytes.
3395 */
3396 if( in_len < 61 ||
3397 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3398 in[3] != 0 || in[4] != 0 ||
3399 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3400 {
3401 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3402 }
3403
3404 sid_len = in[59];
3405 if( sid_len > in_len - 61 )
3406 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3407
3408 cookie_len = in[60 + sid_len];
3409 if( cookie_len > in_len - 60 )
3410 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3411
3412 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3413 cli_id, cli_id_len ) == 0 )
3414 {
3415 /* Valid cookie */
3416 return( 0 );
3417 }
3418
3419 /*
3420 * If we get here, we've got an invalid cookie, let's prepare HVR.
3421 *
3422 * 0-0 ContentType type; copied
3423 * 1-2 ProtocolVersion version; copied
3424 * 3-4 uint16 epoch; copied
3425 * 5-10 uint48 sequence_number; copied
3426 * 11-12 uint16 length; olen - 13
3427 *
3428 * 13-13 HandshakeType msg_type; hello_verify_request
3429 * 14-16 uint24 length; olen - 25
3430 * 17-18 uint16 message_seq; copied
3431 * 19-21 uint24 fragment_offset; copied
3432 * 22-24 uint24 fragment_length; olen - 25
3433 *
3434 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3435 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3436 *
3437 * Minimum length is 28.
3438 */
3439 if( buf_len < 28 )
3440 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3441
3442 /* Copy most fields and adapt others */
3443 memcpy( obuf, in, 25 );
3444 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3445 obuf[25] = 0xfe;
3446 obuf[26] = 0xff;
3447
3448 /* Generate and write actual cookie */
3449 p = obuf + 28;
3450 if( f_cookie_write( p_cookie,
3451 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3452 {
3453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3454 }
3455
3456 *olen = p - obuf;
3457
3458 /* Go back and fill length fields */
3459 obuf[27] = (unsigned char)( *olen - 28 );
3460
3461 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3462 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3463 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3464
3465 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3466 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3467
3468 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3469}
3470
3471/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003472 * Handle possible client reconnect with the same UDP quadruplet
3473 * (RFC 6347 Section 4.2.8).
3474 *
3475 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3476 * that looks like a ClientHello.
3477 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003478 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003479 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003480 * - if the input looks like a ClientHello with a valid cookie,
3481 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003482 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003483 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003484 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003485 * This function is called (through ssl_check_client_reconnect()) when an
3486 * unexpected record is found in ssl_get_next_record(), which will discard the
3487 * record if we return 0, and bubble up the return value otherwise (this
3488 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3489 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003490 */
3491static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3492{
Janos Follath865b3eb2019-12-16 11:46:15 +00003493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003494 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003495
Hanno Becker2fddd372019-07-10 14:37:41 +01003496 if( ssl->conf->f_cookie_write == NULL ||
3497 ssl->conf->f_cookie_check == NULL )
3498 {
3499 /* If we can't use cookies to verify reachability of the peer,
3500 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3502 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003503 return( 0 );
3504 }
3505
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003506 ret = ssl_check_dtls_clihlo_cookie(
3507 ssl->conf->f_cookie_write,
3508 ssl->conf->f_cookie_check,
3509 ssl->conf->p_cookie,
3510 ssl->cli_id, ssl->cli_id_len,
3511 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003512 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003513
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003514 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3515
3516 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003517 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003518 int send_ret;
3519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3520 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3521 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003522 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003523 * If the error is permanent we'll catch it later,
3524 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003525 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3526 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3527 (void) send_ret;
3528
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003529 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003530 }
3531
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003532 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003533 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003535 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003536 {
3537 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3538 return( ret );
3539 }
3540
3541 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003542 }
3543
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003544 return( ret );
3545}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003546#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003547
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003548static int ssl_check_record_type( uint8_t record_type )
3549{
3550 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3551 record_type != MBEDTLS_SSL_MSG_ALERT &&
3552 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3553 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3554 {
3555 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3556 }
3557
3558 return( 0 );
3559}
3560
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003561/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003562 * ContentType type;
3563 * ProtocolVersion version;
3564 * uint16 epoch; // DTLS only
3565 * uint48 sequence_number; // DTLS only
3566 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003567 *
3568 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003569 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003570 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3571 *
3572 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003573 * 1. proceed with the record if this function returns 0
3574 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3575 * 3. return CLIENT_RECONNECT if this function return that value
3576 * 4. drop the whole datagram if this function returns anything else.
3577 * Point 2 is needed when the peer is resending, and we have already received
3578 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003579 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003580static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003581 unsigned char *buf,
3582 size_t len,
3583 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003584{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003585 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Hanno Beckere5e7e782019-07-11 12:29:35 +01003587 size_t const rec_hdr_type_offset = 0;
3588 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003589
Hanno Beckere5e7e782019-07-11 12:29:35 +01003590 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3591 rec_hdr_type_len;
3592 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003593
Hanno Beckere5e7e782019-07-11 12:29:35 +01003594 size_t const rec_hdr_ctr_len = 8;
3595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003596 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003597 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3598 rec_hdr_version_len;
3599
Hanno Beckera0e20d02019-05-15 14:03:01 +01003600#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003601 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3602 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003603 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003604#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3605#endif /* MBEDTLS_SSL_PROTO_DTLS */
3606
3607 size_t rec_hdr_len_offset; /* To be determined */
3608 size_t const rec_hdr_len_len = 2;
3609
3610 /*
3611 * Check minimum lengths for record header.
3612 */
3613
3614#if defined(MBEDTLS_SSL_PROTO_DTLS)
3615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3616 {
3617 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3618 }
3619 else
3620#endif /* MBEDTLS_SSL_PROTO_DTLS */
3621 {
3622 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3623 }
3624
3625 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3626 {
3627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3628 (unsigned) len,
3629 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3630 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3631 }
3632
3633 /*
3634 * Parse and validate record content type
3635 */
3636
3637 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003638
3639 /* Check record content type */
3640#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3641 rec->cid_len = 0;
3642
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003644 ssl->conf->cid_len != 0 &&
3645 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003646 {
3647 /* Shift pointers to account for record header including CID
3648 * struct {
3649 * ContentType special_type = tls12_cid;
3650 * ProtocolVersion version;
3651 * uint16 epoch;
3652 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003653 * opaque cid[cid_length]; // Additional field compared to
3654 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003655 * uint16 length;
3656 * opaque enc_content[DTLSCiphertext.length];
3657 * } DTLSCiphertext;
3658 */
3659
3660 /* So far, we only support static CID lengths
3661 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003662 rec_hdr_cid_len = ssl->conf->cid_len;
3663 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003664
Hanno Beckere5e7e782019-07-11 12:29:35 +01003665 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003666 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3668 (unsigned) len,
3669 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003671 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003672
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003673 /* configured CID len is guaranteed at most 255, see
3674 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3675 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003676 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003677 }
3678 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003679#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003680 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003681 if( ssl_check_record_type( rec->type ) )
3682 {
Hanno Becker54229812019-07-12 14:40:00 +01003683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3684 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003685 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3686 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003687 }
3688
Hanno Beckere5e7e782019-07-11 12:29:35 +01003689 /*
3690 * Parse and validate record version
3691 */
3692
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003693 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3694 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003695 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3696 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003697 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003698
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003699 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3702 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 }
3704
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003705 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3708 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 }
3710
Hanno Beckere5e7e782019-07-11 12:29:35 +01003711 /*
3712 * Parse/Copy record sequence number.
3713 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003714
Hanno Beckere5e7e782019-07-11 12:29:35 +01003715#if defined(MBEDTLS_SSL_PROTO_DTLS)
3716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003717 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003718 /* Copy explicit record sequence number from input buffer. */
3719 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3720 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003721 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003722 else
3723#endif /* MBEDTLS_SSL_PROTO_DTLS */
3724 {
3725 /* Copy implicit record sequence number from SSL context structure. */
3726 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3727 }
Paul Bakker40e46942009-01-03 21:51:57 +00003728
Hanno Beckere5e7e782019-07-11 12:29:35 +01003729 /*
3730 * Parse record length.
3731 */
3732
Hanno Beckere5e7e782019-07-11 12:29:35 +01003733 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003734 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3735 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003736 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003739 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003740 rec->type,
3741 major_ver, minor_ver, rec->data_len ) );
3742
3743 rec->buf = buf;
3744 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003745
Hanno Beckerd417cc92019-07-26 08:20:27 +01003746 if( rec->data_len == 0 )
3747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003749 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003750 * DTLS-related tests.
3751 * Check epoch before checking length constraint because
3752 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3753 * message gets duplicated before the corresponding Finished message,
3754 * the second ChangeCipherSpec should be discarded because it belongs
3755 * to an old epoch, but not because its length is shorter than
3756 * the minimum record length for packets using the new record transform.
3757 * Note that these two kinds of failures are handled differently,
3758 * as an unexpected record is silently skipped but an invalid
3759 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003760 */
3761#if defined(MBEDTLS_SSL_PROTO_DTLS)
3762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3763 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003764 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003765
Hanno Becker955a5c92019-07-10 17:12:07 +01003766 /* Check that the datagram is large enough to contain a record
3767 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003768 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003769 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3771 (unsigned) len,
3772 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3774 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003775
Hanno Becker37cfe732019-07-10 17:20:01 +01003776 /* Records from other, non-matching epochs are silently discarded.
3777 * (The case of same-port Client reconnects must be considered in
3778 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003779 if( rec_epoch != ssl->in_epoch )
3780 {
3781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3782 "expected %d, received %d",
3783 ssl->in_epoch, rec_epoch ) );
3784
Hanno Becker552f7472019-07-19 10:59:12 +01003785 /* Records from the next epoch are considered for buffering
3786 * (concretely: early Finished messages). */
3787 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003788 {
Hanno Becker552f7472019-07-19 10:59:12 +01003789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3790 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003791 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003792
Hanno Becker2fddd372019-07-10 14:37:41 +01003793 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003794 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003795#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003796 /* For records from the correct epoch, check whether their
3797 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003798 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3799 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003800 {
3801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3802 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3803 }
3804#endif
3805 }
3806#endif /* MBEDTLS_SSL_PROTO_DTLS */
3807
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003808 return( 0 );
3809}
Paul Bakker5121ce52009-01-03 21:22:43 +00003810
Paul Bakker5121ce52009-01-03 21:22:43 +00003811
Hanno Becker2fddd372019-07-10 14:37:41 +01003812#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3813static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3814{
3815 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3816
3817 /*
3818 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3819 * access the first byte of record content (handshake type), as we
3820 * have an active transform (possibly iv_len != 0), so use the
3821 * fact that the record header len is 13 instead.
3822 */
3823 if( rec_epoch == 0 &&
3824 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3825 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3826 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3827 ssl->in_left > 13 &&
3828 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3829 {
3830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3831 "from the same port" ) );
3832 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 }
3834
3835 return( 0 );
3836}
Hanno Becker2fddd372019-07-10 14:37:41 +01003837#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003839/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003840 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003841 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003842static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3843 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003844{
3845 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003848 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3851 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 ret = mbedtls_ssl_hw_record_read( ssl );
3856 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3859 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003860 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003861
3862 if( ret == 0 )
3863 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003866 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003867 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003868 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003869
Hanno Beckera18d1322018-01-03 14:27:32 +00003870 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003871 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003874
Hanno Beckera0e20d02019-05-15 14:03:01 +01003875#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003876 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3877 ssl->conf->ignore_unexpected_cid
3878 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3879 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003881 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003882 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003883#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003884
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 return( ret );
3886 }
3887
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003888 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003889 {
3890 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003891 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003892 }
3893
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003894 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003895 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003896
Hanno Beckera0e20d02019-05-15 14:03:01 +01003897#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003898 /* We have already checked the record content type
3899 * in ssl_parse_record_header(), failing or silently
3900 * dropping the record in the case of an unknown type.
3901 *
3902 * Since with the use of CIDs, the record content type
3903 * might change during decryption, re-check the record
3904 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003905 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3908 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3909 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003911
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003912 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003913 {
3914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003916 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003917 {
3918 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3920 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3921 }
3922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3923
3924 ssl->nb_zero++;
3925
3926 /*
3927 * Three or more empty messages may be a DoS attack
3928 * (excessive CPU consumption).
3929 */
3930 if( ssl->nb_zero > 3 )
3931 {
3932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003933 "messages, possible DoS attack" ) );
3934 /* Treat the records as if they were not properly authenticated,
3935 * thereby failing the connection if we see more than allowed
3936 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003937 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3938 }
3939 }
3940 else
3941 ssl->nb_zero = 0;
3942
3943#if defined(MBEDTLS_SSL_PROTO_DTLS)
3944 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3945 {
3946 ; /* in_ctr read from peer, not maintained internally */
3947 }
3948 else
3949#endif
3950 {
3951 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003952 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003953 if( ++ssl->in_ctr[i - 1] != 0 )
3954 break;
3955
3956 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003957 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003958 {
3959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3960 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3961 }
3962 }
3963
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 }
3965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003967 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003970 }
3971#endif
3972
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003973 /* Check actual (decrypted) record content length against
3974 * configured maximum. */
3975 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3976 {
3977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3978 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3979 }
3980
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003981 return( 0 );
3982}
3983
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003984/*
3985 * Read a record.
3986 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003987 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3988 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3989 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003990 */
Hanno Becker1097b342018-08-15 14:09:41 +01003991
3992/* Helper functions for mbedtls_ssl_read_record(). */
3993static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003994static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3995static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003996
Hanno Becker327c93b2018-08-15 13:56:18 +01003997int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003998 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003999{
Janos Follath865b3eb2019-12-16 11:46:15 +00004000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004003
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004004 if( ssl->keep_current_message == 0 )
4005 {
4006 do {
Simon Butcher99000142016-10-13 17:21:01 +01004007
Hanno Becker26994592018-08-15 14:14:59 +01004008 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004009 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004010 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004011
Hanno Beckere74d5562018-08-15 14:26:08 +01004012 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004013 {
Hanno Becker40f50842018-08-15 14:48:01 +01004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
4015 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004016
Hanno Becker40f50842018-08-15 14:48:01 +01004017 /* We only check for buffered messages if the
4018 * current datagram is fully consumed. */
4019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004020 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004021 {
Hanno Becker40f50842018-08-15 14:48:01 +01004022 if( ssl_load_buffered_message( ssl ) == 0 )
4023 have_buffered = 1;
4024 }
4025
4026 if( have_buffered == 0 )
4027#endif /* MBEDTLS_SSL_PROTO_DTLS */
4028 {
4029 ret = ssl_get_next_record( ssl );
4030 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4031 continue;
4032
4033 if( ret != 0 )
4034 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004035 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004036 return( ret );
4037 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004038 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004039 }
4040
4041 ret = mbedtls_ssl_handle_message_type( ssl );
4042
Hanno Becker40f50842018-08-15 14:48:01 +01004043#if defined(MBEDTLS_SSL_PROTO_DTLS)
4044 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4045 {
4046 /* Buffer future message */
4047 ret = ssl_buffer_message( ssl );
4048 if( ret != 0 )
4049 return( ret );
4050
4051 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4052 }
4053#endif /* MBEDTLS_SSL_PROTO_DTLS */
4054
Hanno Becker90333da2017-10-10 11:27:13 +01004055 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4056 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004057
4058 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004059 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004060 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004061 return( ret );
4062 }
4063
Hanno Becker327c93b2018-08-15 13:56:18 +01004064 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004065 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004066 {
4067 mbedtls_ssl_update_handshake_status( ssl );
4068 }
Simon Butcher99000142016-10-13 17:21:01 +01004069 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004070 else
Simon Butcher99000142016-10-13 17:21:01 +01004071 {
Hanno Becker02f59072018-08-15 14:00:24 +01004072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004073 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004074 }
4075
4076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4077
4078 return( 0 );
4079}
4080
Hanno Becker40f50842018-08-15 14:48:01 +01004081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004082static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004083{
Hanno Becker40f50842018-08-15 14:48:01 +01004084 if( ssl->in_left > ssl->next_record_offset )
4085 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004086
Hanno Becker40f50842018-08-15 14:48:01 +01004087 return( 0 );
4088}
4089
4090static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4091{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004092 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004093 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004094 int ret = 0;
4095
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004096 if( hs == NULL )
4097 return( -1 );
4098
Hanno Beckere00ae372018-08-20 09:39:42 +01004099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4100
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004101 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4102 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4103 {
4104 /* Check if we have seen a ChangeCipherSpec before.
4105 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004106 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004107 {
4108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4109 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004110 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004111 }
4112
Hanno Becker39b8bc92018-08-28 17:17:13 +01004113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004114 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4115 ssl->in_msglen = 1;
4116 ssl->in_msg[0] = 1;
4117
4118 /* As long as they are equal, the exact value doesn't matter. */
4119 ssl->in_left = 0;
4120 ssl->next_record_offset = 0;
4121
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004122 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004123 goto exit;
4124 }
Hanno Becker37f95322018-08-16 13:55:32 +01004125
Hanno Beckerb8f50142018-08-28 10:01:34 +01004126#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004127 /* Debug only */
4128 {
4129 unsigned offset;
4130 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4131 {
4132 hs_buf = &hs->buffering.hs[offset];
4133 if( hs_buf->is_valid == 1 )
4134 {
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4136 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004137 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004138 }
4139 }
4140 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004141#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004142
4143 /* Check if we have buffered and/or fully reassembled the
4144 * next handshake message. */
4145 hs_buf = &hs->buffering.hs[0];
4146 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4147 {
4148 /* Synthesize a record containing the buffered HS message. */
4149 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4150 ( hs_buf->data[2] << 8 ) |
4151 hs_buf->data[3];
4152
4153 /* Double-check that we haven't accidentally buffered
4154 * a message that doesn't fit into the input buffer. */
4155 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4159 }
4160
4161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4162 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4163 hs_buf->data, msg_len + 12 );
4164
4165 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4166 ssl->in_hslen = msg_len + 12;
4167 ssl->in_msglen = msg_len + 12;
4168 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4169
4170 ret = 0;
4171 goto exit;
4172 }
4173 else
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4176 hs->in_msg_seq ) );
4177 }
4178
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004179 ret = -1;
4180
4181exit:
4182
4183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4184 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004185}
4186
Hanno Beckera02b0b42018-08-21 17:20:27 +01004187static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4188 size_t desired )
4189{
4190 int offset;
4191 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4193 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004194
Hanno Becker01315ea2018-08-21 17:22:17 +01004195 /* Get rid of future records epoch first, if such exist. */
4196 ssl_free_buffered_record( ssl );
4197
4198 /* Check if we have enough space available now. */
4199 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4200 hs->buffering.total_bytes_buffered ) )
4201 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004203 return( 0 );
4204 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004205
Hanno Becker4f432ad2018-08-28 10:02:32 +01004206 /* We don't have enough space to buffer the next expected handshake
4207 * message. Remove buffers used for future messages to gain space,
4208 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004209 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4210 offset >= 0; offset-- )
4211 {
4212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4213 offset ) );
4214
Hanno Beckerb309b922018-08-23 13:18:05 +01004215 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004216
4217 /* Check if we have enough space available now. */
4218 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4219 hs->buffering.total_bytes_buffered ) )
4220 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004222 return( 0 );
4223 }
4224 }
4225
4226 return( -1 );
4227}
4228
Hanno Becker40f50842018-08-15 14:48:01 +01004229static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4230{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004231 int ret = 0;
4232 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4233
4234 if( hs == NULL )
4235 return( 0 );
4236
4237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4238
4239 switch( ssl->in_msgtype )
4240 {
4241 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004243
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004244 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004245 break;
4246
4247 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004248 {
4249 unsigned recv_msg_seq_offset;
4250 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4251 mbedtls_ssl_hs_buffer *hs_buf;
4252 size_t msg_len = ssl->in_hslen - 12;
4253
4254 /* We should never receive an old handshake
4255 * message - double-check nonetheless. */
4256 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4257 {
4258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4260 }
4261
4262 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4263 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4264 {
4265 /* Silently ignore -- message too far in the future */
4266 MBEDTLS_SSL_DEBUG_MSG( 2,
4267 ( "Ignore future HS message with sequence number %u, "
4268 "buffering window %u - %u",
4269 recv_msg_seq, ssl->handshake->in_msg_seq,
4270 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4271
4272 goto exit;
4273 }
4274
4275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4276 recv_msg_seq, recv_msg_seq_offset ) );
4277
4278 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4279
4280 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004281 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004282 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004283 size_t reassembly_buf_sz;
4284
Hanno Becker37f95322018-08-16 13:55:32 +01004285 hs_buf->is_fragmented =
4286 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4287
4288 /* We copy the message back into the input buffer
4289 * after reassembly, so check that it's not too large.
4290 * This is an implementation-specific limitation
4291 * and not one from the standard, hence it is not
4292 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004293 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004294 {
4295 /* Ignore message */
4296 goto exit;
4297 }
4298
Hanno Beckere0b150f2018-08-21 15:51:03 +01004299 /* Check if we have enough space to buffer the message. */
4300 if( hs->buffering.total_bytes_buffered >
4301 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4302 {
4303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4305 }
4306
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004307 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4308 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004309
4310 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4311 hs->buffering.total_bytes_buffered ) )
4312 {
4313 if( recv_msg_seq_offset > 0 )
4314 {
4315 /* If we can't buffer a future message because
4316 * of space limitations -- ignore. */
4317 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",
4318 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4319 (unsigned) hs->buffering.total_bytes_buffered ) );
4320 goto exit;
4321 }
Hanno Beckere1801392018-08-21 16:51:05 +01004322 else
4323 {
4324 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",
4325 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4326 (unsigned) hs->buffering.total_bytes_buffered ) );
4327 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004328
Hanno Beckera02b0b42018-08-21 17:20:27 +01004329 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004330 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004331 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",
4332 (unsigned) msg_len,
4333 (unsigned) reassembly_buf_sz,
4334 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004335 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004336 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4337 goto exit;
4338 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004339 }
4340
4341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4342 msg_len ) );
4343
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004344 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4345 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004346 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004347 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004348 goto exit;
4349 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004350 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004351
4352 /* Prepare final header: copy msg_type, length and message_seq,
4353 * then add standardised fragment_offset and fragment_length */
4354 memcpy( hs_buf->data, ssl->in_msg, 6 );
4355 memset( hs_buf->data + 6, 0, 3 );
4356 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4357
4358 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004359
4360 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004361 }
4362 else
4363 {
4364 /* Make sure msg_type and length are consistent */
4365 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4366 {
4367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4368 /* Ignore */
4369 goto exit;
4370 }
4371 }
4372
Hanno Becker4422bbb2018-08-20 09:40:19 +01004373 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004374 {
4375 size_t frag_len, frag_off;
4376 unsigned char * const msg = hs_buf->data + 12;
4377
4378 /*
4379 * Check and copy current fragment
4380 */
4381
4382 /* Validation of header fields already done in
4383 * mbedtls_ssl_prepare_handshake_record(). */
4384 frag_off = ssl_get_hs_frag_off( ssl );
4385 frag_len = ssl_get_hs_frag_len( ssl );
4386
4387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4388 frag_off, frag_len ) );
4389 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4390
4391 if( hs_buf->is_fragmented )
4392 {
4393 unsigned char * const bitmask = msg + msg_len;
4394 ssl_bitmask_set( bitmask, frag_off, frag_len );
4395 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4396 msg_len ) == 0 );
4397 }
4398 else
4399 {
4400 hs_buf->is_complete = 1;
4401 }
4402
4403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4404 hs_buf->is_complete ? "" : "not yet " ) );
4405 }
4406
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004407 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004408 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004409
4410 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004411 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004412 break;
4413 }
4414
4415exit:
4416
4417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4418 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004419}
4420#endif /* MBEDTLS_SSL_PROTO_DTLS */
4421
Hanno Becker1097b342018-08-15 14:09:41 +01004422static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004423{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004424 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004425 * Consume last content-layer message and potentially
4426 * update in_msglen which keeps track of the contents'
4427 * consumption state.
4428 *
4429 * (1) Handshake messages:
4430 * Remove last handshake message, move content
4431 * and adapt in_msglen.
4432 *
4433 * (2) Alert messages:
4434 * Consume whole record content, in_msglen = 0.
4435 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004436 * (3) Change cipher spec:
4437 * Consume whole record content, in_msglen = 0.
4438 *
4439 * (4) Application data:
4440 * Don't do anything - the record layer provides
4441 * the application data as a stream transport
4442 * and consumes through mbedtls_ssl_read only.
4443 *
4444 */
4445
4446 /* Case (1): Handshake messages */
4447 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004448 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004449 /* Hard assertion to be sure that no application data
4450 * is in flight, as corrupting ssl->in_msglen during
4451 * ssl->in_offt != NULL is fatal. */
4452 if( ssl->in_offt != NULL )
4453 {
4454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4456 }
4457
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004458 /*
4459 * Get next Handshake message in the current record
4460 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004461
Hanno Becker4a810fb2017-05-24 16:27:30 +01004462 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004463 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004464 * current handshake content: If DTLS handshake
4465 * fragmentation is used, that's the fragment
4466 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004467 * size here is faulty and should be changed at
4468 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004469 * (2) While it doesn't seem to cause problems, one
4470 * has to be very careful not to assume that in_hslen
4471 * is always <= in_msglen in a sensible communication.
4472 * Again, it's wrong for DTLS handshake fragmentation.
4473 * The following check is therefore mandatory, and
4474 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004475 * Additionally, ssl->in_hslen might be arbitrarily out of
4476 * bounds after handling a DTLS message with an unexpected
4477 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004478 */
4479 if( ssl->in_hslen < ssl->in_msglen )
4480 {
4481 ssl->in_msglen -= ssl->in_hslen;
4482 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4483 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004484
Hanno Becker4a810fb2017-05-24 16:27:30 +01004485 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4486 ssl->in_msg, ssl->in_msglen );
4487 }
4488 else
4489 {
4490 ssl->in_msglen = 0;
4491 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004492
Hanno Becker4a810fb2017-05-24 16:27:30 +01004493 ssl->in_hslen = 0;
4494 }
4495 /* Case (4): Application data */
4496 else if( ssl->in_offt != NULL )
4497 {
4498 return( 0 );
4499 }
4500 /* Everything else (CCS & Alerts) */
4501 else
4502 {
4503 ssl->in_msglen = 0;
4504 }
4505
Hanno Becker1097b342018-08-15 14:09:41 +01004506 return( 0 );
4507}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004508
Hanno Beckere74d5562018-08-15 14:26:08 +01004509static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4510{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004511 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004512 return( 1 );
4513
4514 return( 0 );
4515}
4516
Hanno Becker5f066e72018-08-16 14:56:31 +01004517#if defined(MBEDTLS_SSL_PROTO_DTLS)
4518
4519static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4520{
4521 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4522 if( hs == NULL )
4523 return;
4524
Hanno Becker01315ea2018-08-21 17:22:17 +01004525 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004526 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004527 hs->buffering.total_bytes_buffered -=
4528 hs->buffering.future_record.len;
4529
4530 mbedtls_free( hs->buffering.future_record.data );
4531 hs->buffering.future_record.data = NULL;
4532 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004533}
4534
4535static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4536{
4537 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4538 unsigned char * rec;
4539 size_t rec_len;
4540 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004541#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4542 size_t in_buf_len = ssl->in_buf_len;
4543#else
4544 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4545#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004546 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4547 return( 0 );
4548
4549 if( hs == NULL )
4550 return( 0 );
4551
Hanno Becker5f066e72018-08-16 14:56:31 +01004552 rec = hs->buffering.future_record.data;
4553 rec_len = hs->buffering.future_record.len;
4554 rec_epoch = hs->buffering.future_record.epoch;
4555
4556 if( rec == NULL )
4557 return( 0 );
4558
Hanno Becker4cb782d2018-08-20 11:19:05 +01004559 /* Only consider loading future records if the
4560 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004561 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004562 return( 0 );
4563
Hanno Becker5f066e72018-08-16 14:56:31 +01004564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4565
4566 if( rec_epoch != ssl->in_epoch )
4567 {
4568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4569 goto exit;
4570 }
4571
4572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4573
4574 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004575 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004576 {
4577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4579 }
4580
4581 memcpy( ssl->in_hdr, rec, rec_len );
4582 ssl->in_left = rec_len;
4583 ssl->next_record_offset = 0;
4584
4585 ssl_free_buffered_record( ssl );
4586
4587exit:
4588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4589 return( 0 );
4590}
4591
Hanno Becker519f15d2019-07-11 12:43:20 +01004592static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4593 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004594{
4595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004596
4597 /* Don't buffer future records outside handshakes. */
4598 if( hs == NULL )
4599 return( 0 );
4600
4601 /* Only buffer handshake records (we are only interested
4602 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004603 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004604 return( 0 );
4605
4606 /* Don't buffer more than one future epoch record. */
4607 if( hs->buffering.future_record.data != NULL )
4608 return( 0 );
4609
Hanno Becker01315ea2018-08-21 17:22:17 +01004610 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004611 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004612 hs->buffering.total_bytes_buffered ) )
4613 {
4614 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 +01004615 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004616 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004617 return( 0 );
4618 }
4619
Hanno Becker5f066e72018-08-16 14:56:31 +01004620 /* Buffer record */
4621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4622 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004623 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004624
4625 /* ssl_parse_record_header() only considers records
4626 * of the next epoch as candidates for buffering. */
4627 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004628 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004629
4630 hs->buffering.future_record.data =
4631 mbedtls_calloc( 1, hs->buffering.future_record.len );
4632 if( hs->buffering.future_record.data == NULL )
4633 {
4634 /* If we run out of RAM trying to buffer a
4635 * record from the next epoch, just ignore. */
4636 return( 0 );
4637 }
4638
Hanno Becker519f15d2019-07-11 12:43:20 +01004639 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004640
Hanno Becker519f15d2019-07-11 12:43:20 +01004641 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004642 return( 0 );
4643}
4644
4645#endif /* MBEDTLS_SSL_PROTO_DTLS */
4646
Hanno Beckere74d5562018-08-15 14:26:08 +01004647static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004648{
Janos Follath865b3eb2019-12-16 11:46:15 +00004649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004650 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004651
Hanno Becker5f066e72018-08-16 14:56:31 +01004652#if defined(MBEDTLS_SSL_PROTO_DTLS)
4653 /* We might have buffered a future record; if so,
4654 * and if the epoch matches now, load it.
4655 * On success, this call will set ssl->in_left to
4656 * the length of the buffered record, so that
4657 * the calls to ssl_fetch_input() below will
4658 * essentially be no-ops. */
4659 ret = ssl_load_buffered_record( ssl );
4660 if( ret != 0 )
4661 return( ret );
4662#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004663
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004664 /* Ensure that we have enough space available for the default form
4665 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4666 * with no space for CIDs counted in). */
4667 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4668 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004671 return( ret );
4672 }
4673
Hanno Beckere5e7e782019-07-11 12:29:35 +01004674 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4675 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004678 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004679 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4681 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004682 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004683 if( ret != 0 )
4684 return( ret );
4685
4686 /* Fall through to handling of unexpected records */
4687 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4688 }
4689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4691 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004692#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004693 /* Reset in pointers to default state for TLS/DTLS records,
4694 * assuming no CID and no offset between record content and
4695 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004696 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004697
Hanno Becker7ae20e02019-07-12 08:33:49 +01004698 /* Setup internal message pointers from record structure. */
4699 ssl->in_msgtype = rec.type;
4700#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4701 ssl->in_len = ssl->in_cid + rec.cid_len;
4702#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4703 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4704 ssl->in_msglen = rec.data_len;
4705
Hanno Becker2fddd372019-07-10 14:37:41 +01004706 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004707 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004708 if( ret != 0 )
4709 return( ret );
4710#endif
4711
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004713 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004714
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4716 "(header)" ) );
4717 }
4718 else
4719 {
4720 /* Skip invalid record and the rest of the datagram */
4721 ssl->next_record_offset = 0;
4722 ssl->in_left = 0;
4723
4724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4725 "(header)" ) );
4726 }
4727
4728 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004729 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004730 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004731 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004732#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004733 {
4734 return( ret );
4735 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004736 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004740 {
Hanno Beckera8814792019-07-10 15:01:45 +01004741 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004742 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004743 if( ssl->next_record_offset < ssl->in_left )
4744 {
4745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4746 }
4747 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004748 else
4749#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004750 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004751 /*
4752 * Fetch record contents from underlying transport.
4753 */
Hanno Beckera3175662019-07-11 12:50:29 +01004754 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004755 if( ret != 0 )
4756 {
4757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4758 return( ret );
4759 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004760
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004761 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004762 }
4763
4764 /*
4765 * Decrypt record contents.
4766 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004767
Hanno Beckerfdf66042019-07-11 13:07:45 +01004768 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004772 {
4773 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004774 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004775 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004776 /* Except when waiting for Finished as a bad mac here
4777 * probably means something went wrong in the handshake
4778 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4779 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4780 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4781 {
4782#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4783 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4784 {
4785 mbedtls_ssl_send_alert_message( ssl,
4786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4787 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4788 }
4789#endif
4790 return( ret );
4791 }
4792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004794 if( ssl->conf->badmac_limit != 0 &&
4795 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4798 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004799 }
4800#endif
4801
Hanno Becker4a810fb2017-05-24 16:27:30 +01004802 /* As above, invalid records cause
4803 * dismissal of the whole datagram. */
4804
4805 ssl->next_record_offset = 0;
4806 ssl->in_left = 0;
4807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004809 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004810 }
4811
4812 return( ret );
4813 }
4814 else
4815#endif
4816 {
4817 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4819 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 mbedtls_ssl_send_alert_message( ssl,
4822 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4823 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004824 }
4825#endif
4826 return( ret );
4827 }
4828 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004829
Hanno Becker44d89b22019-07-12 09:40:44 +01004830
4831 /* Reset in pointers to default state for TLS/DTLS records,
4832 * assuming no CID and no offset between record content and
4833 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004834 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004835#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4836 ssl->in_len = ssl->in_cid + rec.cid_len;
4837#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004838 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004839
Hanno Becker8685c822019-07-12 09:37:30 +01004840 /* The record content type may change during decryption,
4841 * so re-read it. */
4842 ssl->in_msgtype = rec.type;
4843 /* Also update the input buffer, because unfortunately
4844 * the server-side ssl_parse_client_hello() reparses the
4845 * record header when receiving a ClientHello initiating
4846 * a renegotiation. */
4847 ssl->in_hdr[0] = rec.type;
4848 ssl->in_msg = rec.buf + rec.data_offset;
4849 ssl->in_msglen = rec.data_len;
4850 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4851 ssl->in_len[1] = (unsigned char)( rec.data_len );
4852
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004853#if defined(MBEDTLS_ZLIB_SUPPORT)
4854 if( ssl->transform_in != NULL &&
4855 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4856 {
4857 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4858 {
4859 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4860 return( ret );
4861 }
4862
4863 /* Check actual (decompress) record content length against
4864 * configured maximum. */
4865 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4866 {
4867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4869 }
4870 }
4871#endif /* MBEDTLS_ZLIB_SUPPORT */
4872
Simon Butcher99000142016-10-13 17:21:01 +01004873 return( 0 );
4874}
4875
4876int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4877{
Janos Follath865b3eb2019-12-16 11:46:15 +00004878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004879
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004880 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004881 * Handle particular types of records
4882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 {
Simon Butcher99000142016-10-13 17:21:01 +01004885 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4886 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004887 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004888 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 }
4890
Hanno Beckere678eaa2018-08-21 14:57:46 +01004891 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004892 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004893 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004894 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4896 ssl->in_msglen ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004898 }
4899
Hanno Beckere678eaa2018-08-21 14:57:46 +01004900 if( ssl->in_msg[0] != 1 )
4901 {
4902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4903 ssl->in_msg[0] ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4905 }
4906
4907#if defined(MBEDTLS_SSL_PROTO_DTLS)
4908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4909 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4910 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4911 {
4912 if( ssl->handshake == NULL )
4913 {
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4915 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4916 }
4917
4918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4919 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4920 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004921#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004922 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004926 if( ssl->in_msglen != 2 )
4927 {
4928 /* Note: Standard allows for more than one 2 byte alert
4929 to be packed in a single message, but Mbed TLS doesn't
4930 currently support this. */
4931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4932 ssl->in_msglen ) );
4933 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4934 }
4935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 ssl->in_msg[0], ssl->in_msg[1] ) );
4938
4939 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004940 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004945 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004947 }
4948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4950 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4953 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004954 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004955
4956#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4957 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4958 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4959 {
Hanno Becker90333da2017-10-10 11:27:13 +01004960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004961 /* Will be handled when trying to parse ServerHello */
4962 return( 0 );
4963 }
4964#endif
4965
4966#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4967 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4968 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4969 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4970 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4971 {
4972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4973 /* Will be handled in mbedtls_ssl_parse_certificate() */
4974 return( 0 );
4975 }
4976#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4977
4978 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004979 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004980 }
4981
Hanno Beckerc76c6192017-06-06 10:03:17 +01004982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004984 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004985 /* Drop unexpected ApplicationData records,
4986 * except at the beginning of renegotiations */
4987 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4988 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4989#if defined(MBEDTLS_SSL_RENEGOTIATION)
4990 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4991 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004992#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004993 )
4994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4996 return( MBEDTLS_ERR_SSL_NON_FATAL );
4997 }
4998
4999 if( ssl->handshake != NULL &&
5000 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5001 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005002 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01005003 }
5004 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01005005#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005006
Paul Bakker5121ce52009-01-03 21:22:43 +00005007 return( 0 );
5008}
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005011{
irwir6c0da642019-09-26 21:07:41 +03005012 return( mbedtls_ssl_send_alert_message( ssl,
5013 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5014 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005015}
5016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005018 unsigned char level,
5019 unsigned char message )
5020{
Janos Follath865b3eb2019-12-16 11:46:15 +00005021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00005022
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005023 if( ssl == NULL || ssl->conf == NULL )
5024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005030 ssl->out_msglen = 2;
5031 ssl->out_msg[0] = level;
5032 ssl->out_msg[1] = message;
5033
Hanno Becker67bc7c32018-08-06 11:33:50 +01005034 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005037 return( ret );
5038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005040
5041 return( 0 );
5042}
5043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005045{
Janos Follath865b3eb2019-12-16 11:46:15 +00005046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005051 ssl->out_msglen = 1;
5052 ssl->out_msg[0] = 1;
5053
Paul Bakker5121ce52009-01-03 21:22:43 +00005054 ssl->state++;
5055
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005056 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 return( ret );
5060 }
5061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005063
5064 return( 0 );
5065}
5066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005068{
Janos Follath865b3eb2019-12-16 11:46:15 +00005069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005072
Hanno Becker327c93b2018-08-15 13:56:18 +01005073 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005076 return( ret );
5077 }
5078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5083 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005085 }
5086
Hanno Beckere678eaa2018-08-21 14:57:46 +01005087 /* CCS records are only accepted if they have length 1 and content '1',
5088 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005089
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005090 /*
5091 * Switch to our negotiated transform and session parameters for inbound
5092 * data.
5093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005095 ssl->transform_in = ssl->transform_negotiate;
5096 ssl->session_in = ssl->session_negotiate;
5097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00005102 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005103#endif
5104
5105 /* Increment epoch */
5106 if( ++ssl->in_epoch == 0 )
5107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005109 /* This is highly unlikely to happen for legitimate reasons, so
5110 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005112 }
5113 }
5114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005116 memset( ssl->in_ctr, 0, 8 );
5117
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005118 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5121 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5127 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005129 }
5130 }
5131#endif
5132
Paul Bakker5121ce52009-01-03 21:22:43 +00005133 ssl->state++;
5134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136
5137 return( 0 );
5138}
5139
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005140/* Once ssl->out_hdr as the address of the beginning of the
5141 * next outgoing record is set, deduce the other pointers.
5142 *
5143 * Note: For TLS, we save the implicit record sequence number
5144 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5145 * and the caller has to make sure there's space for this.
5146 */
5147
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005148static size_t ssl_transform_get_explicit_iv_len(
5149 mbedtls_ssl_transform const *transform )
5150{
5151 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5152 return( 0 );
5153
5154 return( transform->ivlen - transform->fixed_ivlen );
5155}
5156
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005157void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5158 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005159{
5160#if defined(MBEDTLS_SSL_PROTO_DTLS)
5161 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5162 {
5163 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005164#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005165 ssl->out_cid = ssl->out_ctr + 8;
5166 ssl->out_len = ssl->out_cid;
5167 if( transform != NULL )
5168 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005169#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005170 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005171#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005172 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005173 }
5174 else
5175#endif
5176 {
5177 ssl->out_ctr = ssl->out_hdr - 8;
5178 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005180 ssl->out_cid = ssl->out_len;
5181#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005182 ssl->out_iv = ssl->out_hdr + 5;
5183 }
5184
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005185 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005186 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005187 if( transform != NULL )
5188 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005189}
5190
5191/* Once ssl->in_hdr as the address of the beginning of the
5192 * next incoming record is set, deduce the other pointers.
5193 *
5194 * Note: For TLS, we save the implicit record sequence number
5195 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5196 * and the caller has to make sure there's space for this.
5197 */
5198
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005199void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005200{
Hanno Becker79594fd2019-05-08 09:38:41 +01005201 /* This function sets the pointers to match the case
5202 * of unprotected TLS/DTLS records, with both ssl->in_iv
5203 * and ssl->in_msg pointing to the beginning of the record
5204 * content.
5205 *
5206 * When decrypting a protected record, ssl->in_msg
5207 * will be shifted to point to the beginning of the
5208 * record plaintext.
5209 */
5210
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005211#if defined(MBEDTLS_SSL_PROTO_DTLS)
5212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5213 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005214 /* This sets the header pointers to match records
5215 * without CID. When we receive a record containing
5216 * a CID, the fields are shifted accordingly in
5217 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005218 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005219#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005220 ssl->in_cid = ssl->in_ctr + 8;
5221 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005222#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005223 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005224#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005225 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005226 }
5227 else
5228#endif
5229 {
5230 ssl->in_ctr = ssl->in_hdr - 8;
5231 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005232#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005233 ssl->in_cid = ssl->in_len;
5234#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005235 ssl->in_iv = ssl->in_hdr + 5;
5236 }
5237
Hanno Becker79594fd2019-05-08 09:38:41 +01005238 /* This will be adjusted at record decryption time. */
5239 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005240}
5241
Paul Bakker5121ce52009-01-03 21:22:43 +00005242/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005243 * Setup an SSL context
5244 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005245
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005246void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005247{
5248 /* Set the incoming and outgoing record pointers. */
5249#if defined(MBEDTLS_SSL_PROTO_DTLS)
5250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5251 {
5252 ssl->out_hdr = ssl->out_buf;
5253 ssl->in_hdr = ssl->in_buf;
5254 }
5255 else
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257 {
5258 ssl->out_hdr = ssl->out_buf + 8;
5259 ssl->in_hdr = ssl->in_buf + 8;
5260 }
5261
5262 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005263 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5264 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005265}
5266
Paul Bakker5121ce52009-01-03 21:22:43 +00005267/*
5268 * SSL get accessors
5269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271{
5272 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5273}
5274
Hanno Becker8b170a02017-10-10 11:51:19 +01005275int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5276{
5277 /*
5278 * Case A: We're currently holding back
5279 * a message for further processing.
5280 */
5281
5282 if( ssl->keep_current_message == 1 )
5283 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005285 return( 1 );
5286 }
5287
5288 /*
5289 * Case B: Further records are pending in the current datagram.
5290 */
5291
5292#if defined(MBEDTLS_SSL_PROTO_DTLS)
5293 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5294 ssl->in_left > ssl->next_record_offset )
5295 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005297 return( 1 );
5298 }
5299#endif /* MBEDTLS_SSL_PROTO_DTLS */
5300
5301 /*
5302 * Case C: A handshake message is being processed.
5303 */
5304
Hanno Becker8b170a02017-10-10 11:51:19 +01005305 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5306 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005308 return( 1 );
5309 }
5310
5311 /*
5312 * Case D: An application data message is being processed
5313 */
5314 if( ssl->in_offt != NULL )
5315 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005317 return( 1 );
5318 }
5319
5320 /*
5321 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005322 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005323 * we implement support for multiple alerts in single records.
5324 */
5325
5326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5327 return( 0 );
5328}
5329
Paul Bakker43ca69c2011-01-15 17:35:19 +00005330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005332{
Hanno Becker3136ede2018-08-17 15:28:19 +01005333 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005335 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005336
Hanno Becker5903de42019-05-03 14:46:38 +01005337 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5338
Hanno Becker78640902018-08-13 16:35:15 +01005339 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005340 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if defined(MBEDTLS_ZLIB_SUPPORT)
5343 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5344 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005345#endif
5346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 case MBEDTLS_MODE_GCM:
5350 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005351 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005353 transform_expansion = transform->minlen;
5354 break;
5355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005357
5358 block_size = mbedtls_cipher_get_block_size(
5359 &transform->cipher_ctx_enc );
5360
Hanno Becker3136ede2018-08-17 15:28:19 +01005361 /* Expansion due to the addition of the MAC. */
5362 transform_expansion += transform->maclen;
5363
5364 /* Expansion due to the addition of CBC padding;
5365 * Theoretically up to 256 bytes, but we never use
5366 * more than the block size of the underlying cipher. */
5367 transform_expansion += block_size;
5368
5369 /* For TLS 1.1 or higher, an explicit IV is added
5370 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005371#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5372 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005373 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005374#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005375
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005376 break;
5377
5378 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005381 }
5382
Hanno Beckera0e20d02019-05-15 14:03:01 +01005383#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005384 if( transform->out_cid_len != 0 )
5385 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005386#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005387
Hanno Becker5903de42019-05-03 14:46:38 +01005388 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005389}
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005392/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005393 * Check record counters and renegotiate if they're above the limit.
5394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005396{
Hanno Beckerdd772292020-02-05 10:38:31 +00005397 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005398 int in_ctr_cmp;
5399 int out_ctr_cmp;
5400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5402 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005403 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005404 {
5405 return( 0 );
5406 }
5407
Andres AG2196c7f2016-12-15 17:01:16 +00005408 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5409 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005410 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005411 ssl->conf->renego_period + ep_len, 8 - ep_len );
5412
5413 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005414 {
5415 return( 0 );
5416 }
5417
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005422
5423/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005424 * Receive application data decrypted from the SSL layer
5425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005427{
Janos Follath865b3eb2019-12-16 11:46:15 +00005428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005429 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005430
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005431 if( ssl == NULL || ssl->conf == NULL )
5432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005440 return( ret );
5441
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005442 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005444 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005445 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005446 return( ret );
5447 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005448 }
5449#endif
5450
Hanno Becker4a810fb2017-05-24 16:27:30 +01005451 /*
5452 * Check if renegotiation is necessary and/or handshake is
5453 * in process. If yes, perform/continue, and fall through
5454 * if an unexpected packet is received while the client
5455 * is waiting for the ServerHello.
5456 *
5457 * (There is no equivalent to the last condition on
5458 * the server-side as it is not treated as within
5459 * a handshake while waiting for the ClientHello
5460 * after a renegotiation request.)
5461 */
5462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005464 ret = ssl_check_ctr_renegotiate( ssl );
5465 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5466 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005469 return( ret );
5470 }
5471#endif
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005476 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5477 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 return( ret );
5481 }
5482 }
5483
Hanno Beckere41158b2017-10-23 13:30:32 +01005484 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005485 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005487 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005488 if( ssl->f_get_timer != NULL &&
5489 ssl->f_get_timer( ssl->p_timer ) == -1 )
5490 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005491 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005492 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005493
Hanno Becker327c93b2018-08-15 13:56:18 +01005494 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005495 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5497 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005498
Hanno Becker4a810fb2017-05-24 16:27:30 +01005499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5500 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 }
5502
5503 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 {
5506 /*
5507 * OpenSSL sends empty messages to randomize the IV
5508 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005509 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005512 return( 0 );
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 return( ret );
5516 }
5517 }
5518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005522
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 /*
5524 * - For client-side, expect SERVER_HELLO_REQUEST.
5525 * - For server-side, expect CLIENT_HELLO.
5526 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5527 */
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005532 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005535
5536 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005539 {
5540 continue;
5541 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005544 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005545#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005546
Hanno Becker4a810fb2017-05-24 16:27:30 +01005547#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005548 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005552
5553 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005556 {
5557 continue;
5558 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005561 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005562#endif /* MBEDTLS_SSL_SRV_C */
5563
Hanno Becker21df7f92017-10-17 11:03:26 +01005564#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005565 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005566 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5567 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5568 ssl->conf->allow_legacy_renegotiation ==
5569 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5570 {
5571 /*
5572 * Accept renegotiation request
5573 */
Paul Bakker48916f92012-09-16 19:57:18 +00005574
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005575 /* DTLS clients need to know renego is server-initiated */
5576#if defined(MBEDTLS_SSL_PROTO_DTLS)
5577 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5578 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5579 {
5580 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5581 }
5582#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005583 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005584 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5585 ret != 0 )
5586 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5588 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005589 return( ret );
5590 }
5591 }
5592 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005593#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005594 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005595 /*
5596 * Refuse renegotiation
5597 */
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601#if defined(MBEDTLS_SSL_PROTO_SSL3)
5602 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005603 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005604 /* SSLv3 does not have a "no_renegotiation" warning, so
5605 we send a fatal alert and abort the connection. */
5606 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5607 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5608 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005609 }
5610 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5612#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5613 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5614 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5617 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5618 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005619 {
5620 return( ret );
5621 }
Paul Bakker48916f92012-09-16 19:57:18 +00005622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5625 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005629 }
Paul Bakker48916f92012-09-16 19:57:18 +00005630 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005631
Hanno Becker90333da2017-10-10 11:27:13 +01005632 /* At this point, we don't know whether the renegotiation has been
5633 * completed or not. The cases to consider are the following:
5634 * 1) The renegotiation is complete. In this case, no new record
5635 * has been read yet.
5636 * 2) The renegotiation is incomplete because the client received
5637 * an application data record while awaiting the ServerHello.
5638 * 3) The renegotiation is incomplete because the client received
5639 * a non-handshake, non-application data message while awaiting
5640 * the ServerHello.
5641 * In each of these case, looping will be the proper action:
5642 * - For 1), the next iteration will read a new record and check
5643 * if it's application data.
5644 * - For 2), the loop condition isn't satisfied as application data
5645 * is present, hence continue is the same as break
5646 * - For 3), the loop condition is satisfied and read_record
5647 * will re-deliver the message that was held back by the client
5648 * when expecting the ServerHello.
5649 */
5650 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005651 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005652#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005654 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005655 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005656 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005657 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005660 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005662 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005663 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5668 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005671 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005672 }
5673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5677 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 }
5679
5680 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005681
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005682 /* We're going to return something now, cancel timer,
5683 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005685 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005686
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005688 /* If we requested renego but received AppData, resend HelloRequest.
5689 * Do it now, after setting in_offt, to avoid taking this branch
5690 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005694 {
Hanno Becker786300f2020-02-05 10:46:40 +00005695 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005696 {
Hanno Becker786300f2020-02-05 10:46:40 +00005697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5698 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005699 return( ret );
5700 }
5701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005703#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 }
5705
5706 n = ( len < ssl->in_msglen )
5707 ? len : ssl->in_msglen;
5708
5709 memcpy( buf, ssl->in_offt, n );
5710 ssl->in_msglen -= n;
5711
gabor-mezei-arma3214132020-07-15 10:55:00 +02005712 /* Zeroising the plaintext buffer to erase unused application data
5713 from the memory. */
5714 mbedtls_platform_zeroize( ssl->in_offt, n );
5715
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005717 {
5718 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005720 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005721 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005722 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005723 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 /* more data available */
5725 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005729
Paul Bakker23986e52011-04-24 08:57:21 +00005730 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005731}
5732
5733/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005734 * Send application data to be encrypted by the SSL layer, taking care of max
5735 * fragment length and buffer size.
5736 *
5737 * According to RFC 5246 Section 6.2.1:
5738 *
5739 * Zero-length fragments of Application data MAY be sent as they are
5740 * potentially useful as a traffic analysis countermeasure.
5741 *
5742 * Therefore, it is possible that the input message length is 0 and the
5743 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005745static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005746 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005747{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005748 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5749 const size_t max_len = (size_t) ret;
5750
5751 if( ret < 0 )
5752 {
5753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5754 return( ret );
5755 }
5756
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005757 if( len > max_len )
5758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005760 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005763 "maximum fragment length: %d > %d",
5764 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005766 }
5767 else
5768#endif
5769 len = max_len;
5770 }
Paul Bakker887bd502011-06-08 13:10:54 +00005771
Paul Bakker5121ce52009-01-03 21:22:43 +00005772 if( ssl->out_left != 0 )
5773 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005774 /*
5775 * The user has previously tried to send the data and
5776 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5777 * written. In this case, we expect the high-level write function
5778 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 return( ret );
5784 }
5785 }
Paul Bakker887bd502011-06-08 13:10:54 +00005786 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005787 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005788 /*
5789 * The user is trying to send a message the first time, so we need to
5790 * copy the data into the internal buffers and setup the data structure
5791 * to keep track of partial writes
5792 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005793 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005795 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005796
Hanno Becker67bc7c32018-08-06 11:33:50 +01005797 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005800 return( ret );
5801 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 }
5803
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005804 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005805}
5806
5807/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005808 * Write application data, doing 1/n-1 splitting if necessary.
5809 *
5810 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005811 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005812 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005815static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005816 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005817{
Janos Follath865b3eb2019-12-16 11:46:15 +00005818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005819
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005820 if( ssl->conf->cbc_record_splitting ==
5821 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005822 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5824 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5825 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005826 {
5827 return( ssl_write_real( ssl, buf, len ) );
5828 }
5829
5830 if( ssl->split_done == 0 )
5831 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005832 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005833 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005834 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005835 }
5836
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005837 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5838 return( ret );
5839 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005840
5841 return( ret + 1 );
5842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005844
5845/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005846 * Write application data (public-facing wrapper)
5847 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005848int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005849{
Janos Follath865b3eb2019-12-16 11:46:15 +00005850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005851
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005853
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005854 if( ssl == NULL || ssl->conf == NULL )
5855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5856
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005857#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005858 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5859 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005860 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005861 return( ret );
5862 }
5863#endif
5864
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005865 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005866 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005867 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005868 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005870 return( ret );
5871 }
5872 }
5873
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005874#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005875 ret = ssl_write_split( ssl, buf, len );
5876#else
5877 ret = ssl_write_real( ssl, buf, len );
5878#endif
5879
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005881
5882 return( ret );
5883}
5884
5885/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 * Notify the peer that the connection is being closed
5887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005889{
Janos Follath865b3eb2019-12-16 11:46:15 +00005890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005891
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005892 if( ssl == NULL || ssl->conf == NULL )
5893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005897 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5903 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5904 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 return( ret );
5908 }
5909 }
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005912
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005913 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005914}
5915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005917{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005918 if( transform == NULL )
5919 return;
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005922 deflateEnd( &transform->ctx_deflate );
5923 inflateEnd( &transform->ctx_inflate );
5924#endif
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5927 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005928
Hanno Beckerd56ed242018-01-03 15:32:51 +00005929#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 mbedtls_md_free( &transform->md_ctx_enc );
5931 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005932#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005933
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005934 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005935}
5936
Hanno Becker0271f962018-08-16 13:23:47 +01005937#if defined(MBEDTLS_SSL_PROTO_DTLS)
5938
Hanno Becker533ab5f2020-02-05 10:49:13 +00005939void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005940{
5941 unsigned offset;
5942 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5943
5944 if( hs == NULL )
5945 return;
5946
Hanno Becker283f5ef2018-08-24 09:34:47 +01005947 ssl_free_buffered_record( ssl );
5948
Hanno Becker0271f962018-08-16 13:23:47 +01005949 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005950 ssl_buffering_free_slot( ssl, offset );
5951}
5952
5953static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5954 uint8_t slot )
5955{
5956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5957 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005958
5959 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5960 return;
5961
Hanno Beckere605b192018-08-21 15:59:07 +01005962 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005963 {
Hanno Beckere605b192018-08-21 15:59:07 +01005964 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005965 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005966 mbedtls_free( hs_buf->data );
5967 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005968 }
5969}
5970
5971#endif /* MBEDTLS_SSL_PROTO_DTLS */
5972
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005973/*
5974 * Convert version numbers to/from wire format
5975 * and, for DTLS, to/from TLS equivalent.
5976 *
5977 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005978 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005979 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5980 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005983 unsigned char ver[2] )
5984{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985#if defined(MBEDTLS_SSL_PROTO_DTLS)
5986 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005989 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5990
5991 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5992 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5993 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005994 else
5995#else
5996 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005997#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005998 {
5999 ver[0] = (unsigned char) major;
6000 ver[1] = (unsigned char) minor;
6001 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006002}
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006005 const unsigned char ver[2] )
6006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007#if defined(MBEDTLS_SSL_PROTO_DTLS)
6008 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006009 {
6010 *major = 255 - ver[0] + 2;
6011 *minor = 255 - ver[1] + 1;
6012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006014 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6015 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006016 else
6017#else
6018 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006019#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006020 {
6021 *major = ver[0];
6022 *minor = ver[1];
6023 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006024}
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#endif /* MBEDTLS_SSL_TLS_C */