blob: 6cee53a89e9f250923e2e6ba209261532ceb2f2b [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 *
Hanno Beckerf1a38282020-02-05 16:14:29 +00005 * Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000019 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000020 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
SimonBd5800b72016-04-26 07:43:27 +010039#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
42#include <stdlib.h>
43#define mbedtls_calloc calloc
44#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010045#endif
46
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000049#include "mbedtls/debug.h"
50#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050051#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010052#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050056#if defined(MBEDTLS_USE_PSA_CRYPTO)
57#include "mbedtls/psa_util.h"
58#include "psa/crypto.h"
59#endif
60
Janos Follath23bdca02016-10-07 14:47:14 +010061#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020063#endif
64
Hanno Beckercd9dcda2018-08-28 17:18:56 +010065static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020067/*
68 * Start a timer.
69 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020070 */
Hanno Becker0f57a652020-02-05 10:37:26 +000071void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020073 if( ssl->f_set_timer == NULL )
74 return;
75
76 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
77 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078}
79
80/*
81 * Return -1 is timer is expired, 0 if it isn't.
82 */
Hanno Becker7876d122020-02-05 10:39:31 +000083int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020085 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020086 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087
88 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020089 {
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020091 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020092 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020093
94 return( 0 );
95}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096
Hanno Beckercfe45792019-07-03 16:13:00 +010097#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010098static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
99 unsigned char *buf,
100 size_t len,
101 mbedtls_record *rec );
102
Hanno Beckercfe45792019-07-03 16:13:00 +0100103int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
104 unsigned char *buf,
105 size_t buflen )
106{
Hanno Becker54229812019-07-12 14:40:00 +0100107 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
109 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
110
111 /* We don't support record checking in TLS because
112 * (a) there doesn't seem to be a usecase for it, and
113 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
114 * and we'd need to backup the transform here.
115 */
116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
117 {
118 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
119 goto exit;
120 }
121#if defined(MBEDTLS_SSL_PROTO_DTLS)
122 else
123 {
irwir734f0cf2019-09-26 21:03:24 +0300124 mbedtls_record rec;
125
Hanno Becker54229812019-07-12 14:40:00 +0100126 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
127 if( ret != 0 )
128 {
129 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
130 goto exit;
131 }
132
133 if( ssl->transform_in != NULL )
134 {
135 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
136 if( ret != 0 )
137 {
138 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
139 goto exit;
140 }
141 }
142 }
143#endif /* MBEDTLS_SSL_PROTO_DTLS */
144
145exit:
146 /* On success, we have decrypted the buffer in-place, so make
147 * sure we don't leak any plaintext data. */
148 mbedtls_platform_zeroize( buf, buflen );
149
150 /* For the purpose of this API, treat messages with unexpected CID
151 * as well as such from future epochs as unexpected. */
152 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
153 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
154 {
155 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
156 }
157
158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
159 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100160}
161#endif /* MBEDTLS_SSL_RECORD_CHECKING */
162
Hanno Becker67bc7c32018-08-06 11:33:50 +0100163#define SSL_DONT_FORCE_FLUSH 0
164#define SSL_FORCE_FLUSH 1
165
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100167
Hanno Beckerd5847772018-08-28 10:09:23 +0100168/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100169static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
170 uint8_t slot );
171static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
172static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
173static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
174static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100175static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
176 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100177static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100178
Hanno Becker11682cc2018-08-22 14:41:02 +0100179static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180{
Hanno Becker89490712020-02-05 10:50:12 +0000181 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182
183 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100184 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100185
186 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
187}
188
Hanno Becker67bc7c32018-08-06 11:33:50 +0100189static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
190{
Hanno Becker11682cc2018-08-22 14:41:02 +0100191 size_t const bytes_written = ssl->out_left;
192 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100193
194 /* Double-check that the write-index hasn't gone
195 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100196 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100197 {
198 /* Should never happen... */
199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
200 }
201
202 return( (int) ( mtu - bytes_written ) );
203}
204
205static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
206{
Janos Follath865b3eb2019-12-16 11:46:15 +0000207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100208 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400209 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210
211#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
212 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
213
214 if( max_len > mfl )
215 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100216
217 /* By the standard (RFC 6066 Sect. 4), the MFL extension
218 * only limits the maximum record payload size, so in theory
219 * we would be allowed to pack multiple records of payload size
220 * MFL into a single datagram. However, this would mean that there's
221 * no way to explicitly communicate MTU restrictions to the peer.
222 *
223 * The following reduction of max_len makes sure that we never
224 * write datagrams larger than MFL + Record Expansion Overhead.
225 */
226 if( max_len <= ssl->out_left )
227 return( 0 );
228
229 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100230#endif
231
232 ret = ssl_get_remaining_space_in_datagram( ssl );
233 if( ret < 0 )
234 return( ret );
235 remaining = (size_t) ret;
236
237 ret = mbedtls_ssl_get_record_expansion( ssl );
238 if( ret < 0 )
239 return( ret );
240 expansion = (size_t) ret;
241
242 if( remaining <= expansion )
243 return( 0 );
244
245 remaining -= expansion;
246 if( remaining >= max_len )
247 remaining = max_len;
248
249 return( (int) remaining );
250}
251
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252/*
253 * Double the retransmit timeout value, within the allowed range,
254 * returning -1 if the maximum value has already been reached.
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200257{
258 uint32_t new_timeout;
259
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200260 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200261 return( -1 );
262
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200263 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
264 * in the following way: after the initial transmission and a first
265 * retransmission, back off to a temporary estimated MTU of 508 bytes.
266 * This value is guaranteed to be deliverable (if not guaranteed to be
267 * delivered) of any compliant IPv4 (and IPv6) network, and should work
268 * on most non-IP stacks too. */
269 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400270 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200271 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
273 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200274
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200275 new_timeout = 2 * ssl->handshake->retransmit_timeout;
276
277 /* Avoid arithmetic overflow and range overflow */
278 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200279 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200280 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200281 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200282 }
283
284 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200286 ssl->handshake->retransmit_timeout ) );
287
288 return( 0 );
289}
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200293 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295 ssl->handshake->retransmit_timeout ) );
296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
300int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200301 const unsigned char *key_enc, const unsigned char *key_dec,
302 size_t keylen,
303 const unsigned char *iv_enc, const unsigned char *iv_dec,
304 size_t ivlen,
305 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200306 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
308int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
309int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
311int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
312#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000313
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200314/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +0100315 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +0000316#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200317 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
318 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
319 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
320/* This function makes sure every byte in the memory region is accessed
321 * (in ascending addresses order) */
322static void ssl_read_memory( unsigned char *p, size_t len )
323{
324 unsigned char acc = 0;
325 volatile unsigned char force;
326
327 for( ; len != 0; p++, len-- )
328 acc ^= *p;
329
330 force = acc;
331 (void) force;
332}
333#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
334
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100335/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200337 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000338
Hanno Beckera0e20d02019-05-15 14:03:01 +0100339#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +0100340/* This functions transforms a DTLS plaintext fragment and a record content
341 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100342 *
343 * struct {
344 * opaque content[DTLSPlaintext.length];
345 * ContentType real_type;
346 * uint8 zeros[length_of_padding];
347 * } DTLSInnerPlaintext;
348 *
349 * Input:
350 * - `content`: The beginning of the buffer holding the
351 * plaintext to be wrapped.
352 * - `*content_size`: The length of the plaintext in Bytes.
353 * - `max_len`: The number of Bytes available starting from
354 * `content`. This must be `>= *content_size`.
355 * - `rec_type`: The desired record content type.
356 *
357 * Output:
358 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
359 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
360 *
361 * Returns:
362 * - `0` on success.
363 * - A negative error code if `max_len` didn't offer enough space
364 * for the expansion.
365 */
366static int ssl_cid_build_inner_plaintext( unsigned char *content,
367 size_t *content_size,
368 size_t remaining,
369 uint8_t rec_type )
370{
371 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +0100372 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
373 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
374 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100375
376 /* Write real content type */
377 if( remaining == 0 )
378 return( -1 );
379 content[ len ] = rec_type;
380 len++;
381 remaining--;
382
383 if( remaining < pad )
384 return( -1 );
385 memset( content + len, 0, pad );
386 len += pad;
387 remaining -= pad;
388
389 *content_size = len;
390 return( 0 );
391}
392
Hanno Becker07dc97d2019-05-20 15:08:01 +0100393/* This function parses a DTLSInnerPlaintext structure.
394 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100395static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
396 size_t *content_size,
397 uint8_t *rec_type )
398{
399 size_t remaining = *content_size;
400
401 /* Determine length of padding by skipping zeroes from the back. */
402 do
403 {
404 if( remaining == 0 )
405 return( -1 );
406 remaining--;
407 } while( content[ remaining ] == 0 );
408
409 *content_size = remaining;
410 *rec_type = content[ remaining ];
411
412 return( 0 );
413}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100415
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100416/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100417 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000418static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100419 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000420 mbedtls_record *rec )
421{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100422 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100423 *
424 * additional_data = seq_num + TLSCompressed.type +
425 * TLSCompressed.version + TLSCompressed.length;
426 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100427 * For the CID extension, this is extended as follows
428 * (quoting draft-ietf-tls-dtls-connection-id-05,
429 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100430 *
431 * additional_data = seq_num + DTLSPlaintext.type +
432 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100433 * cid +
434 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100435 * length_of_DTLSInnerPlaintext;
436 */
437
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000438 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
439 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +0100440 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +0100441
Hanno Beckera0e20d02019-05-15 14:03:01 +0100442#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100443 if( rec->cid_len != 0 )
444 {
445 memcpy( add_data + 11, rec->cid, rec->cid_len );
446 add_data[11 + rec->cid_len + 0] = rec->cid_len;
447 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
448 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
449 *add_data_len = 13 + 1 + rec->cid_len;
450 }
451 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100452#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100453 {
454 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
455 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
456 *add_data_len = 13;
457 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000458}
459
Hanno Beckera18d1322018-01-03 14:27:32 +0000460int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
461 mbedtls_ssl_transform *transform,
462 mbedtls_record *rec,
463 int (*f_rng)(void *, unsigned char *, size_t),
464 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100467 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000468 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100469 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100470 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000471 size_t post_avail;
472
473 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000474#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200475 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000476 ((void) ssl);
477#endif
478
479 /* The PRNG is used for dynamic IV generation that's used
480 * for CBC transformations in TLS 1.1 and TLS 1.2. */
481#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
482 ( defined(MBEDTLS_AES_C) || \
483 defined(MBEDTLS_ARIA_C) || \
484 defined(MBEDTLS_CAMELLIA_C) ) && \
485 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
486 ((void) f_rng);
487 ((void) p_rng);
488#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000492 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100493 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
496 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100497 if( rec == NULL
498 || rec->buf == NULL
499 || rec->buf_len < rec->data_offset
500 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100501#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100502 || rec->cid_len != 0
503#endif
504 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000505 {
506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100508 }
509
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000510 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100511 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000513 data, rec->data_len );
514
515 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
516
517 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
518 {
519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
520 (unsigned) rec->data_len,
521 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
523 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100524
Hanno Beckera0e20d02019-05-15 14:03:01 +0100525#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100526 /*
527 * Add CID information
528 */
529 rec->cid_len = transform->out_cid_len;
530 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
531 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100532
533 if( rec->cid_len != 0 )
534 {
535 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100536 * Wrap plaintext into DTLSInnerPlaintext structure.
537 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100538 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100539 * Note that this changes `rec->data_len`, and hence
540 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100541 */
542 if( ssl_cid_build_inner_plaintext( data,
543 &rec->data_len,
544 post_avail,
545 rec->type ) != 0 )
546 {
547 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
548 }
549
550 rec->type = MBEDTLS_SSL_MSG_CID;
551 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100552#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100553
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100554 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100557 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Hanno Becker52344c22018-01-03 15:24:20 +0000559#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( mode == MBEDTLS_MODE_STREAM ||
561 ( mode == MBEDTLS_MODE_CBC
562#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000563 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100564#endif
565 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000567 if( post_avail < transform->maclen )
568 {
569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
570 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000574 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200575 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +0100576 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000577 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
578 data, rec->data_len, rec->ctr, rec->type, mac );
579 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200580 }
581 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
584 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000585 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200586 {
Hanno Becker992b6872017-11-09 18:57:39 +0000587 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
588
Hanno Beckercab87e62019-04-29 13:52:53 +0100589 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +0000590
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000591 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100592 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000593 mbedtls_md_hmac_update( &transform->md_ctx_enc,
594 data, rec->data_len );
595 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
596 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
597
598 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200599 }
600 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200601#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200605 }
606
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000607 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
608 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200609
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000610 rec->data_len += transform->maclen;
611 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100612 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200613 }
Hanno Becker52344c22018-01-03 15:24:20 +0000614#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200616 /*
617 * Encrypt
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
620 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000623 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000625 "including %d bytes of padding",
626 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000628 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
629 transform->iv_enc, transform->ivlen,
630 data, rec->data_len,
631 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200634 return( ret );
635 }
636
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000637 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200641 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
Paul Bakker68884e32013-01-07 18:20:04 +0100643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000645
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200646#if defined(MBEDTLS_GCM_C) || \
647 defined(MBEDTLS_CCM_C) || \
648 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200650 mode == MBEDTLS_MODE_CCM ||
651 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200654 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000655 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000656
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000657 /* Check that there's space for both the authentication tag
658 * and the explicit IV before and after the record content. */
659 if( post_avail < transform->taglen ||
660 rec->data_offset < explicit_iv_len )
661 {
662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
663 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
664 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665
Paul Bakker68884e32013-01-07 18:20:04 +0100666 /*
667 * Generate IV
668 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200669 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
670 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200671 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200672 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000673 memcpy( iv + transform->fixed_ivlen, rec->ctr,
674 explicit_iv_len );
675 /* Prefix record content with explicit IV. */
676 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200677 }
678 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
679 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200680 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200681 unsigned char i;
682
683 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
684
685 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000686 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200687 }
688 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100689 {
690 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100693 }
694
Hanno Beckercab87e62019-04-29 13:52:53 +0100695 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +0100696
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200697 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
698 iv, transform->ivlen );
699 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000700 data - explicit_iv_len, explicit_iv_len );
701 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100702 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200704 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000705 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000706
Paul Bakker68884e32013-01-07 18:20:04 +0100707 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200708 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200709 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000710
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200711 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000712 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100713 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000714 data, rec->data_len, /* source */
715 data, &rec->data_len, /* destination */
716 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200719 return( ret );
720 }
721
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000722 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
723 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200724
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000725 rec->data_len += transform->taglen + explicit_iv_len;
726 rec->data_offset -= explicit_iv_len;
727 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100728 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000729 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
732#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000733 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000737 size_t padlen, i;
738 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000739
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000740 /* Currently we're always using minimal padding
741 * (up to 255 bytes would be allowed). */
742 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
743 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 padlen = 0;
745
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000746 /* Check there's enough space in the buffer for the padding. */
747 if( post_avail < padlen + 1 )
748 {
749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
750 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
751 }
752
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000754 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000756 rec->data_len += padlen + 1;
757 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000760 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
762 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000763 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000764 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000765 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000766 if( f_rng == NULL )
767 {
768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
770 }
771
772 if( rec->data_offset < transform->ivlen )
773 {
774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
775 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
776 }
777
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000778 /*
779 * Generate IV
780 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000781 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000782 if( ret != 0 )
783 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000784
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000785 memcpy( data - transform->ivlen, transform->iv_enc,
786 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000787
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000792 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000793 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200794 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000795
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000796 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
797 transform->iv_enc,
798 transform->ivlen,
799 data, rec->data_len,
800 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200803 return( ret );
804 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200805
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000806 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200810 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000813 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200814 {
815 /*
816 * Save IV in SSL3 and TLS1
817 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000818 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
819 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000821 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200822#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000823 {
824 data -= transform->ivlen;
825 rec->data_offset -= transform->ivlen;
826 rec->data_len += transform->ivlen;
827 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100830 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100831 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000832 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
833
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100834 /*
835 * MAC(MAC_write_key, seq_num +
836 * TLSCipherText.type +
837 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100838 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100839 * IV + // except for TLS 1.0
840 * ENC(content + padding + padding_length));
841 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000842
843 if( post_avail < transform->maclen)
844 {
845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
846 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
847 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100848
Hanno Beckercab87e62019-04-29 13:52:53 +0100849 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +0100850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000852 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100853 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100854
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000855 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100856 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000857 mbedtls_md_hmac_update( &transform->md_ctx_enc,
858 data, rec->data_len );
859 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
860 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100861
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000862 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100863
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000864 rec->data_len += transform->maclen;
865 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100866 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100867 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200870 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000872 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200876 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100878 /* Make extra sure authentication was performed, exactly once */
879 if( auth_done != 1 )
880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100883 }
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
887 return( 0 );
888}
889
Hanno Becker605949f2019-07-12 08:23:59 +0100890int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +0000891 mbedtls_ssl_transform *transform,
892 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000893{
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000894 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000896 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +0000897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +0100898 size_t padlen = 0, correct = 1;
899#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000900 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100901 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100902 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000903
Hanno Beckera18d1322018-01-03 14:27:32 +0000904#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200905 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000906 ((void) ssl);
907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000910 if( rec == NULL ||
911 rec->buf == NULL ||
912 rec->buf_len < rec->data_offset ||
913 rec->buf_len - rec->data_offset < rec->data_len )
914 {
915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100917 }
918
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000919 data = rec->buf + rec->data_offset;
920 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Hanno Beckera0e20d02019-05-15 14:03:01 +0100922#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100923 /*
924 * Match record's CID with incoming CID.
925 */
Hanno Becker938489a2019-05-08 13:02:22 +0100926 if( rec->cid_len != transform->in_cid_len ||
927 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
928 {
Hanno Becker8367ccc2019-05-14 11:30:10 +0100929 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +0100930 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100931#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
934 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
936 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000937 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
938 transform->iv_dec,
939 transform->ivlen,
940 data, rec->data_len,
941 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200944 return( ret );
945 }
946
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000947 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 }
Paul Bakker68884e32013-01-07 18:20:04 +0100953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200955#if defined(MBEDTLS_GCM_C) || \
956 defined(MBEDTLS_CCM_C) || \
957 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200959 mode == MBEDTLS_MODE_CCM ||
960 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000961 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200962 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200963 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000964
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200965 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +0100966 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200967 */
Hanno Beckerd96a6522019-07-10 13:55:25 +0100968
969 /* Check that there's enough space for the explicit IV
970 * (at the beginning of the record) and the MAC (at the
971 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000972 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +0200973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000975 "+ taglen (%d)", rec->data_len,
976 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +0200978 }
Paul Bakker68884e32013-01-07 18:20:04 +0100979
Hanno Beckerd96a6522019-07-10 13:55:25 +0100980#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200981 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
982 {
Hanno Beckerd96a6522019-07-10 13:55:25 +0100983 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +0100984
Hanno Beckerd96a6522019-07-10 13:55:25 +0100985 /* Fixed */
986 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
987 /* Explicit */
988 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200989 }
Hanno Beckerd96a6522019-07-10 13:55:25 +0100990 else
991#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
992#if defined(MBEDTLS_CHACHAPOLY_C)
993 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200994 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200995 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200996 unsigned char i;
997
998 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
999
1000 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001001 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001002 }
1003 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01001004#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005 {
1006 /* Reminder if we ever add an AEAD mode with a different size */
1007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1009 }
1010
Hanno Beckerd96a6522019-07-10 13:55:25 +01001011 /* Group changes to data, data_len, and add_data, because
1012 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001013 data += explicit_iv_len;
1014 rec->data_offset += explicit_iv_len;
1015 rec->data_len -= explicit_iv_len + transform->taglen;
1016
Hanno Beckercab87e62019-04-29 13:52:53 +01001017 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001018 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001019 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001020
Hanno Beckerd96a6522019-07-10 13:55:25 +01001021 /* Because of the check above, we know that there are
1022 * explicit_iv_len Bytes preceeding data, and taglen
1023 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001024 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001025 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001026
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001027 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001028 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001029 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001030
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001031 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001032 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001033 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001034 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1035 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001036 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001037 data, rec->data_len,
1038 data, &olen,
1039 data + rec->data_len,
1040 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1045 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001046
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001047 return( ret );
1048 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001049 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001050
Hanno Beckerd96a6522019-07-10 13:55:25 +01001051 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001052 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001056 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1060#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001061 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001064 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001065
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 /*
Paul Bakker45829992013-01-03 14:52:21 +01001067 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001070 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1071 {
1072 /* The ciphertext is prefixed with the CBC IV. */
1073 minlen += transform->ivlen;
1074 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001075#endif
Paul Bakker45829992013-01-03 14:52:21 +01001076
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001077 /* Size considerations:
1078 *
1079 * - The CBC cipher text must not be empty and hence
1080 * at least of size transform->ivlen.
1081 *
1082 * Together with the potential IV-prefix, this explains
1083 * the first of the two checks below.
1084 *
1085 * - The record must contain a MAC, either in plain or
1086 * encrypted, depending on whether Encrypt-then-MAC
1087 * is used or not.
1088 * - If it is, the message contains the IV-prefix,
1089 * the CBC ciphertext, and the MAC.
1090 * - If it is not, the padded plaintext, and hence
1091 * the CBC ciphertext, has at least length maclen + 1
1092 * because there is at least the padding length byte.
1093 *
1094 * As the CBC ciphertext is not empty, both cases give the
1095 * lower bound minlen + maclen + 1 on the record size, which
1096 * we test for in the second check below.
1097 */
1098 if( rec->data_len < minlen + transform->ivlen ||
1099 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001102 "+ 1 ) ( + expl IV )", rec->data_len,
1103 transform->ivlen,
1104 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001106 }
1107
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001108 /*
1109 * Authenticate before decrypt if enabled
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001112 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001113 {
Hanno Becker992b6872017-11-09 18:57:39 +00001114 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001117
Hanno Beckerd96a6522019-07-10 13:55:25 +01001118 /* Update data_len in tandem with add_data.
1119 *
1120 * The subtraction is safe because of the previous check
1121 * data_len >= minlen + maclen + 1.
1122 *
1123 * Afterwards, we know that data + data_len is followed by at
1124 * least maclen Bytes, which justifies the call to
1125 * mbedtls_ssl_safer_memcmp() below.
1126 *
1127 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001128 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01001129 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001130
Hanno Beckerd96a6522019-07-10 13:55:25 +01001131 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001132 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1133 add_data_len );
1134 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1135 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001136 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1137 data, rec->data_len );
1138 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1139 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001140
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001141 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1142 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001143 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001144 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001145
Hanno Beckerd96a6522019-07-10 13:55:25 +01001146 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001147 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1148 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001152 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001153 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001156
1157 /*
1158 * Check length sanity
1159 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001160
1161 /* We know from above that data_len > minlen >= 0,
1162 * so the following check in particular implies that
1163 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001164 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001167 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001169 }
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001172 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001173 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001174 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001175 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001176 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001177 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001178 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001179
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001180 data += transform->ivlen;
1181 rec->data_offset += transform->ivlen;
1182 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001185
Hanno Beckerd96a6522019-07-10 13:55:25 +01001186 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1187
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001188 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1189 transform->iv_dec, transform->ivlen,
1190 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001193 return( ret );
1194 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001195
Hanno Beckerd96a6522019-07-10 13:55:25 +01001196 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001197 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001201 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001204 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001205 {
1206 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001207 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1208 * records is equivalent to CBC decryption of the concatenation
1209 * of the records; in other words, IVs are maintained across
1210 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001211 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001212 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1213 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001217 /* Safe since data_len >= minlen + maclen + 1, so after having
1218 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001219 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1220 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001221 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001222
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001223 if( auth_done == 1 )
1224 {
1225 correct *= ( rec->data_len >= padlen + 1 );
1226 padlen *= ( rec->data_len >= padlen + 1 );
1227 }
1228 else
Paul Bakker45829992013-01-03 14:52:21 +01001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001231 if( rec->data_len < transform->maclen + padlen + 1 )
1232 {
1233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1234 rec->data_len,
1235 transform->maclen,
1236 padlen + 1 ) );
1237 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001238#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001239
1240 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
1241 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01001242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001244 padlen++;
1245
1246 /* Regardless of the validity of the padding,
1247 * we have data_len >= padlen here. */
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001250 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001252 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_DEBUG_ALL)
1255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001256 "should be no more than %d",
1257 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001258#endif
Paul Bakker45829992013-01-03 14:52:21 +01001259 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261 }
1262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1264#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1265 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001266 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001268 /* The padding check involves a series of up to 256
1269 * consecutive memory reads at the end of the record
1270 * plaintext buffer. In order to hide the length and
1271 * validity of the padding, always perform exactly
1272 * `min(256,plaintext_len)` reads (but take into account
1273 * only the last `padlen` bytes for the padding check). */
1274 size_t pad_count = 0;
1275 size_t real_count = 0;
1276 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001277
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001278 /* Index of first padding byte; it has been ensured above
1279 * that the subtraction is safe. */
1280 size_t const padding_idx = rec->data_len - padlen;
1281 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1282 size_t const start_idx = rec->data_len - num_checks;
1283 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001284
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001285 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001286 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001287 real_count |= ( idx >= padding_idx );
1288 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001289 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001290 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001293 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001295#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001296 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1300 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001304 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001305
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001306 /* If the padding was found to be invalid, padlen == 0
1307 * and the subtraction is safe. If the padding was found valid,
1308 * padlen hasn't been changed and the previous assertion
1309 * data_len >= padlen still holds. */
1310 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001314 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001320#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001322 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001326 * Authenticate if not done yet.
1327 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 */
Hanno Becker52344c22018-01-03 15:24:20 +00001329#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001330 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001331 {
Hanno Becker992b6872017-11-09 18:57:39 +00001332 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001333
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001334 /* If the initial value of padlen was such that
1335 * data_len < maclen + padlen + 1, then padlen
1336 * got reset to 1, and the initial check
1337 * data_len >= minlen + maclen + 1
1338 * guarantees that at this point we still
1339 * have at least data_len >= maclen.
1340 *
1341 * If the initial value of padlen was such that
1342 * data_len >= maclen + padlen + 1, then we have
1343 * subtracted either padlen + 1 (if the padding was correct)
1344 * or 0 (if the padding was incorrect) since then,
1345 * hence data_len >= maclen in any case.
1346 */
1347 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01001348 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001351 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001352 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001353 ssl_mac( &transform->md_ctx_dec,
1354 transform->mac_dec,
1355 data, rec->data_len,
1356 rec->ctr, rec->type,
1357 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001358 }
1359 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1362 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001363 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001364 {
1365 /*
1366 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02001367 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001368 *
1369 * Known timing attacks:
1370 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1371 *
Gilles Peskine20b44082018-05-29 14:06:49 +02001372 * To compensate for different timings for the MAC calculation
1373 * depending on how much padding was removed (which is determined
1374 * by padlen), process extra_run more blocks through the hash
1375 * function.
1376 *
1377 * The formula in the paper is
1378 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1379 * where L1 is the size of the header plus the decrypted message
1380 * plus CBC padding and L2 is the size of the header plus the
1381 * decrypted message. This is for an underlying hash function
1382 * with 64-byte blocks.
1383 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1384 * correctly. We round down instead of up, so -56 is the correct
1385 * value for our calculations instead of -55.
1386 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02001387 * Repeat the formula rather than defining a block_size variable.
1388 * This avoids requiring division by a variable at runtime
1389 * (which would be marginally less efficient and would require
1390 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001391 */
1392 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001393 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001394
1395 /*
1396 * The next two sizes are the minimum and maximum values of
1397 * in_msglen over all padlen values.
1398 *
1399 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001400 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001401 *
1402 * Note that max_len + maclen is never more than the buffer
1403 * length, as we previously did in_msglen -= maclen too.
1404 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001405 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001406 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1407
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001408 memset( tmp, 0, sizeof( tmp ) );
1409
1410 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001411 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001412#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1413 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001414 case MBEDTLS_MD_MD5:
1415 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001416 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001417 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001418 extra_run =
1419 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1420 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001421 break;
1422#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001423#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001424 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001425 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001426 extra_run =
1427 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1428 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001429 break;
1430#endif
1431 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1434 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001435
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001436 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001437
Hanno Beckercab87e62019-04-29 13:52:53 +01001438 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1439 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001440 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1441 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001442 /* Make sure we access everything even when padlen > 0. This
1443 * makes the synchronisation requirements for just-in-time
1444 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001445 ssl_read_memory( data + rec->data_len, padlen );
1446 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001447
1448 /* Call mbedtls_md_process at least once due to cache attacks
1449 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001450 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001451 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001452
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001453 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001454
1455 /* Make sure we access all the memory that could contain the MAC,
1456 * before we check it in the next code block. This makes the
1457 * synchronisation requirements for just-in-time Prime+Probe
1458 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001459 ssl_read_memory( data + min_len,
1460 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001461 }
1462 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1464 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001470#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001471 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1472 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001475 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1476 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_SSL_DEBUG_ALL)
1479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001480#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001481 correct = 0;
1482 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001483 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001484 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001485
1486 /*
1487 * Finally check the correct flag
1488 */
1489 if( correct == 0 )
1490 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001491#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001492
1493 /* Make extra sure authentication was performed, exactly once */
1494 if( auth_done != 1 )
1495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
Hanno Beckera0e20d02019-05-15 14:03:01 +01001500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001501 if( rec->cid_len != 0 )
1502 {
1503 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
1504 &rec->type );
1505 if( ret != 0 )
1506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1507 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001508#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
1512 return( 0 );
1513}
1514
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001515#undef MAC_NONE
1516#undef MAC_PLAINTEXT
1517#undef MAC_CIPHERTEXT
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001520/*
1521 * Compression/decompression functions
1522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001524{
Janos Follath865b3eb2019-12-16 11:46:15 +00001525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001526 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001527 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001528 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001529 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001532
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001533 if( len_pre == 0 )
1534 return( 0 );
1535
Paul Bakker2770fbd2012-07-03 13:30:23 +00001536 memcpy( msg_pre, ssl->out_msg, len_pre );
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001539 ssl->out_msglen ) );
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001542 ssl->out_msg, ssl->out_msglen );
1543
Paul Bakker48916f92012-09-16 19:57:18 +00001544 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1545 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1546 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10001547 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001550 if( ret != Z_OK )
1551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1553 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001554 }
1555
Angus Grattond8213d02016-05-25 20:56:48 +10001556 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001557 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001560 ssl->out_msglen ) );
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001563 ssl->out_msg, ssl->out_msglen );
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001566
1567 return( 0 );
1568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001571{
Janos Follath865b3eb2019-12-16 11:46:15 +00001572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001573 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001574 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001575 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001576 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001579
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001580 if( len_pre == 0 )
1581 return( 0 );
1582
Paul Bakker2770fbd2012-07-03 13:30:23 +00001583 memcpy( msg_pre, ssl->in_msg, len_pre );
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586 ssl->in_msglen ) );
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589 ssl->in_msg, ssl->in_msglen );
1590
Paul Bakker48916f92012-09-16 19:57:18 +00001591 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1592 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1593 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10001594 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001595 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakker48916f92012-09-16 19:57:18 +00001597 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001598 if( ret != Z_OK )
1599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1601 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602 }
1603
Angus Grattond8213d02016-05-25 20:56:48 +10001604 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001605 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608 ssl->in_msglen ) );
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001611 ssl->in_msg, ssl->in_msglen );
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001614
1615 return( 0 );
1616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618
Paul Bakker5121ce52009-01-03 21:22:43 +00001619/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001620 * Fill the input message buffer by appending data to it.
1621 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001622 *
1623 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1624 * available (from this read and/or a previous one). Otherwise, an error code
1625 * is returned (possibly EOF or WANT_READ).
1626 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001627 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1628 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1629 * since we always read a whole datagram at once.
1630 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001631 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001632 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001635{
Janos Follath865b3eb2019-12-16 11:46:15 +00001636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001637 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001641 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001644 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001646 }
1647
Angus Grattond8213d02016-05-25 20:56:48 +10001648 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001652 }
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001655 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001657 uint32_t timeout;
1658
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001659 /* Just to be sure */
1660 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1661 {
1662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1663 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1665 }
1666
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001667 /*
1668 * The point is, we need to always read a full datagram at once, so we
1669 * sometimes read more then requested, and handle the additional data.
1670 * It could be the rest of the current record (while fetching the
1671 * header) and/or some other records in the same datagram.
1672 */
1673
1674 /*
1675 * Move to the next record in the already read datagram if applicable
1676 */
1677 if( ssl->next_record_offset != 0 )
1678 {
1679 if( ssl->in_left < ssl->next_record_offset )
1680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001683 }
1684
1685 ssl->in_left -= ssl->next_record_offset;
1686
1687 if( ssl->in_left != 0 )
1688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001690 ssl->next_record_offset ) );
1691 memmove( ssl->in_hdr,
1692 ssl->in_hdr + ssl->next_record_offset,
1693 ssl->in_left );
1694 }
1695
1696 ssl->next_record_offset = 0;
1697 }
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001701
1702 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001703 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001704 */
1705 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001708 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001709 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001710
1711 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001712 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001713 * are not at the beginning of a new record, the caller did something
1714 * wrong.
1715 */
1716 if( ssl->in_left != 0 )
1717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001720 }
1721
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001722 /*
1723 * Don't even try to read if time's out already.
1724 * This avoids by-passing the timer when repeatedly receiving messages
1725 * that will end up being dropped.
1726 */
Hanno Becker7876d122020-02-05 10:39:31 +00001727 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001728 {
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001730 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001731 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001732 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001733 {
Angus Grattond8213d02016-05-25 20:56:48 +10001734 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001737 timeout = ssl->handshake->retransmit_timeout;
1738 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001739 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001742
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001743 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001744 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1745 timeout );
1746 else
1747 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001750
1751 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001753 }
1754
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001755 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001758 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001761 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001762 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001765 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001771 return( ret );
1772 }
1773
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001774 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001777 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001779 {
Hanno Becker786300f2020-02-05 10:46:40 +00001780 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001781 {
Hanno Becker786300f2020-02-05 10:46:40 +00001782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1783 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001784 return( ret );
1785 }
1786
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001787 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001790 }
1791
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 if( ret < 0 )
1793 return( ret );
1794
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001795 ssl->in_left = ret;
1796 }
1797 else
1798#endif
1799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001801 ssl->in_left, nb_want ) );
1802
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001803 while( ssl->in_left < nb_want )
1804 {
1805 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001806
Hanno Becker7876d122020-02-05 10:39:31 +00001807 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001808 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1809 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001810 {
1811 if( ssl->f_recv_timeout != NULL )
1812 {
1813 ret = ssl->f_recv_timeout( ssl->p_bio,
1814 ssl->in_hdr + ssl->in_left, len,
1815 ssl->conf->read_timeout );
1816 }
1817 else
1818 {
1819 ret = ssl->f_recv( ssl->p_bio,
1820 ssl->in_hdr + ssl->in_left, len );
1821 }
1822 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001825 ssl->in_left, nb_want ) );
1826 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001827
1828 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001830
1831 if( ret < 0 )
1832 return( ret );
1833
mohammad160352aecb92018-03-28 23:41:40 -07001834 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001835 {
Darryl Green11999bb2018-03-13 15:22:58 +00001836 MBEDTLS_SSL_DEBUG_MSG( 1,
1837 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07001838 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1840 }
1841
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001842 ssl->in_left += ret;
1843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 return( 0 );
1849}
1850
1851/*
1852 * Flush any data not yet written
1853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001855{
Janos Follath865b3eb2019-12-16 11:46:15 +00001856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001857 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001861 if( ssl->f_send == NULL )
1862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001864 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001866 }
1867
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001868 /* Avoid incrementing counter if data is flushed */
1869 if( ssl->out_left == 0 )
1870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001872 return( 0 );
1873 }
1874
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 while( ssl->out_left > 0 )
1876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01001878 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Hanno Becker2b1e3542018-08-06 11:19:13 +01001880 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001881 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
1885 if( ret <= 0 )
1886 return( ret );
1887
mohammad160352aecb92018-03-28 23:41:40 -07001888 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001889 {
Darryl Green11999bb2018-03-13 15:22:58 +00001890 MBEDTLS_SSL_DEBUG_MSG( 1,
1891 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07001892 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1894 }
1895
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 ssl->out_left -= ret;
1897 }
1898
Hanno Becker2b1e3542018-08-06 11:19:13 +01001899#if defined(MBEDTLS_SSL_PROTO_DTLS)
1900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001901 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001902 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001903 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001904 else
1905#endif
1906 {
1907 ssl->out_hdr = ssl->out_buf + 8;
1908 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001909 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
1913 return( 0 );
1914}
1915
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001916/*
1917 * Functions to handle the DTLS retransmission state machine
1918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001920/*
1921 * Append current handshake message to current outgoing flight
1922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1927 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1928 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001929
1930 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001931 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001932 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001935 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001936 }
1937
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001938 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001939 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001942 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001943 }
1944
1945 /* Copy current handshake message with headers */
1946 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1947 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001948 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001949 msg->next = NULL;
1950
1951 /* Append to the current flight */
1952 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001953 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001954 else
1955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001957 while( cur->next != NULL )
1958 cur = cur->next;
1959 cur->next = msg;
1960 }
1961
Hanno Becker3b235902018-08-06 09:54:53 +01001962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001963 return( 0 );
1964}
1965
1966/*
1967 * Free the current flight of handshake messages
1968 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00001969void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_ssl_flight_item *cur = flight;
1972 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001973
1974 while( cur != NULL )
1975 {
1976 next = cur->next;
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 mbedtls_free( cur->p );
1979 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001980
1981 cur = next;
1982 }
1983}
1984
1985/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001986 * Swap transform_out and out_ctr with the alternative ones
1987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001991 unsigned char tmp_out_ctr[8];
1992
1993 if( ssl->transform_out == ssl->handshake->alt_transform_out )
1994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001996 return;
1997 }
1998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002000
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002001 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002002 tmp_transform = ssl->transform_out;
2003 ssl->transform_out = ssl->handshake->alt_transform_out;
2004 ssl->handshake->alt_transform_out = tmp_transform;
2005
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002006 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002007 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2008 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002009 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002010
2011 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002012 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2015 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2020 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002021 }
2022 }
2023#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002024}
2025
2026/*
2027 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002028 */
2029int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2030{
2031 int ret = 0;
2032
2033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2034
2035 ret = mbedtls_ssl_flight_transmit( ssl );
2036
2037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2038
2039 return( ret );
2040}
2041
2042/*
2043 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002044 *
2045 * Need to remember the current message in case flush_output returns
2046 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002047 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002048 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002049int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002050{
Janos Follath865b3eb2019-12-16 11:46:15 +00002051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002055 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002057
2058 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002059 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002060 ssl_swap_epochs( ssl );
2061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002063 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002064
2065 while( ssl->handshake->cur_msg != NULL )
2066 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002067 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002068 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002069
Hanno Beckere1dcb032018-08-17 16:47:58 +01002070 int const is_finished =
2071 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2072 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2073
Hanno Becker04da1892018-08-14 13:22:10 +01002074 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2075 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2076
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002077 /* Swap epochs before sending Finished: we can't do it after
2078 * sending ChangeCipherSpec, in case write returns WANT_READ.
2079 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002080 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002081 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002083 ssl_swap_epochs( ssl );
2084 }
2085
Hanno Becker67bc7c32018-08-06 11:33:50 +01002086 ret = ssl_get_remaining_payload_in_datagram( ssl );
2087 if( ret < 0 )
2088 return( ret );
2089 max_frag_len = (size_t) ret;
2090
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002091 /* CCS is copied as is, while HS messages may need fragmentation */
2092 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2093 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002094 if( max_frag_len == 0 )
2095 {
2096 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2097 return( ret );
2098
2099 continue;
2100 }
2101
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002102 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002103 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002104 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002106 /* Update position inside current message */
2107 ssl->handshake->cur_msg_p += cur->len;
2108 }
2109 else
2110 {
2111 const unsigned char * const p = ssl->handshake->cur_msg_p;
2112 const size_t hs_len = cur->len - 12;
2113 const size_t frag_off = p - ( cur->p + 12 );
2114 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002115 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002116
Hanno Beckere1dcb032018-08-17 16:47:58 +01002117 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002118 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002119 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002120 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002121
Hanno Becker67bc7c32018-08-06 11:33:50 +01002122 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2123 return( ret );
2124
2125 continue;
2126 }
2127 max_hs_frag_len = max_frag_len - 12;
2128
2129 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2130 max_hs_frag_len : rem_len;
2131
2132 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002133 {
2134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002135 (unsigned) cur_hs_frag_len,
2136 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002137 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002138
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002139 /* Messages are stored with handshake headers as if not fragmented,
2140 * copy beginning of headers then fill fragmentation fields.
2141 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2142 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002143
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002144 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2145 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2146 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2147
Hanno Becker67bc7c32018-08-06 11:33:50 +01002148 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2149 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2150 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002151
2152 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2153
Hanno Becker3f7b9732018-08-28 09:53:25 +01002154 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002155 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2156 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002157 ssl->out_msgtype = cur->type;
2158
2159 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002160 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002161 }
2162
2163 /* If done with the current message move to the next one if any */
2164 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2165 {
2166 if( cur->next != NULL )
2167 {
2168 ssl->handshake->cur_msg = cur->next;
2169 ssl->handshake->cur_msg_p = cur->next->p + 12;
2170 }
2171 else
2172 {
2173 ssl->handshake->cur_msg = NULL;
2174 ssl->handshake->cur_msg_p = NULL;
2175 }
2176 }
2177
2178 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002179 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002182 return( ret );
2183 }
2184 }
2185
Hanno Becker67bc7c32018-08-06 11:33:50 +01002186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2187 return( ret );
2188
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002189 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2191 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002192 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002195 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002196 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002197
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002199
2200 return( 0 );
2201}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002202
2203/*
2204 * To be called when the last message of an incoming flight is received.
2205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207{
2208 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002209 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002210 ssl->handshake->flight = NULL;
2211 ssl->handshake->cur_msg = NULL;
2212
2213 /* The next incoming flight will start with this msg_seq */
2214 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2215
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002216 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002217 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002218
Hanno Becker0271f962018-08-16 13:23:47 +01002219 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002220 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002221
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002222 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002223 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2226 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002229 }
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002232}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002233
2234/*
2235 * To be called when the last message of an outgoing flight is send.
2236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002238{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002239 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002240 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2243 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002246 }
2247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002251
Paul Bakker5121ce52009-01-03 21:22:43 +00002252/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002253 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002255
2256/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002257 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002258 *
2259 * - fill in handshake headers
2260 * - update handshake checksum
2261 * - DTLS: save message for resending
2262 * - then pass to the record layer
2263 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002264 * DTLS: except for HelloRequest, messages are only queued, and will only be
2265 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002266 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002267 * Inputs:
2268 * - ssl->out_msglen: 4 + actual handshake message len
2269 * (4 is the size of handshake headers for TLS)
2270 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2271 * - ssl->out_msg + 4: the handshake message body
2272 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002273 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002274 * - ssl->out_msglen: the length of the record contents
2275 * (including handshake headers but excluding record headers)
2276 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002277 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002278int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Janos Follath865b3eb2019-12-16 11:46:15 +00002280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002281 const size_t hs_len = ssl->out_msglen - 4;
2282 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2285
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002286 /*
2287 * Sanity checks
2288 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002289 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002290 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2291 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002292 /* In SSLv3, the client might send a NoCertificate alert. */
2293#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2294 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2295 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2296 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2297#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2298 {
2299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2301 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002304 /* Whenever we send anything different from a
2305 * HelloRequest we should be in a handshake - double check. */
2306 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2307 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002308 ssl->handshake == NULL )
2309 {
2310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002316 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002318 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002321 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002322#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002323
Hanno Beckerb50a2532018-08-06 11:52:54 +01002324 /* Double-check that we did not exceed the bounds
2325 * of the outgoing record buffer.
2326 * This should never fail as the various message
2327 * writing functions must obey the bounds of the
2328 * outgoing record buffer, but better be safe.
2329 *
2330 * Note: We deliberately do not check for the MTU or MFL here.
2331 */
2332 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2333 {
2334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2335 "size %u, maximum %u",
2336 (unsigned) ssl->out_msglen,
2337 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2339 }
2340
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002341 /*
2342 * Fill handshake headers
2343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002346 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2347 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2348 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002350 /*
2351 * DTLS has additional fields in the Handshake layer,
2352 * between the length field and the actual payload:
2353 * uint16 message_seq;
2354 * uint24 fragment_offset;
2355 * uint24 fragment_length;
2356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002359 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002360 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002361 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002362 {
2363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2364 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002365 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002366 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2368 }
2369
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002370 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002371 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002372
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002373 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002374 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002375 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002376 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2377 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2378 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002379 }
2380 else
2381 {
2382 ssl->out_msg[4] = 0;
2383 ssl->out_msg[5] = 0;
2384 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002385
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002386 /* Handshake hashes are computed without fragmentation,
2387 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002388 memset( ssl->out_msg + 6, 0x00, 3 );
2389 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002392
Hanno Becker0207e532018-08-28 10:28:28 +01002393 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002394 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2395 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002398 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002401 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2402 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002403 {
2404 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002407 return( ret );
2408 }
2409 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002410 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002411#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002412 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002413 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002414 {
2415 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2416 return( ret );
2417 }
2418 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002419
2420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2421
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002422 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002423}
2424
2425/*
2426 * Record layer functions
2427 */
2428
2429/*
2430 * Write current record.
2431 *
2432 * Uses:
2433 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2434 * - ssl->out_msglen: length of the record content (excl headers)
2435 * - ssl->out_msg: record content
2436 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002437int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002438{
2439 int ret, done = 0;
2440 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002441 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002442
2443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002446 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448 {
2449 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452 return( ret );
2453 }
2454
2455 len = ssl->out_msglen;
2456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2460 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 ret = mbedtls_ssl_hw_record_write( ssl );
2465 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2468 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002469 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002470
2471 if( ret == 0 )
2472 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002475 if( !done )
2476 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002477 unsigned i;
2478 size_t protected_record_size;
2479
Hanno Becker6430faf2019-05-08 11:57:13 +01002480 /* Skip writing the record content type to after the encryption,
2481 * as it may change when using the CID extension. */
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002484 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002485
Hanno Becker19859472018-08-06 09:40:20 +01002486 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002487 ssl->out_len[0] = (unsigned char)( len >> 8 );
2488 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002489
Paul Bakker48916f92012-09-16 19:57:18 +00002490 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002491 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002492 mbedtls_record rec;
2493
2494 rec.buf = ssl->out_iv;
2495 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
2496 ( ssl->out_iv - ssl->out_buf );
2497 rec.data_len = ssl->out_msglen;
2498 rec.data_offset = ssl->out_msg - rec.buf;
2499
2500 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2501 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2502 ssl->conf->transport, rec.ver );
2503 rec.type = ssl->out_msgtype;
2504
Hanno Beckera0e20d02019-05-15 14:03:01 +01002505#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002506 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002507 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002508#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002509
Hanno Beckera18d1322018-01-03 14:27:32 +00002510 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002514 return( ret );
2515 }
2516
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 if( rec.data_offset != 0 )
2518 {
2519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2521 }
2522
Hanno Becker6430faf2019-05-08 11:57:13 +01002523 /* Update the record content type and CID. */
2524 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002525#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002526 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002527#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002528 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002529 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2530 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002531 }
2532
Hanno Becker5903de42019-05-03 14:46:38 +01002533 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002534
2535#if defined(MBEDTLS_SSL_PROTO_DTLS)
2536 /* In case of DTLS, double-check that we don't exceed
2537 * the remaining space in the datagram. */
2538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2539 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002540 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002541 if( ret < 0 )
2542 return( ret );
2543
2544 if( protected_record_size > (size_t) ret )
2545 {
2546 /* Should never happen */
2547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2548 }
2549 }
2550#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002551
Hanno Becker6430faf2019-05-08 11:57:13 +01002552 /* Now write the potentially updated record content type. */
2553 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002556 "version = [%d:%d], msglen = %d",
2557 ssl->out_hdr[0], ssl->out_hdr[1],
2558 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002561 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002562
2563 ssl->out_left += protected_record_size;
2564 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002565 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002566
Hanno Beckerdd772292020-02-05 10:38:31 +00002567 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002568 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2569 break;
2570
2571 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002572 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002573 {
2574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2575 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Hanno Becker67bc7c32018-08-06 11:33:50 +01002579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002580 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2581 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002582 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002583 size_t remaining;
2584 ret = ssl_get_remaining_payload_in_datagram( ssl );
2585 if( ret < 0 )
2586 {
2587 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2588 ret );
2589 return( ret );
2590 }
2591
2592 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002593 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002594 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002595 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002596 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002597 else
2598 {
Hanno Becker513815a2018-08-20 11:56:09 +01002599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002600 }
2601 }
2602#endif /* MBEDTLS_SSL_PROTO_DTLS */
2603
2604 if( ( flush == SSL_FORCE_FLUSH ) &&
2605 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 return( ret );
2609 }
2610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
2613 return( 0 );
2614}
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002617
2618static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2619{
2620 if( ssl->in_msglen < ssl->in_hslen ||
2621 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2622 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2623 {
2624 return( 1 );
2625 }
2626 return( 0 );
2627}
Hanno Becker44650b72018-08-16 12:51:11 +01002628
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002629static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002630{
2631 return( ( ssl->in_msg[9] << 16 ) |
2632 ( ssl->in_msg[10] << 8 ) |
2633 ssl->in_msg[11] );
2634}
2635
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002636static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002637{
2638 return( ( ssl->in_msg[6] << 16 ) |
2639 ( ssl->in_msg[7] << 8 ) |
2640 ssl->in_msg[8] );
2641}
2642
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002643static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002644{
2645 uint32_t msg_len, frag_off, frag_len;
2646
2647 msg_len = ssl_get_hs_total_len( ssl );
2648 frag_off = ssl_get_hs_frag_off( ssl );
2649 frag_len = ssl_get_hs_frag_len( ssl );
2650
2651 if( frag_off > msg_len )
2652 return( -1 );
2653
2654 if( frag_len > msg_len - frag_off )
2655 return( -1 );
2656
2657 if( frag_len + 12 > ssl->in_msglen )
2658 return( -1 );
2659
2660 return( 0 );
2661}
2662
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002663/*
2664 * Mark bits in bitmask (used for DTLS HS reassembly)
2665 */
2666static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2667{
2668 unsigned int start_bits, end_bits;
2669
2670 start_bits = 8 - ( offset % 8 );
2671 if( start_bits != 8 )
2672 {
2673 size_t first_byte_idx = offset / 8;
2674
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002675 /* Special case */
2676 if( len <= start_bits )
2677 {
2678 for( ; len != 0; len-- )
2679 mask[first_byte_idx] |= 1 << ( start_bits - len );
2680
2681 /* Avoid potential issues with offset or len becoming invalid */
2682 return;
2683 }
2684
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002685 offset += start_bits; /* Now offset % 8 == 0 */
2686 len -= start_bits;
2687
2688 for( ; start_bits != 0; start_bits-- )
2689 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2690 }
2691
2692 end_bits = len % 8;
2693 if( end_bits != 0 )
2694 {
2695 size_t last_byte_idx = ( offset + len ) / 8;
2696
2697 len -= end_bits; /* Now len % 8 == 0 */
2698
2699 for( ; end_bits != 0; end_bits-- )
2700 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2701 }
2702
2703 memset( mask + offset / 8, 0xFF, len / 8 );
2704}
2705
2706/*
2707 * Check that bitmask is full
2708 */
2709static int ssl_bitmask_check( unsigned char *mask, size_t len )
2710{
2711 size_t i;
2712
2713 for( i = 0; i < len / 8; i++ )
2714 if( mask[i] != 0xFF )
2715 return( -1 );
2716
2717 for( i = 0; i < len % 8; i++ )
2718 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2719 return( -1 );
2720
2721 return( 0 );
2722}
2723
Hanno Becker56e205e2018-08-16 09:06:12 +01002724/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002725static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002726 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002727{
Hanno Becker56e205e2018-08-16 09:06:12 +01002728 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002729
Hanno Becker56e205e2018-08-16 09:06:12 +01002730 alloc_len = 12; /* Handshake header */
2731 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002732
Hanno Beckerd07df862018-08-16 09:14:58 +01002733 if( add_bitmap )
2734 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002735
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002736 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002737}
Hanno Becker56e205e2018-08-16 09:06:12 +01002738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002740
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002741static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002742{
2743 return( ( ssl->in_msg[1] << 16 ) |
2744 ( ssl->in_msg[2] << 8 ) |
2745 ssl->in_msg[3] );
2746}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002747
Simon Butcher99000142016-10-13 17:21:01 +01002748int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002753 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002755 }
2756
Hanno Becker12555c62018-08-16 12:47:53 +01002757 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002760 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002761 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002765 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002767 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002768
Hanno Becker44650b72018-08-16 12:51:11 +01002769 if( ssl_check_hs_header( ssl ) != 0 )
2770 {
2771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2772 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2773 }
2774
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002775 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002776 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2777 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2778 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2779 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002780 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002781 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2782 {
2783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2784 recv_msg_seq,
2785 ssl->handshake->in_msg_seq ) );
2786 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2787 }
2788
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002789 /* Retransmit only on last message from previous flight, to avoid
2790 * too many retransmissions.
2791 * Besides, No sane server ever retransmits HelloVerifyRequest */
2792 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002796 "message_seq = %d, start_of_flight = %d",
2797 recv_msg_seq,
2798 ssl->handshake->in_flight_start_seq ) );
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002803 return( ret );
2804 }
2805 }
2806 else
2807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002809 "message_seq = %d, expected = %d",
2810 recv_msg_seq,
2811 ssl->handshake->in_msg_seq ) );
2812 }
2813
Hanno Becker90333da2017-10-10 11:27:13 +01002814 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002815 }
2816 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002817
Hanno Becker6d97ef52018-08-16 13:09:04 +01002818 /* Message reassembly is handled alongside buffering of future
2819 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002820 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002821 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002822 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002825 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002826 }
2827 }
2828 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002830 /* With TLS we don't handle fragmentation (for now) */
2831 if( ssl->in_msglen < ssl->in_hslen )
2832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2834 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002835 }
2836
Simon Butcher99000142016-10-13 17:21:01 +01002837 return( 0 );
2838}
2839
2840void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2841{
Hanno Becker0271f962018-08-16 13:23:47 +01002842 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002843
Hanno Becker0271f962018-08-16 13:23:47 +01002844 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002845 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002846 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002847 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002848
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002849 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002852 ssl->handshake != NULL )
2853 {
Hanno Becker0271f962018-08-16 13:23:47 +01002854 unsigned offset;
2855 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002856
Hanno Becker0271f962018-08-16 13:23:47 +01002857 /* Increment handshake sequence number */
2858 hs->in_msg_seq++;
2859
2860 /*
2861 * Clear up handshake buffering and reassembly structure.
2862 */
2863
2864 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002865 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002866
2867 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002868 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2869 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002870 offset++, hs_buf++ )
2871 {
2872 *hs_buf = *(hs_buf + 1);
2873 }
2874
2875 /* Create a fresh last entry */
2876 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002877 }
2878#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002879}
2880
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002881/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002882 * DTLS anti-replay: RFC 6347 4.1.2.6
2883 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002884 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2885 * Bit n is set iff record number in_window_top - n has been seen.
2886 *
2887 * Usually, in_window_top is the last record number seen and the lsb of
2888 * in_window is set. The only exception is the initial state (record number 0
2889 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002892void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002893{
2894 ssl->in_window_top = 0;
2895 ssl->in_window = 0;
2896}
2897
2898static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2899{
2900 return( ( (uint64_t) buf[0] << 40 ) |
2901 ( (uint64_t) buf[1] << 32 ) |
2902 ( (uint64_t) buf[2] << 24 ) |
2903 ( (uint64_t) buf[3] << 16 ) |
2904 ( (uint64_t) buf[4] << 8 ) |
2905 ( (uint64_t) buf[5] ) );
2906}
2907
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002908static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2909{
Janos Follath865b3eb2019-12-16 11:46:15 +00002910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002911 unsigned char *original_in_ctr;
2912
2913 // save original in_ctr
2914 original_in_ctr = ssl->in_ctr;
2915
2916 // use counter from record
2917 ssl->in_ctr = record_in_ctr;
2918
2919 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2920
2921 // restore the counter
2922 ssl->in_ctr = original_in_ctr;
2923
2924 return ret;
2925}
2926
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002927/*
2928 * Return 0 if sequence number is acceptable, -1 otherwise
2929 */
Hanno Becker0183d692019-07-12 08:50:37 +01002930int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002931{
2932 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2933 uint64_t bit;
2934
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002935 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002936 return( 0 );
2937
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002938 if( rec_seqnum > ssl->in_window_top )
2939 return( 0 );
2940
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002941 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002942
2943 if( bit >= 64 )
2944 return( -1 );
2945
2946 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2947 return( -1 );
2948
2949 return( 0 );
2950}
2951
2952/*
2953 * Update replay window on new validated record
2954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002956{
2957 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2958
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002959 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002960 return;
2961
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002962 if( rec_seqnum > ssl->in_window_top )
2963 {
2964 /* Update window_top and the contents of the window */
2965 uint64_t shift = rec_seqnum - ssl->in_window_top;
2966
2967 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002968 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002969 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002970 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002971 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002972 ssl->in_window |= 1;
2973 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002974
2975 ssl->in_window_top = rec_seqnum;
2976 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002977 else
2978 {
2979 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002980 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002981
2982 if( bit < 64 ) /* Always true, but be extra sure */
2983 ssl->in_window |= (uint64_t) 1 << bit;
2984 }
2985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002987
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002989/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002990 * Without any SSL context, check if a datagram looks like a ClientHello with
2991 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002992 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002993 *
2994 * - if cookie is valid, return 0
2995 * - if ClientHello looks superficially valid but cookie is not,
2996 * fill obuf and set olen, then
2997 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
2998 * - otherwise return a specific error code
2999 */
3000static int ssl_check_dtls_clihlo_cookie(
3001 mbedtls_ssl_cookie_write_t *f_cookie_write,
3002 mbedtls_ssl_cookie_check_t *f_cookie_check,
3003 void *p_cookie,
3004 const unsigned char *cli_id, size_t cli_id_len,
3005 const unsigned char *in, size_t in_len,
3006 unsigned char *obuf, size_t buf_len, size_t *olen )
3007{
3008 size_t sid_len, cookie_len;
3009 unsigned char *p;
3010
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003011 /*
3012 * Structure of ClientHello with record and handshake headers,
3013 * and expected values. We don't need to check a lot, more checks will be
3014 * done when actually parsing the ClientHello - skipping those checks
3015 * avoids code duplication and does not make cookie forging any easier.
3016 *
3017 * 0-0 ContentType type; copied, must be handshake
3018 * 1-2 ProtocolVersion version; copied
3019 * 3-4 uint16 epoch; copied, must be 0
3020 * 5-10 uint48 sequence_number; copied
3021 * 11-12 uint16 length; (ignored)
3022 *
3023 * 13-13 HandshakeType msg_type; (ignored)
3024 * 14-16 uint24 length; (ignored)
3025 * 17-18 uint16 message_seq; copied
3026 * 19-21 uint24 fragment_offset; copied, must be 0
3027 * 22-24 uint24 fragment_length; (ignored)
3028 *
3029 * 25-26 ProtocolVersion client_version; (ignored)
3030 * 27-58 Random random; (ignored)
3031 * 59-xx SessionID session_id; 1 byte len + sid_len content
3032 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3033 * ...
3034 *
3035 * Minimum length is 61 bytes.
3036 */
3037 if( in_len < 61 ||
3038 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3039 in[3] != 0 || in[4] != 0 ||
3040 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3041 {
3042 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3043 }
3044
3045 sid_len = in[59];
3046 if( sid_len > in_len - 61 )
3047 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3048
3049 cookie_len = in[60 + sid_len];
3050 if( cookie_len > in_len - 60 )
3051 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3052
3053 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3054 cli_id, cli_id_len ) == 0 )
3055 {
3056 /* Valid cookie */
3057 return( 0 );
3058 }
3059
3060 /*
3061 * If we get here, we've got an invalid cookie, let's prepare HVR.
3062 *
3063 * 0-0 ContentType type; copied
3064 * 1-2 ProtocolVersion version; copied
3065 * 3-4 uint16 epoch; copied
3066 * 5-10 uint48 sequence_number; copied
3067 * 11-12 uint16 length; olen - 13
3068 *
3069 * 13-13 HandshakeType msg_type; hello_verify_request
3070 * 14-16 uint24 length; olen - 25
3071 * 17-18 uint16 message_seq; copied
3072 * 19-21 uint24 fragment_offset; copied
3073 * 22-24 uint24 fragment_length; olen - 25
3074 *
3075 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3076 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3077 *
3078 * Minimum length is 28.
3079 */
3080 if( buf_len < 28 )
3081 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3082
3083 /* Copy most fields and adapt others */
3084 memcpy( obuf, in, 25 );
3085 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3086 obuf[25] = 0xfe;
3087 obuf[26] = 0xff;
3088
3089 /* Generate and write actual cookie */
3090 p = obuf + 28;
3091 if( f_cookie_write( p_cookie,
3092 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3093 {
3094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3095 }
3096
3097 *olen = p - obuf;
3098
3099 /* Go back and fill length fields */
3100 obuf[27] = (unsigned char)( *olen - 28 );
3101
3102 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3103 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3104 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3105
3106 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3107 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3108
3109 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3110}
3111
3112/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003113 * Handle possible client reconnect with the same UDP quadruplet
3114 * (RFC 6347 Section 4.2.8).
3115 *
3116 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3117 * that looks like a ClientHello.
3118 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003119 * - if the input looks like a ClientHello without cookies,
3120 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003121 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003122 * - if the input looks like a ClientHello with a valid cookie,
3123 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003124 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003125 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003126 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003127 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003128 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3129 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003130 */
3131static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3132{
Janos Follath865b3eb2019-12-16 11:46:15 +00003133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003134 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003135
Hanno Becker2fddd372019-07-10 14:37:41 +01003136 if( ssl->conf->f_cookie_write == NULL ||
3137 ssl->conf->f_cookie_check == NULL )
3138 {
3139 /* If we can't use cookies to verify reachability of the peer,
3140 * drop the record. */
3141 return( 0 );
3142 }
3143
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003144 ret = ssl_check_dtls_clihlo_cookie(
3145 ssl->conf->f_cookie_write,
3146 ssl->conf->f_cookie_check,
3147 ssl->conf->p_cookie,
3148 ssl->cli_id, ssl->cli_id_len,
3149 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003150 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003151
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003152 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3153
3154 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003155 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003156 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003157 * If the error is permanent we'll catch it later,
3158 * if it's not, then hopefully it'll work next time. */
3159 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01003160 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003161 }
3162
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003163 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003164 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003165 /* Got a valid cookie, partially reset context */
Hanno Becker43aefe22020-02-05 10:44:56 +00003166 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003167 {
3168 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3169 return( ret );
3170 }
3171
3172 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003173 }
3174
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003175 return( ret );
3176}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003177#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003178
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003179static int ssl_check_record_type( uint8_t record_type )
3180{
3181 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3182 record_type != MBEDTLS_SSL_MSG_ALERT &&
3183 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3184 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3185 {
3186 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3187 }
3188
3189 return( 0 );
3190}
3191
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003192/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003193 * ContentType type;
3194 * ProtocolVersion version;
3195 * uint16 epoch; // DTLS only
3196 * uint48 sequence_number; // DTLS only
3197 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003198 *
3199 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003200 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003201 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3202 *
3203 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003204 * 1. proceed with the record if this function returns 0
3205 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3206 * 3. return CLIENT_RECONNECT if this function return that value
3207 * 4. drop the whole datagram if this function returns anything else.
3208 * Point 2 is needed when the peer is resending, and we have already received
3209 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003210 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003211static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003212 unsigned char *buf,
3213 size_t len,
3214 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003216 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Hanno Beckere5e7e782019-07-11 12:29:35 +01003218 size_t const rec_hdr_type_offset = 0;
3219 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003220
Hanno Beckere5e7e782019-07-11 12:29:35 +01003221 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3222 rec_hdr_type_len;
3223 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Hanno Beckere5e7e782019-07-11 12:29:35 +01003225 size_t const rec_hdr_ctr_len = 8;
3226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003227 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003228 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3229 rec_hdr_version_len;
3230
Hanno Beckera0e20d02019-05-15 14:03:01 +01003231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003232 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3233 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003234 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003235#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3236#endif /* MBEDTLS_SSL_PROTO_DTLS */
3237
3238 size_t rec_hdr_len_offset; /* To be determined */
3239 size_t const rec_hdr_len_len = 2;
3240
3241 /*
3242 * Check minimum lengths for record header.
3243 */
3244
3245#if defined(MBEDTLS_SSL_PROTO_DTLS)
3246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3247 {
3248 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3249 }
3250 else
3251#endif /* MBEDTLS_SSL_PROTO_DTLS */
3252 {
3253 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3254 }
3255
3256 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3257 {
3258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3259 (unsigned) len,
3260 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3261 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3262 }
3263
3264 /*
3265 * Parse and validate record content type
3266 */
3267
3268 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003269
3270 /* Check record content type */
3271#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3272 rec->cid_len = 0;
3273
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003275 ssl->conf->cid_len != 0 &&
3276 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003277 {
3278 /* Shift pointers to account for record header including CID
3279 * struct {
3280 * ContentType special_type = tls12_cid;
3281 * ProtocolVersion version;
3282 * uint16 epoch;
3283 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003284 * opaque cid[cid_length]; // Additional field compared to
3285 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003286 * uint16 length;
3287 * opaque enc_content[DTLSCiphertext.length];
3288 * } DTLSCiphertext;
3289 */
3290
3291 /* So far, we only support static CID lengths
3292 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003293 rec_hdr_cid_len = ssl->conf->cid_len;
3294 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003295
Hanno Beckere5e7e782019-07-11 12:29:35 +01003296 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003297 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3299 (unsigned) len,
3300 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003301 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003302 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003303
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003304 /* configured CID len is guaranteed at most 255, see
3305 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3306 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003307 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003308 }
3309 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003310#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003311 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003312 if( ssl_check_record_type( rec->type ) )
3313 {
Hanno Becker54229812019-07-12 14:40:00 +01003314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3315 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003316 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3317 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003318 }
3319
Hanno Beckere5e7e782019-07-11 12:29:35 +01003320 /*
3321 * Parse and validate record version
3322 */
3323
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003324 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3325 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003326 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3327 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003328 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003329
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003330 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3333 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 }
3335
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003336 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3339 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 }
3341
Hanno Beckere5e7e782019-07-11 12:29:35 +01003342 /*
3343 * Parse/Copy record sequence number.
3344 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003345
Hanno Beckere5e7e782019-07-11 12:29:35 +01003346#if defined(MBEDTLS_SSL_PROTO_DTLS)
3347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003348 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003349 /* Copy explicit record sequence number from input buffer. */
3350 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3351 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003352 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003353 else
3354#endif /* MBEDTLS_SSL_PROTO_DTLS */
3355 {
3356 /* Copy implicit record sequence number from SSL context structure. */
3357 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3358 }
Paul Bakker40e46942009-01-03 21:51:57 +00003359
Hanno Beckere5e7e782019-07-11 12:29:35 +01003360 /*
3361 * Parse record length.
3362 */
3363
Hanno Beckere5e7e782019-07-11 12:29:35 +01003364 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003365 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3366 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003367 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003370 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003371 rec->type,
3372 major_ver, minor_ver, rec->data_len ) );
3373
3374 rec->buf = buf;
3375 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003376
Hanno Beckerd417cc92019-07-26 08:20:27 +01003377 if( rec->data_len == 0 )
3378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003380 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003381 * DTLS-related tests.
3382 * Check epoch before checking length constraint because
3383 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3384 * message gets duplicated before the corresponding Finished message,
3385 * the second ChangeCipherSpec should be discarded because it belongs
3386 * to an old epoch, but not because its length is shorter than
3387 * the minimum record length for packets using the new record transform.
3388 * Note that these two kinds of failures are handled differently,
3389 * as an unexpected record is silently skipped but an invalid
3390 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003391 */
3392#if defined(MBEDTLS_SSL_PROTO_DTLS)
3393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3394 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003395 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003396
Hanno Becker955a5c92019-07-10 17:12:07 +01003397 /* Check that the datagram is large enough to contain a record
3398 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003399 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003400 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3402 (unsigned) len,
3403 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3405 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003406
Hanno Becker37cfe732019-07-10 17:20:01 +01003407 /* Records from other, non-matching epochs are silently discarded.
3408 * (The case of same-port Client reconnects must be considered in
3409 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003410 if( rec_epoch != ssl->in_epoch )
3411 {
3412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3413 "expected %d, received %d",
3414 ssl->in_epoch, rec_epoch ) );
3415
Hanno Becker552f7472019-07-19 10:59:12 +01003416 /* Records from the next epoch are considered for buffering
3417 * (concretely: early Finished messages). */
3418 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003419 {
Hanno Becker552f7472019-07-19 10:59:12 +01003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3421 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003422 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003423
Hanno Becker2fddd372019-07-10 14:37:41 +01003424 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003425 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003426#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003427 /* For records from the correct epoch, check whether their
3428 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003429 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3430 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003431 {
3432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3433 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3434 }
3435#endif
3436 }
3437#endif /* MBEDTLS_SSL_PROTO_DTLS */
3438
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003439 return( 0 );
3440}
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Paul Bakker5121ce52009-01-03 21:22:43 +00003442
Hanno Becker2fddd372019-07-10 14:37:41 +01003443#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3444static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3445{
3446 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3447
3448 /*
3449 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3450 * access the first byte of record content (handshake type), as we
3451 * have an active transform (possibly iv_len != 0), so use the
3452 * fact that the record header len is 13 instead.
3453 */
3454 if( rec_epoch == 0 &&
3455 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3456 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3457 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3458 ssl->in_left > 13 &&
3459 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3460 {
3461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3462 "from the same port" ) );
3463 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 }
3465
3466 return( 0 );
3467}
Hanno Becker2fddd372019-07-10 14:37:41 +01003468#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003470/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003471 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003472 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003473static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3474 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003475{
3476 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003479 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3482 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 ret = mbedtls_ssl_hw_record_read( ssl );
3487 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3490 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003491 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003492
3493 if( ret == 0 )
3494 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003497 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003499 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003500
Hanno Beckera18d1322018-01-03 14:27:32 +00003501 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003502 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003505
Hanno Beckera0e20d02019-05-15 14:03:01 +01003506#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003507 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3508 ssl->conf->ignore_unexpected_cid
3509 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3510 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003512 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003513 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003514#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003515
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 return( ret );
3517 }
3518
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003519 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003520 {
3521 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003522 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003523 }
3524
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003525 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003526 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003527
Hanno Beckera0e20d02019-05-15 14:03:01 +01003528#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003529 /* We have already checked the record content type
3530 * in ssl_parse_record_header(), failing or silently
3531 * dropping the record in the case of an unknown type.
3532 *
3533 * Since with the use of CIDs, the record content type
3534 * might change during decryption, re-check the record
3535 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003536 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3539 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3540 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003541#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003542
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003543 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003544 {
3545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3546 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003547 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003548 {
3549 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3551 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3552 }
3553#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3554
3555 ssl->nb_zero++;
3556
3557 /*
3558 * Three or more empty messages may be a DoS attack
3559 * (excessive CPU consumption).
3560 */
3561 if( ssl->nb_zero > 3 )
3562 {
3563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003564 "messages, possible DoS attack" ) );
3565 /* Treat the records as if they were not properly authenticated,
3566 * thereby failing the connection if we see more than allowed
3567 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003568 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3569 }
3570 }
3571 else
3572 ssl->nb_zero = 0;
3573
3574#if defined(MBEDTLS_SSL_PROTO_DTLS)
3575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3576 {
3577 ; /* in_ctr read from peer, not maintained internally */
3578 }
3579 else
3580#endif
3581 {
3582 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003583 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003584 if( ++ssl->in_ctr[i - 1] != 0 )
3585 break;
3586
3587 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003588 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003589 {
3590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3591 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3592 }
3593 }
3594
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 }
3596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003598 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003601 }
3602#endif
3603
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003604 /* Check actual (decrypted) record content length against
3605 * configured maximum. */
3606 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3607 {
3608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3609 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3610 }
3611
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003612 return( 0 );
3613}
3614
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003615/*
3616 * Read a record.
3617 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003618 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3619 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3620 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003621 */
Hanno Becker1097b342018-08-15 14:09:41 +01003622
3623/* Helper functions for mbedtls_ssl_read_record(). */
3624static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003625static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3626static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003627
Hanno Becker327c93b2018-08-15 13:56:18 +01003628int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003629 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003630{
Janos Follath865b3eb2019-12-16 11:46:15 +00003631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003634
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003635 if( ssl->keep_current_message == 0 )
3636 {
3637 do {
Simon Butcher99000142016-10-13 17:21:01 +01003638
Hanno Becker26994592018-08-15 14:14:59 +01003639 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003640 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003641 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003642
Hanno Beckere74d5562018-08-15 14:26:08 +01003643 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003644 {
Hanno Becker40f50842018-08-15 14:48:01 +01003645#if defined(MBEDTLS_SSL_PROTO_DTLS)
3646 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003647
Hanno Becker40f50842018-08-15 14:48:01 +01003648 /* We only check for buffered messages if the
3649 * current datagram is fully consumed. */
3650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003651 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003652 {
Hanno Becker40f50842018-08-15 14:48:01 +01003653 if( ssl_load_buffered_message( ssl ) == 0 )
3654 have_buffered = 1;
3655 }
3656
3657 if( have_buffered == 0 )
3658#endif /* MBEDTLS_SSL_PROTO_DTLS */
3659 {
3660 ret = ssl_get_next_record( ssl );
3661 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3662 continue;
3663
3664 if( ret != 0 )
3665 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003666 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003667 return( ret );
3668 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003669 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003670 }
3671
3672 ret = mbedtls_ssl_handle_message_type( ssl );
3673
Hanno Becker40f50842018-08-15 14:48:01 +01003674#if defined(MBEDTLS_SSL_PROTO_DTLS)
3675 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3676 {
3677 /* Buffer future message */
3678 ret = ssl_buffer_message( ssl );
3679 if( ret != 0 )
3680 return( ret );
3681
3682 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3683 }
3684#endif /* MBEDTLS_SSL_PROTO_DTLS */
3685
Hanno Becker90333da2017-10-10 11:27:13 +01003686 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3687 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003688
3689 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003690 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003691 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003692 return( ret );
3693 }
3694
Hanno Becker327c93b2018-08-15 13:56:18 +01003695 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003696 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003697 {
3698 mbedtls_ssl_update_handshake_status( ssl );
3699 }
Simon Butcher99000142016-10-13 17:21:01 +01003700 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003701 else
Simon Butcher99000142016-10-13 17:21:01 +01003702 {
Hanno Becker02f59072018-08-15 14:00:24 +01003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003704 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003705 }
3706
3707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3708
3709 return( 0 );
3710}
3711
Hanno Becker40f50842018-08-15 14:48:01 +01003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003713static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003714{
Hanno Becker40f50842018-08-15 14:48:01 +01003715 if( ssl->in_left > ssl->next_record_offset )
3716 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003717
Hanno Becker40f50842018-08-15 14:48:01 +01003718 return( 0 );
3719}
3720
3721static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3722{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003723 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003724 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003725 int ret = 0;
3726
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003727 if( hs == NULL )
3728 return( -1 );
3729
Hanno Beckere00ae372018-08-20 09:39:42 +01003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3731
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003732 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3733 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3734 {
3735 /* Check if we have seen a ChangeCipherSpec before.
3736 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003737 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3740 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003741 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003742 }
3743
Hanno Becker39b8bc92018-08-28 17:17:13 +01003744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003745 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3746 ssl->in_msglen = 1;
3747 ssl->in_msg[0] = 1;
3748
3749 /* As long as they are equal, the exact value doesn't matter. */
3750 ssl->in_left = 0;
3751 ssl->next_record_offset = 0;
3752
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003753 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003754 goto exit;
3755 }
Hanno Becker37f95322018-08-16 13:55:32 +01003756
Hanno Beckerb8f50142018-08-28 10:01:34 +01003757#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003758 /* Debug only */
3759 {
3760 unsigned offset;
3761 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3762 {
3763 hs_buf = &hs->buffering.hs[offset];
3764 if( hs_buf->is_valid == 1 )
3765 {
3766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3767 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003768 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003769 }
3770 }
3771 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003772#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003773
3774 /* Check if we have buffered and/or fully reassembled the
3775 * next handshake message. */
3776 hs_buf = &hs->buffering.hs[0];
3777 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3778 {
3779 /* Synthesize a record containing the buffered HS message. */
3780 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3781 ( hs_buf->data[2] << 8 ) |
3782 hs_buf->data[3];
3783
3784 /* Double-check that we haven't accidentally buffered
3785 * a message that doesn't fit into the input buffer. */
3786 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3789 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3790 }
3791
3792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3793 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3794 hs_buf->data, msg_len + 12 );
3795
3796 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3797 ssl->in_hslen = msg_len + 12;
3798 ssl->in_msglen = msg_len + 12;
3799 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3800
3801 ret = 0;
3802 goto exit;
3803 }
3804 else
3805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3807 hs->in_msg_seq ) );
3808 }
3809
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003810 ret = -1;
3811
3812exit:
3813
3814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3815 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003816}
3817
Hanno Beckera02b0b42018-08-21 17:20:27 +01003818static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3819 size_t desired )
3820{
3821 int offset;
3822 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3824 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003825
Hanno Becker01315ea2018-08-21 17:22:17 +01003826 /* Get rid of future records epoch first, if such exist. */
3827 ssl_free_buffered_record( ssl );
3828
3829 /* Check if we have enough space available now. */
3830 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3831 hs->buffering.total_bytes_buffered ) )
3832 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003834 return( 0 );
3835 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003836
Hanno Becker4f432ad2018-08-28 10:02:32 +01003837 /* We don't have enough space to buffer the next expected handshake
3838 * message. Remove buffers used for future messages to gain space,
3839 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003840 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3841 offset >= 0; offset-- )
3842 {
3843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3844 offset ) );
3845
Hanno Beckerb309b922018-08-23 13:18:05 +01003846 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003847
3848 /* Check if we have enough space available now. */
3849 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3850 hs->buffering.total_bytes_buffered ) )
3851 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003853 return( 0 );
3854 }
3855 }
3856
3857 return( -1 );
3858}
3859
Hanno Becker40f50842018-08-15 14:48:01 +01003860static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3861{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003862 int ret = 0;
3863 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3864
3865 if( hs == NULL )
3866 return( 0 );
3867
3868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3869
3870 switch( ssl->in_msgtype )
3871 {
3872 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003875 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003876 break;
3877
3878 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003879 {
3880 unsigned recv_msg_seq_offset;
3881 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3882 mbedtls_ssl_hs_buffer *hs_buf;
3883 size_t msg_len = ssl->in_hslen - 12;
3884
3885 /* We should never receive an old handshake
3886 * message - double-check nonetheless. */
3887 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3888 {
3889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3891 }
3892
3893 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3894 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3895 {
3896 /* Silently ignore -- message too far in the future */
3897 MBEDTLS_SSL_DEBUG_MSG( 2,
3898 ( "Ignore future HS message with sequence number %u, "
3899 "buffering window %u - %u",
3900 recv_msg_seq, ssl->handshake->in_msg_seq,
3901 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3902
3903 goto exit;
3904 }
3905
3906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3907 recv_msg_seq, recv_msg_seq_offset ) );
3908
3909 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3910
3911 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003912 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003913 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003914 size_t reassembly_buf_sz;
3915
Hanno Becker37f95322018-08-16 13:55:32 +01003916 hs_buf->is_fragmented =
3917 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3918
3919 /* We copy the message back into the input buffer
3920 * after reassembly, so check that it's not too large.
3921 * This is an implementation-specific limitation
3922 * and not one from the standard, hence it is not
3923 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003924 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003925 {
3926 /* Ignore message */
3927 goto exit;
3928 }
3929
Hanno Beckere0b150f2018-08-21 15:51:03 +01003930 /* Check if we have enough space to buffer the message. */
3931 if( hs->buffering.total_bytes_buffered >
3932 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3933 {
3934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3936 }
3937
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003938 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3939 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003940
3941 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3942 hs->buffering.total_bytes_buffered ) )
3943 {
3944 if( recv_msg_seq_offset > 0 )
3945 {
3946 /* If we can't buffer a future message because
3947 * of space limitations -- ignore. */
3948 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",
3949 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
3950 (unsigned) hs->buffering.total_bytes_buffered ) );
3951 goto exit;
3952 }
Hanno Beckere1801392018-08-21 16:51:05 +01003953 else
3954 {
3955 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",
3956 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
3957 (unsigned) hs->buffering.total_bytes_buffered ) );
3958 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003959
Hanno Beckera02b0b42018-08-21 17:20:27 +01003960 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003961 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003962 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",
3963 (unsigned) msg_len,
3964 (unsigned) reassembly_buf_sz,
3965 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003966 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003967 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3968 goto exit;
3969 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003970 }
3971
3972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
3973 msg_len ) );
3974
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003975 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3976 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003977 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003978 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003979 goto exit;
3980 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003981 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003982
3983 /* Prepare final header: copy msg_type, length and message_seq,
3984 * then add standardised fragment_offset and fragment_length */
3985 memcpy( hs_buf->data, ssl->in_msg, 6 );
3986 memset( hs_buf->data + 6, 0, 3 );
3987 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3988
3989 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01003990
3991 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003992 }
3993 else
3994 {
3995 /* Make sure msg_type and length are consistent */
3996 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
3997 {
3998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
3999 /* Ignore */
4000 goto exit;
4001 }
4002 }
4003
Hanno Becker4422bbb2018-08-20 09:40:19 +01004004 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004005 {
4006 size_t frag_len, frag_off;
4007 unsigned char * const msg = hs_buf->data + 12;
4008
4009 /*
4010 * Check and copy current fragment
4011 */
4012
4013 /* Validation of header fields already done in
4014 * mbedtls_ssl_prepare_handshake_record(). */
4015 frag_off = ssl_get_hs_frag_off( ssl );
4016 frag_len = ssl_get_hs_frag_len( ssl );
4017
4018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4019 frag_off, frag_len ) );
4020 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4021
4022 if( hs_buf->is_fragmented )
4023 {
4024 unsigned char * const bitmask = msg + msg_len;
4025 ssl_bitmask_set( bitmask, frag_off, frag_len );
4026 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4027 msg_len ) == 0 );
4028 }
4029 else
4030 {
4031 hs_buf->is_complete = 1;
4032 }
4033
4034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4035 hs_buf->is_complete ? "" : "not yet " ) );
4036 }
4037
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004038 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004039 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004040
4041 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004042 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004043 break;
4044 }
4045
4046exit:
4047
4048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4049 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004050}
4051#endif /* MBEDTLS_SSL_PROTO_DTLS */
4052
Hanno Becker1097b342018-08-15 14:09:41 +01004053static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004054{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004055 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004056 * Consume last content-layer message and potentially
4057 * update in_msglen which keeps track of the contents'
4058 * consumption state.
4059 *
4060 * (1) Handshake messages:
4061 * Remove last handshake message, move content
4062 * and adapt in_msglen.
4063 *
4064 * (2) Alert messages:
4065 * Consume whole record content, in_msglen = 0.
4066 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004067 * (3) Change cipher spec:
4068 * Consume whole record content, in_msglen = 0.
4069 *
4070 * (4) Application data:
4071 * Don't do anything - the record layer provides
4072 * the application data as a stream transport
4073 * and consumes through mbedtls_ssl_read only.
4074 *
4075 */
4076
4077 /* Case (1): Handshake messages */
4078 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004079 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004080 /* Hard assertion to be sure that no application data
4081 * is in flight, as corrupting ssl->in_msglen during
4082 * ssl->in_offt != NULL is fatal. */
4083 if( ssl->in_offt != NULL )
4084 {
4085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4087 }
4088
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004089 /*
4090 * Get next Handshake message in the current record
4091 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004092
Hanno Becker4a810fb2017-05-24 16:27:30 +01004093 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004094 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004095 * current handshake content: If DTLS handshake
4096 * fragmentation is used, that's the fragment
4097 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004098 * size here is faulty and should be changed at
4099 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004100 * (2) While it doesn't seem to cause problems, one
4101 * has to be very careful not to assume that in_hslen
4102 * is always <= in_msglen in a sensible communication.
4103 * Again, it's wrong for DTLS handshake fragmentation.
4104 * The following check is therefore mandatory, and
4105 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004106 * Additionally, ssl->in_hslen might be arbitrarily out of
4107 * bounds after handling a DTLS message with an unexpected
4108 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004109 */
4110 if( ssl->in_hslen < ssl->in_msglen )
4111 {
4112 ssl->in_msglen -= ssl->in_hslen;
4113 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4114 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004115
Hanno Becker4a810fb2017-05-24 16:27:30 +01004116 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4117 ssl->in_msg, ssl->in_msglen );
4118 }
4119 else
4120 {
4121 ssl->in_msglen = 0;
4122 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004123
Hanno Becker4a810fb2017-05-24 16:27:30 +01004124 ssl->in_hslen = 0;
4125 }
4126 /* Case (4): Application data */
4127 else if( ssl->in_offt != NULL )
4128 {
4129 return( 0 );
4130 }
4131 /* Everything else (CCS & Alerts) */
4132 else
4133 {
4134 ssl->in_msglen = 0;
4135 }
4136
Hanno Becker1097b342018-08-15 14:09:41 +01004137 return( 0 );
4138}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004139
Hanno Beckere74d5562018-08-15 14:26:08 +01004140static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4141{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004142 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004143 return( 1 );
4144
4145 return( 0 );
4146}
4147
Hanno Becker5f066e72018-08-16 14:56:31 +01004148#if defined(MBEDTLS_SSL_PROTO_DTLS)
4149
4150static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4151{
4152 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4153 if( hs == NULL )
4154 return;
4155
Hanno Becker01315ea2018-08-21 17:22:17 +01004156 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004157 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004158 hs->buffering.total_bytes_buffered -=
4159 hs->buffering.future_record.len;
4160
4161 mbedtls_free( hs->buffering.future_record.data );
4162 hs->buffering.future_record.data = NULL;
4163 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004164}
4165
4166static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4167{
4168 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4169 unsigned char * rec;
4170 size_t rec_len;
4171 unsigned rec_epoch;
4172
4173 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4174 return( 0 );
4175
4176 if( hs == NULL )
4177 return( 0 );
4178
Hanno Becker5f066e72018-08-16 14:56:31 +01004179 rec = hs->buffering.future_record.data;
4180 rec_len = hs->buffering.future_record.len;
4181 rec_epoch = hs->buffering.future_record.epoch;
4182
4183 if( rec == NULL )
4184 return( 0 );
4185
Hanno Becker4cb782d2018-08-20 11:19:05 +01004186 /* Only consider loading future records if the
4187 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004188 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004189 return( 0 );
4190
Hanno Becker5f066e72018-08-16 14:56:31 +01004191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4192
4193 if( rec_epoch != ssl->in_epoch )
4194 {
4195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4196 goto exit;
4197 }
4198
4199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4200
4201 /* Double-check that the record is not too large */
4202 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4203 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4204 {
4205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4207 }
4208
4209 memcpy( ssl->in_hdr, rec, rec_len );
4210 ssl->in_left = rec_len;
4211 ssl->next_record_offset = 0;
4212
4213 ssl_free_buffered_record( ssl );
4214
4215exit:
4216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4217 return( 0 );
4218}
4219
Hanno Becker519f15d2019-07-11 12:43:20 +01004220static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4221 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004222{
4223 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004224
4225 /* Don't buffer future records outside handshakes. */
4226 if( hs == NULL )
4227 return( 0 );
4228
4229 /* Only buffer handshake records (we are only interested
4230 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004231 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004232 return( 0 );
4233
4234 /* Don't buffer more than one future epoch record. */
4235 if( hs->buffering.future_record.data != NULL )
4236 return( 0 );
4237
Hanno Becker01315ea2018-08-21 17:22:17 +01004238 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004239 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004240 hs->buffering.total_bytes_buffered ) )
4241 {
4242 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 +01004243 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004244 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004245 return( 0 );
4246 }
4247
Hanno Becker5f066e72018-08-16 14:56:31 +01004248 /* Buffer record */
4249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4250 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004251 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004252
4253 /* ssl_parse_record_header() only considers records
4254 * of the next epoch as candidates for buffering. */
4255 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004256 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004257
4258 hs->buffering.future_record.data =
4259 mbedtls_calloc( 1, hs->buffering.future_record.len );
4260 if( hs->buffering.future_record.data == NULL )
4261 {
4262 /* If we run out of RAM trying to buffer a
4263 * record from the next epoch, just ignore. */
4264 return( 0 );
4265 }
4266
Hanno Becker519f15d2019-07-11 12:43:20 +01004267 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004268
Hanno Becker519f15d2019-07-11 12:43:20 +01004269 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004270 return( 0 );
4271}
4272
4273#endif /* MBEDTLS_SSL_PROTO_DTLS */
4274
Hanno Beckere74d5562018-08-15 14:26:08 +01004275static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004276{
Janos Follath865b3eb2019-12-16 11:46:15 +00004277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004278 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004279
Hanno Becker5f066e72018-08-16 14:56:31 +01004280#if defined(MBEDTLS_SSL_PROTO_DTLS)
4281 /* We might have buffered a future record; if so,
4282 * and if the epoch matches now, load it.
4283 * On success, this call will set ssl->in_left to
4284 * the length of the buffered record, so that
4285 * the calls to ssl_fetch_input() below will
4286 * essentially be no-ops. */
4287 ret = ssl_load_buffered_record( ssl );
4288 if( ret != 0 )
4289 return( ret );
4290#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004291
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004292 /* Ensure that we have enough space available for the default form
4293 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4294 * with no space for CIDs counted in). */
4295 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4296 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004299 return( ret );
4300 }
4301
Hanno Beckere5e7e782019-07-11 12:29:35 +01004302 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4303 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004307 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004308 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4309 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004310 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004311 if( ret != 0 )
4312 return( ret );
4313
4314 /* Fall through to handling of unexpected records */
4315 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4316 }
4317
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004318 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4319 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004320#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004321 /* Reset in pointers to default state for TLS/DTLS records,
4322 * assuming no CID and no offset between record content and
4323 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004324 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004325
Hanno Becker7ae20e02019-07-12 08:33:49 +01004326 /* Setup internal message pointers from record structure. */
4327 ssl->in_msgtype = rec.type;
4328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4329 ssl->in_len = ssl->in_cid + rec.cid_len;
4330#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4331 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4332 ssl->in_msglen = rec.data_len;
4333
Hanno Becker2fddd372019-07-10 14:37:41 +01004334 ret = ssl_check_client_reconnect( ssl );
4335 if( ret != 0 )
4336 return( ret );
4337#endif
4338
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004339 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004340 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004341
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4343 "(header)" ) );
4344 }
4345 else
4346 {
4347 /* Skip invalid record and the rest of the datagram */
4348 ssl->next_record_offset = 0;
4349 ssl->in_left = 0;
4350
4351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4352 "(header)" ) );
4353 }
4354
4355 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004356 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004357 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004358 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004359#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004360 {
4361 return( ret );
4362 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004363 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004366 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004367 {
Hanno Beckera8814792019-07-10 15:01:45 +01004368 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004369 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004370 if( ssl->next_record_offset < ssl->in_left )
4371 {
4372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4373 }
4374 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004375 else
4376#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004377 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004378 /*
4379 * Fetch record contents from underlying transport.
4380 */
Hanno Beckera3175662019-07-11 12:50:29 +01004381 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004382 if( ret != 0 )
4383 {
4384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4385 return( ret );
4386 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004387
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004388 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004389 }
4390
4391 /*
4392 * Decrypt record contents.
4393 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004394
Hanno Beckerfdf66042019-07-11 13:07:45 +01004395 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004398 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004399 {
4400 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004401 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004402 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004403 /* Except when waiting for Finished as a bad mac here
4404 * probably means something went wrong in the handshake
4405 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4406 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4407 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4408 {
4409#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4410 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4411 {
4412 mbedtls_ssl_send_alert_message( ssl,
4413 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4414 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4415 }
4416#endif
4417 return( ret );
4418 }
4419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004421 if( ssl->conf->badmac_limit != 0 &&
4422 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4425 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004426 }
4427#endif
4428
Hanno Becker4a810fb2017-05-24 16:27:30 +01004429 /* As above, invalid records cause
4430 * dismissal of the whole datagram. */
4431
4432 ssl->next_record_offset = 0;
4433 ssl->in_left = 0;
4434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004436 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004437 }
4438
4439 return( ret );
4440 }
4441 else
4442#endif
4443 {
4444 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4446 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 mbedtls_ssl_send_alert_message( ssl,
4449 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4450 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004451 }
4452#endif
4453 return( ret );
4454 }
4455 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004456
Hanno Becker44d89b22019-07-12 09:40:44 +01004457
4458 /* Reset in pointers to default state for TLS/DTLS records,
4459 * assuming no CID and no offset between record content and
4460 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004461 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004462#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4463 ssl->in_len = ssl->in_cid + rec.cid_len;
4464#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004465 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004466
Hanno Becker8685c822019-07-12 09:37:30 +01004467 /* The record content type may change during decryption,
4468 * so re-read it. */
4469 ssl->in_msgtype = rec.type;
4470 /* Also update the input buffer, because unfortunately
4471 * the server-side ssl_parse_client_hello() reparses the
4472 * record header when receiving a ClientHello initiating
4473 * a renegotiation. */
4474 ssl->in_hdr[0] = rec.type;
4475 ssl->in_msg = rec.buf + rec.data_offset;
4476 ssl->in_msglen = rec.data_len;
4477 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4478 ssl->in_len[1] = (unsigned char)( rec.data_len );
4479
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004480#if defined(MBEDTLS_ZLIB_SUPPORT)
4481 if( ssl->transform_in != NULL &&
4482 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4483 {
4484 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4485 {
4486 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4487 return( ret );
4488 }
4489
4490 /* Check actual (decompress) record content length against
4491 * configured maximum. */
4492 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4493 {
4494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4495 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4496 }
4497 }
4498#endif /* MBEDTLS_ZLIB_SUPPORT */
4499
Simon Butcher99000142016-10-13 17:21:01 +01004500 return( 0 );
4501}
4502
4503int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4504{
Janos Follath865b3eb2019-12-16 11:46:15 +00004505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004506
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004507 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004508 * Handle particular types of records
4509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 {
Simon Butcher99000142016-10-13 17:21:01 +01004512 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4513 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004514 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004516 }
4517
Hanno Beckere678eaa2018-08-21 14:57:46 +01004518 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004519 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004520 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004521 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4523 ssl->in_msglen ) );
4524 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004525 }
4526
Hanno Beckere678eaa2018-08-21 14:57:46 +01004527 if( ssl->in_msg[0] != 1 )
4528 {
4529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4530 ssl->in_msg[0] ) );
4531 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4532 }
4533
4534#if defined(MBEDTLS_SSL_PROTO_DTLS)
4535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4536 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4537 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4538 {
4539 if( ssl->handshake == NULL )
4540 {
4541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4542 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4543 }
4544
4545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4546 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4547 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004548#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004549 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004553 if( ssl->in_msglen != 2 )
4554 {
4555 /* Note: Standard allows for more than one 2 byte alert
4556 to be packed in a single message, but Mbed TLS doesn't
4557 currently support this. */
4558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4559 ssl->in_msglen ) );
4560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4561 }
4562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 ssl->in_msg[0], ssl->in_msg[1] ) );
4565
4566 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004567 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004572 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 }
4575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4577 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4580 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004582
4583#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4584 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4585 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4586 {
Hanno Becker90333da2017-10-10 11:27:13 +01004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004588 /* Will be handled when trying to parse ServerHello */
4589 return( 0 );
4590 }
4591#endif
4592
4593#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4594 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4595 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4596 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4597 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4598 {
4599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4600 /* Will be handled in mbedtls_ssl_parse_certificate() */
4601 return( 0 );
4602 }
4603#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4604
4605 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004606 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 }
4608
Hanno Beckerc76c6192017-06-06 10:03:17 +01004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004610 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004611 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004612 /* Drop unexpected ApplicationData records,
4613 * except at the beginning of renegotiations */
4614 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4615 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4616#if defined(MBEDTLS_SSL_RENEGOTIATION)
4617 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4618 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004619#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004620 )
4621 {
4622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4623 return( MBEDTLS_ERR_SSL_NON_FATAL );
4624 }
4625
4626 if( ssl->handshake != NULL &&
4627 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4628 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004629 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004630 }
4631 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004632#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004633
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 return( 0 );
4635}
4636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004638{
irwir6c0da642019-09-26 21:07:41 +03004639 return( mbedtls_ssl_send_alert_message( ssl,
4640 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4641 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004642}
4643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004645 unsigned char level,
4646 unsigned char message )
4647{
Janos Follath865b3eb2019-12-16 11:46:15 +00004648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004649
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004650 if( ssl == NULL || ssl->conf == NULL )
4651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004657 ssl->out_msglen = 2;
4658 ssl->out_msg[0] = level;
4659 ssl->out_msg[1] = message;
4660
Hanno Becker67bc7c32018-08-06 11:33:50 +01004661 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004664 return( ret );
4665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004667
4668 return( 0 );
4669}
4670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004672{
Janos Follath865b3eb2019-12-16 11:46:15 +00004673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 ssl->out_msglen = 1;
4679 ssl->out_msg[0] = 1;
4680
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 ssl->state++;
4682
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004683 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004684 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004685 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 return( ret );
4687 }
4688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004690
4691 return( 0 );
4692}
4693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004695{
Janos Follath865b3eb2019-12-16 11:46:15 +00004696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699
Hanno Becker327c93b2018-08-15 13:56:18 +01004700 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 return( ret );
4704 }
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004709 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4710 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 }
4713
Hanno Beckere678eaa2018-08-21 14:57:46 +01004714 /* CCS records are only accepted if they have length 1 and content '1',
4715 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004716
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004717 /*
4718 * Switch to our negotiated transform and session parameters for inbound
4719 * data.
4720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004722 ssl->transform_in = ssl->transform_negotiate;
4723 ssl->session_in = ssl->session_negotiate;
4724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004726 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004729 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004730#endif
4731
4732 /* Increment epoch */
4733 if( ++ssl->in_epoch == 0 )
4734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004736 /* This is highly unlikely to happen for legitimate reasons, so
4737 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004739 }
4740 }
4741 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004743 memset( ssl->in_ctr, 0, 8 );
4744
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004745 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4748 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004753 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4754 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004756 }
4757 }
4758#endif
4759
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 ssl->state++;
4761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004763
4764 return( 0 );
4765}
4766
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004767/* Once ssl->out_hdr as the address of the beginning of the
4768 * next outgoing record is set, deduce the other pointers.
4769 *
4770 * Note: For TLS, we save the implicit record sequence number
4771 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4772 * and the caller has to make sure there's space for this.
4773 */
4774
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004775void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4776 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004777{
4778#if defined(MBEDTLS_SSL_PROTO_DTLS)
4779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4780 {
4781 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004782#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004783 ssl->out_cid = ssl->out_ctr + 8;
4784 ssl->out_len = ssl->out_cid;
4785 if( transform != NULL )
4786 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004787#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004788 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004789#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004790 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004791 }
4792 else
4793#endif
4794 {
4795 ssl->out_ctr = ssl->out_hdr - 8;
4796 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004797#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004798 ssl->out_cid = ssl->out_len;
4799#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004800 ssl->out_iv = ssl->out_hdr + 5;
4801 }
4802
4803 /* Adjust out_msg to make space for explicit IV, if used. */
4804 if( transform != NULL &&
4805 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
4806 {
4807 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
4808 }
4809 else
4810 ssl->out_msg = ssl->out_iv;
4811}
4812
4813/* Once ssl->in_hdr as the address of the beginning of the
4814 * next incoming record is set, deduce the other pointers.
4815 *
4816 * Note: For TLS, we save the implicit record sequence number
4817 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4818 * and the caller has to make sure there's space for this.
4819 */
4820
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004821void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004822{
Hanno Becker79594fd2019-05-08 09:38:41 +01004823 /* This function sets the pointers to match the case
4824 * of unprotected TLS/DTLS records, with both ssl->in_iv
4825 * and ssl->in_msg pointing to the beginning of the record
4826 * content.
4827 *
4828 * When decrypting a protected record, ssl->in_msg
4829 * will be shifted to point to the beginning of the
4830 * record plaintext.
4831 */
4832
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004833#if defined(MBEDTLS_SSL_PROTO_DTLS)
4834 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4835 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004836 /* This sets the header pointers to match records
4837 * without CID. When we receive a record containing
4838 * a CID, the fields are shifted accordingly in
4839 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004840 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004841#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004842 ssl->in_cid = ssl->in_ctr + 8;
4843 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004844#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004845 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004847 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004848 }
4849 else
4850#endif
4851 {
4852 ssl->in_ctr = ssl->in_hdr - 8;
4853 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004854#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004855 ssl->in_cid = ssl->in_len;
4856#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004857 ssl->in_iv = ssl->in_hdr + 5;
4858 }
4859
Hanno Becker79594fd2019-05-08 09:38:41 +01004860 /* This will be adjusted at record decryption time. */
4861 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004862}
4863
Paul Bakker5121ce52009-01-03 21:22:43 +00004864/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004865 * Setup an SSL context
4866 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004867
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004868void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004869{
4870 /* Set the incoming and outgoing record pointers. */
4871#if defined(MBEDTLS_SSL_PROTO_DTLS)
4872 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4873 {
4874 ssl->out_hdr = ssl->out_buf;
4875 ssl->in_hdr = ssl->in_buf;
4876 }
4877 else
4878#endif /* MBEDTLS_SSL_PROTO_DTLS */
4879 {
4880 ssl->out_hdr = ssl->out_buf + 8;
4881 ssl->in_hdr = ssl->in_buf + 8;
4882 }
4883
4884 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004885 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4886 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004887}
4888
Paul Bakker5121ce52009-01-03 21:22:43 +00004889/*
4890 * SSL get accessors
4891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004893{
4894 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4895}
4896
Hanno Becker8b170a02017-10-10 11:51:19 +01004897int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4898{
4899 /*
4900 * Case A: We're currently holding back
4901 * a message for further processing.
4902 */
4903
4904 if( ssl->keep_current_message == 1 )
4905 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004907 return( 1 );
4908 }
4909
4910 /*
4911 * Case B: Further records are pending in the current datagram.
4912 */
4913
4914#if defined(MBEDTLS_SSL_PROTO_DTLS)
4915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4916 ssl->in_left > ssl->next_record_offset )
4917 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004919 return( 1 );
4920 }
4921#endif /* MBEDTLS_SSL_PROTO_DTLS */
4922
4923 /*
4924 * Case C: A handshake message is being processed.
4925 */
4926
Hanno Becker8b170a02017-10-10 11:51:19 +01004927 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4928 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004930 return( 1 );
4931 }
4932
4933 /*
4934 * Case D: An application data message is being processed
4935 */
4936 if( ssl->in_offt != NULL )
4937 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004939 return( 1 );
4940 }
4941
4942 /*
4943 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004944 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004945 * we implement support for multiple alerts in single records.
4946 */
4947
4948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4949 return( 0 );
4950}
4951
Paul Bakker43ca69c2011-01-15 17:35:19 +00004952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004954{
Hanno Becker3136ede2018-08-17 15:28:19 +01004955 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004957 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004958
Hanno Becker5903de42019-05-03 14:46:38 +01004959 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4960
Hanno Becker78640902018-08-13 16:35:15 +01004961 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004962 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964#if defined(MBEDTLS_ZLIB_SUPPORT)
4965 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
4966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004967#endif
4968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 case MBEDTLS_MODE_GCM:
4972 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004973 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004975 transform_expansion = transform->minlen;
4976 break;
4977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004979
4980 block_size = mbedtls_cipher_get_block_size(
4981 &transform->cipher_ctx_enc );
4982
Hanno Becker3136ede2018-08-17 15:28:19 +01004983 /* Expansion due to the addition of the MAC. */
4984 transform_expansion += transform->maclen;
4985
4986 /* Expansion due to the addition of CBC padding;
4987 * Theoretically up to 256 bytes, but we never use
4988 * more than the block size of the underlying cipher. */
4989 transform_expansion += block_size;
4990
4991 /* For TLS 1.1 or higher, an explicit IV is added
4992 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01004993#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
4994 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01004995 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004996#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004997
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004998 break;
4999
5000 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005003 }
5004
Hanno Beckera0e20d02019-05-15 14:03:01 +01005005#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005006 if( transform->out_cid_len != 0 )
5007 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005008#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005009
Hanno Becker5903de42019-05-03 14:46:38 +01005010 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005011}
5012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005014/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005015 * Check record counters and renegotiate if they're above the limit.
5016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005018{
Hanno Beckerdd772292020-02-05 10:38:31 +00005019 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005020 int in_ctr_cmp;
5021 int out_ctr_cmp;
5022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5024 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005025 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005026 {
5027 return( 0 );
5028 }
5029
Andres AG2196c7f2016-12-15 17:01:16 +00005030 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5031 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005032 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005033 ssl->conf->renego_period + ep_len, 8 - ep_len );
5034
5035 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005036 {
5037 return( 0 );
5038 }
5039
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005044
5045/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005046 * Receive application data decrypted from the SSL layer
5047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005049{
Janos Follath865b3eb2019-12-16 11:46:15 +00005050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005051 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005052
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005053 if( ssl == NULL || ssl->conf == NULL )
5054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005062 return( ret );
5063
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005064 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005066 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005067 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005068 return( ret );
5069 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005070 }
5071#endif
5072
Hanno Becker4a810fb2017-05-24 16:27:30 +01005073 /*
5074 * Check if renegotiation is necessary and/or handshake is
5075 * in process. If yes, perform/continue, and fall through
5076 * if an unexpected packet is received while the client
5077 * is waiting for the ServerHello.
5078 *
5079 * (There is no equivalent to the last condition on
5080 * the server-side as it is not treated as within
5081 * a handshake while waiting for the ClientHello
5082 * after a renegotiation request.)
5083 */
5084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005086 ret = ssl_check_ctr_renegotiate( ssl );
5087 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5088 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005091 return( ret );
5092 }
5093#endif
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005098 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5099 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005102 return( ret );
5103 }
5104 }
5105
Hanno Beckere41158b2017-10-23 13:30:32 +01005106 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005107 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005108 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005109 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005110 if( ssl->f_get_timer != NULL &&
5111 ssl->f_get_timer( ssl->p_timer ) == -1 )
5112 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005113 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005114 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005115
Hanno Becker327c93b2018-08-15 13:56:18 +01005116 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005118 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5119 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005120
Hanno Becker4a810fb2017-05-24 16:27:30 +01005121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5122 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 }
5124
5125 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005127 {
5128 /*
5129 * OpenSSL sends empty messages to randomize the IV
5130 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005131 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005134 return( 0 );
5135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005137 return( ret );
5138 }
5139 }
5140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005144
Hanno Becker4a810fb2017-05-24 16:27:30 +01005145 /*
5146 * - For client-side, expect SERVER_HELLO_REQUEST.
5147 * - For server-side, expect CLIENT_HELLO.
5148 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5149 */
5150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005152 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005154 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005157
5158 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005161 {
5162 continue;
5163 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005166 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005167#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005168
Hanno Becker4a810fb2017-05-24 16:27:30 +01005169#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005170 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005174
5175 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005178 {
5179 continue;
5180 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005183 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005184#endif /* MBEDTLS_SSL_SRV_C */
5185
Hanno Becker21df7f92017-10-17 11:03:26 +01005186#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005187 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005188 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5189 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5190 ssl->conf->allow_legacy_renegotiation ==
5191 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5192 {
5193 /*
5194 * Accept renegotiation request
5195 */
Paul Bakker48916f92012-09-16 19:57:18 +00005196
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005197 /* DTLS clients need to know renego is server-initiated */
5198#if defined(MBEDTLS_SSL_PROTO_DTLS)
5199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5200 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5201 {
5202 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5203 }
5204#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005205 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005206 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5207 ret != 0 )
5208 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5210 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005211 return( ret );
5212 }
5213 }
5214 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005215#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005216 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005217 /*
5218 * Refuse renegotiation
5219 */
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223#if defined(MBEDTLS_SSL_PROTO_SSL3)
5224 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005225 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005226 /* SSLv3 does not have a "no_renegotiation" warning, so
5227 we send a fatal alert and abort the connection. */
5228 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5229 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005231 }
5232 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5234#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5235 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5236 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5239 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5240 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005241 {
5242 return( ret );
5243 }
Paul Bakker48916f92012-09-16 19:57:18 +00005244 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5247 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005251 }
Paul Bakker48916f92012-09-16 19:57:18 +00005252 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005253
Hanno Becker90333da2017-10-10 11:27:13 +01005254 /* At this point, we don't know whether the renegotiation has been
5255 * completed or not. The cases to consider are the following:
5256 * 1) The renegotiation is complete. In this case, no new record
5257 * has been read yet.
5258 * 2) The renegotiation is incomplete because the client received
5259 * an application data record while awaiting the ServerHello.
5260 * 3) The renegotiation is incomplete because the client received
5261 * a non-handshake, non-application data message while awaiting
5262 * the ServerHello.
5263 * In each of these case, looping will be the proper action:
5264 * - For 1), the next iteration will read a new record and check
5265 * if it's application data.
5266 * - For 2), the loop condition isn't satisfied as application data
5267 * is present, hence continue is the same as break
5268 * - For 3), the loop condition is satisfied and read_record
5269 * will re-deliver the message that was held back by the client
5270 * when expecting the ServerHello.
5271 */
5272 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005273 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005274#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005276 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005277 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005278 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005279 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005282 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005284 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005285 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005286 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5290 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005293 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005294 }
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5299 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 }
5301
5302 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005303
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005304 /* We're going to return something now, cancel timer,
5305 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005307 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005308
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005309#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005310 /* If we requested renego but received AppData, resend HelloRequest.
5311 * Do it now, after setting in_offt, to avoid taking this branch
5312 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005314 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005316 {
Hanno Becker786300f2020-02-05 10:46:40 +00005317 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005318 {
Hanno Becker786300f2020-02-05 10:46:40 +00005319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5320 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005321 return( ret );
5322 }
5323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005325#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 }
5327
5328 n = ( len < ssl->in_msglen )
5329 ? len : ssl->in_msglen;
5330
5331 memcpy( buf, ssl->in_offt, n );
5332 ssl->in_msglen -= n;
5333
5334 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005335 {
5336 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005338 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005340 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005341 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005342 /* more data available */
5343 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005347
Paul Bakker23986e52011-04-24 08:57:21 +00005348 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005349}
5350
5351/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005352 * Send application data to be encrypted by the SSL layer, taking care of max
5353 * fragment length and buffer size.
5354 *
5355 * According to RFC 5246 Section 6.2.1:
5356 *
5357 * Zero-length fragments of Application data MAY be sent as they are
5358 * potentially useful as a traffic analysis countermeasure.
5359 *
5360 * Therefore, it is possible that the input message length is 0 and the
5361 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005363static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005364 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005365{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005366 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5367 const size_t max_len = (size_t) ret;
5368
5369 if( ret < 0 )
5370 {
5371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5372 return( ret );
5373 }
5374
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005375 if( len > max_len )
5376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005381 "maximum fragment length: %d > %d",
5382 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005384 }
5385 else
5386#endif
5387 len = max_len;
5388 }
Paul Bakker887bd502011-06-08 13:10:54 +00005389
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 if( ssl->out_left != 0 )
5391 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005392 /*
5393 * The user has previously tried to send the data and
5394 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5395 * written. In this case, we expect the high-level write function
5396 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 return( ret );
5402 }
5403 }
Paul Bakker887bd502011-06-08 13:10:54 +00005404 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005405 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005406 /*
5407 * The user is trying to send a message the first time, so we need to
5408 * copy the data into the internal buffers and setup the data structure
5409 * to keep track of partial writes
5410 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005411 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005413 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005414
Hanno Becker67bc7c32018-08-06 11:33:50 +01005415 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005418 return( ret );
5419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 }
5421
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005422 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005423}
5424
5425/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005426 * Write application data, doing 1/n-1 splitting if necessary.
5427 *
5428 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005429 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005430 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005433static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005434 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005435{
Janos Follath865b3eb2019-12-16 11:46:15 +00005436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005437
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005438 if( ssl->conf->cbc_record_splitting ==
5439 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005440 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5442 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5443 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005444 {
5445 return( ssl_write_real( ssl, buf, len ) );
5446 }
5447
5448 if( ssl->split_done == 0 )
5449 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005450 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005451 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005452 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005453 }
5454
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005455 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5456 return( ret );
5457 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005458
5459 return( ret + 1 );
5460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005462
5463/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005464 * Write application data (public-facing wrapper)
5465 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005466int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005467{
Janos Follath865b3eb2019-12-16 11:46:15 +00005468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005469
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005471
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005472 if( ssl == NULL || ssl->conf == NULL )
5473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5474
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005475#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005476 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5477 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005478 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005479 return( ret );
5480 }
5481#endif
5482
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005483 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005484 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005485 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005486 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005488 return( ret );
5489 }
5490 }
5491
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005492#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005493 ret = ssl_write_split( ssl, buf, len );
5494#else
5495 ret = ssl_write_real( ssl, buf, len );
5496#endif
5497
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005499
5500 return( ret );
5501}
5502
5503/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 * Notify the peer that the connection is being closed
5505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005507{
Janos Follath865b3eb2019-12-16 11:46:15 +00005508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005509
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005510 if( ssl == NULL || ssl->conf == NULL )
5511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005514
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005515 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5521 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5522 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 return( ret );
5526 }
5527 }
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005530
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005531 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005532}
5533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005535{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005536 if( transform == NULL )
5537 return;
5538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005540 deflateEnd( &transform->ctx_deflate );
5541 inflateEnd( &transform->ctx_inflate );
5542#endif
5543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5545 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005546
Hanno Beckerd56ed242018-01-03 15:32:51 +00005547#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 mbedtls_md_free( &transform->md_ctx_enc );
5549 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005550#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005551
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005552 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005553}
5554
Hanno Becker0271f962018-08-16 13:23:47 +01005555#if defined(MBEDTLS_SSL_PROTO_DTLS)
5556
Hanno Becker533ab5f2020-02-05 10:49:13 +00005557void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005558{
5559 unsigned offset;
5560 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5561
5562 if( hs == NULL )
5563 return;
5564
Hanno Becker283f5ef2018-08-24 09:34:47 +01005565 ssl_free_buffered_record( ssl );
5566
Hanno Becker0271f962018-08-16 13:23:47 +01005567 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005568 ssl_buffering_free_slot( ssl, offset );
5569}
5570
5571static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5572 uint8_t slot )
5573{
5574 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5575 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005576
5577 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5578 return;
5579
Hanno Beckere605b192018-08-21 15:59:07 +01005580 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005581 {
Hanno Beckere605b192018-08-21 15:59:07 +01005582 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005583 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005584 mbedtls_free( hs_buf->data );
5585 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005586 }
5587}
5588
5589#endif /* MBEDTLS_SSL_PROTO_DTLS */
5590
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005591/*
5592 * Convert version numbers to/from wire format
5593 * and, for DTLS, to/from TLS equivalent.
5594 *
5595 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005596 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005597 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5598 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005601 unsigned char ver[2] )
5602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603#if defined(MBEDTLS_SSL_PROTO_DTLS)
5604 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005607 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5608
5609 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5610 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5611 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005612 else
5613#else
5614 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005615#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005616 {
5617 ver[0] = (unsigned char) major;
5618 ver[1] = (unsigned char) minor;
5619 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005620}
5621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005623 const unsigned char ver[2] )
5624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625#if defined(MBEDTLS_SSL_PROTO_DTLS)
5626 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005627 {
5628 *major = 255 - ver[0] + 2;
5629 *minor = 255 - ver[1] + 1;
5630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005632 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5633 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005634 else
5635#else
5636 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005637#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005638 {
5639 *major = ver[0];
5640 *minor = ver[1];
5641 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005642}
5643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644#endif /* MBEDTLS_SSL_TLS_C */