blob: 4ae1b8f0863fe3f7f41012bff046c26273aa831d [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 );
Darryl Greenb33cc762019-11-28 14:29:44 +0000182#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
183 size_t out_buf_len = ssl->out_buf_len;
184#else
185 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
186#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100187
Darryl Greenb33cc762019-11-28 14:29:44 +0000188 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100189 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100190
Darryl Greenb33cc762019-11-28 14:29:44 +0000191 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100192}
193
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
195{
Hanno Becker11682cc2018-08-22 14:41:02 +0100196 size_t const bytes_written = ssl->out_left;
197 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100198
199 /* Double-check that the write-index hasn't gone
200 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100201 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100202 {
203 /* Should never happen... */
204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
205 }
206
207 return( (int) ( mtu - bytes_written ) );
208}
209
210static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
211{
Janos Follath865b3eb2019-12-16 11:46:15 +0000212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100213 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400214 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100215
216#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400217 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218
219 if( max_len > mfl )
220 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100221
222 /* By the standard (RFC 6066 Sect. 4), the MFL extension
223 * only limits the maximum record payload size, so in theory
224 * we would be allowed to pack multiple records of payload size
225 * MFL into a single datagram. However, this would mean that there's
226 * no way to explicitly communicate MTU restrictions to the peer.
227 *
228 * The following reduction of max_len makes sure that we never
229 * write datagrams larger than MFL + Record Expansion Overhead.
230 */
231 if( max_len <= ssl->out_left )
232 return( 0 );
233
234 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235#endif
236
237 ret = ssl_get_remaining_space_in_datagram( ssl );
238 if( ret < 0 )
239 return( ret );
240 remaining = (size_t) ret;
241
242 ret = mbedtls_ssl_get_record_expansion( ssl );
243 if( ret < 0 )
244 return( ret );
245 expansion = (size_t) ret;
246
247 if( remaining <= expansion )
248 return( 0 );
249
250 remaining -= expansion;
251 if( remaining >= max_len )
252 remaining = max_len;
253
254 return( (int) remaining );
255}
256
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200257/*
258 * Double the retransmit timeout value, within the allowed range,
259 * returning -1 if the maximum value has already been reached.
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262{
263 uint32_t new_timeout;
264
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200265 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200266 return( -1 );
267
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200268 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
269 * in the following way: after the initial transmission and a first
270 * retransmission, back off to a temporary estimated MTU of 508 bytes.
271 * This value is guaranteed to be deliverable (if not guaranteed to be
272 * delivered) of any compliant IPv4 (and IPv6) network, and should work
273 * on most non-IP stacks too. */
274 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400275 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200276 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
278 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200279
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200280 new_timeout = 2 * ssl->handshake->retransmit_timeout;
281
282 /* Avoid arithmetic overflow and range overflow */
283 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200284 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200285 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200286 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200287 }
288
289 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291 ssl->handshake->retransmit_timeout ) );
292
293 return( 0 );
294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200298 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300 ssl->handshake->retransmit_timeout ) );
301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
305int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200306 const unsigned char *key_enc, const unsigned char *key_dec,
307 size_t keylen,
308 const unsigned char *iv_enc, const unsigned char *iv_dec,
309 size_t ivlen,
310 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200311 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
313int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
314int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
315int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
316int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
317#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000318
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200319/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +0100320 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +0000321#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200322 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
323 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
324 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
325/* This function makes sure every byte in the memory region is accessed
326 * (in ascending addresses order) */
327static void ssl_read_memory( unsigned char *p, size_t len )
328{
329 unsigned char acc = 0;
330 volatile unsigned char force;
331
332 for( ; len != 0; p++, len-- )
333 acc ^= *p;
334
335 force = acc;
336 (void) force;
337}
338#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
339
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100340/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200342 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000343
Hanno Beckerccc13d02020-05-04 12:30:04 +0100344#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
345 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100346
347static size_t ssl_compute_padding_length( size_t len,
348 size_t granularity )
349{
350 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
351}
352
Hanno Becker581bc1b2020-05-04 12:20:03 +0100353/* This functions transforms a (D)TLS plaintext fragment and a record content
354 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
355 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
356 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100357 *
358 * struct {
359 * opaque content[DTLSPlaintext.length];
360 * ContentType real_type;
361 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100362 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100363 *
364 * Input:
365 * - `content`: The beginning of the buffer holding the
366 * plaintext to be wrapped.
367 * - `*content_size`: The length of the plaintext in Bytes.
368 * - `max_len`: The number of Bytes available starting from
369 * `content`. This must be `>= *content_size`.
370 * - `rec_type`: The desired record content type.
371 *
372 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100373 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
374 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100375 *
376 * Returns:
377 * - `0` on success.
378 * - A negative error code if `max_len` didn't offer enough space
379 * for the expansion.
380 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100381static int ssl_build_inner_plaintext( unsigned char *content,
382 size_t *content_size,
383 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100384 uint8_t rec_type,
385 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100386{
387 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100388
389 /* Write real content type */
390 if( remaining == 0 )
391 return( -1 );
392 content[ len ] = rec_type;
393 len++;
394 remaining--;
395
396 if( remaining < pad )
397 return( -1 );
398 memset( content + len, 0, pad );
399 len += pad;
400 remaining -= pad;
401
402 *content_size = len;
403 return( 0 );
404}
405
Hanno Becker581bc1b2020-05-04 12:20:03 +0100406/* This function parses a (D)TLSInnerPlaintext structure.
407 * See ssl_build_inner_plaintext() for details. */
408static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100409 size_t *content_size,
410 uint8_t *rec_type )
411{
412 size_t remaining = *content_size;
413
414 /* Determine length of padding by skipping zeroes from the back. */
415 do
416 {
417 if( remaining == 0 )
418 return( -1 );
419 remaining--;
420 } while( content[ remaining ] == 0 );
421
422 *content_size = remaining;
423 *rec_type = content[ remaining ];
424
425 return( 0 );
426}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100427#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
428 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100429
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100430/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100431 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000432static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100433 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100434 mbedtls_record *rec,
435 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000436{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100437 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100438 *
439 * additional_data = seq_num + TLSCompressed.type +
440 * TLSCompressed.version + TLSCompressed.length;
441 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100442 * For the CID extension, this is extended as follows
443 * (quoting draft-ietf-tls-dtls-connection-id-05,
444 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100445 *
446 * additional_data = seq_num + DTLSPlaintext.type +
447 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100448 * cid +
449 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100450 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100451 *
452 * For TLS 1.3, the record sequence number is dropped from the AAD
453 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100454 */
455
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100456 unsigned char *cur = add_data;
457
458#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
459 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
460#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
461 {
462 ((void) minor_ver);
463 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
464 cur += sizeof( rec->ctr );
465 }
466
467 *cur = rec->type;
468 cur++;
469
470 memcpy( cur, rec->ver, sizeof( rec->ver ) );
471 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100472
Hanno Beckera0e20d02019-05-15 14:03:01 +0100473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100474 if( rec->cid_len != 0 )
475 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100476 memcpy( cur, rec->cid, rec->cid_len );
477 cur += rec->cid_len;
478
479 *cur = rec->cid_len;
480 cur++;
481
482 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
483 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
484 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100485 }
486 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100487#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100488 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100489 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
490 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
491 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100492 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100493
494 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000495}
496
Hanno Becker9d062f92020-02-07 10:26:36 +0000497#if defined(MBEDTLS_SSL_PROTO_SSL3)
498
499#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
500
501/*
502 * SSLv3.0 MAC functions
503 */
504static void ssl_mac( mbedtls_md_context_t *md_ctx,
505 const unsigned char *secret,
506 const unsigned char *buf, size_t len,
507 const unsigned char *ctr, int type,
508 unsigned char out[SSL3_MAC_MAX_BYTES] )
509{
510 unsigned char header[11];
511 unsigned char padding[48];
512 int padlen;
513 int md_size = mbedtls_md_get_size( md_ctx->md_info );
514 int md_type = mbedtls_md_get_type( md_ctx->md_info );
515
516 /* Only MD5 and SHA-1 supported */
517 if( md_type == MBEDTLS_MD_MD5 )
518 padlen = 48;
519 else
520 padlen = 40;
521
522 memcpy( header, ctr, 8 );
523 header[ 8] = (unsigned char) type;
524 header[ 9] = (unsigned char)( len >> 8 );
525 header[10] = (unsigned char)( len );
526
527 memset( padding, 0x36, padlen );
528 mbedtls_md_starts( md_ctx );
529 mbedtls_md_update( md_ctx, secret, md_size );
530 mbedtls_md_update( md_ctx, padding, padlen );
531 mbedtls_md_update( md_ctx, header, 11 );
532 mbedtls_md_update( md_ctx, buf, len );
533 mbedtls_md_finish( md_ctx, out );
534
535 memset( padding, 0x5C, padlen );
536 mbedtls_md_starts( md_ctx );
537 mbedtls_md_update( md_ctx, secret, md_size );
538 mbedtls_md_update( md_ctx, padding, padlen );
539 mbedtls_md_update( md_ctx, out, md_size );
540 mbedtls_md_finish( md_ctx, out );
541}
542#endif /* MBEDTLS_SSL_PROTO_SSL3 */
543
Hanno Becker17263802020-05-28 07:05:48 +0100544static int ssl_transform_aead_dynamic_iv_is_explicit(
545 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100546{
Hanno Becker17263802020-05-28 07:05:48 +0100547 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100548}
549
Hanno Beckerdf8be222020-05-21 15:30:57 +0100550
Hanno Becker17263802020-05-28 07:05:48 +0100551/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
552 *
553 * Concretely, this occurs in two variants:
554 *
555 * a) Fixed and dynamic IV lengths add up to total IV length, giving
556 * IV = fixed_iv || dynamic_iv
557 *
558 * b) Fixed IV lengths matches total IV length, giving
559 * IV = fixed_iv XOR ( 0 || dynamic_iv )
560 */
561static void ssl_build_record_nonce( unsigned char *dst_iv,
562 size_t dst_iv_len,
563 unsigned char const *fixed_iv,
564 size_t fixed_iv_len,
565 unsigned char const *dynamic_iv,
566 size_t dynamic_iv_len )
567{
568 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100569
570 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100571 memset( dst_iv, 0, dst_iv_len );
572 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100573
Hanno Becker17263802020-05-28 07:05:48 +0100574 dst_iv += dst_iv_len - dynamic_iv_len;
575 for( i = 0; i < dynamic_iv_len; i++ )
576 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100577}
578
Hanno Beckera18d1322018-01-03 14:27:32 +0000579int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
580 mbedtls_ssl_transform *transform,
581 mbedtls_record *rec,
582 int (*f_rng)(void *, unsigned char *, size_t),
583 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100586 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000587 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100588 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100589 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000590 size_t post_avail;
591
592 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000593#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200594 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000595 ((void) ssl);
596#endif
597
598 /* The PRNG is used for dynamic IV generation that's used
599 * for CBC transformations in TLS 1.1 and TLS 1.2. */
600#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
601 ( defined(MBEDTLS_AES_C) || \
602 defined(MBEDTLS_ARIA_C) || \
603 defined(MBEDTLS_CAMELLIA_C) ) && \
604 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
605 ((void) f_rng);
606 ((void) p_rng);
607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000611 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100612 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
615 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100616 if( rec == NULL
617 || rec->buf == NULL
618 || rec->buf_len < rec->data_offset
619 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100620#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100621 || rec->cid_len != 0
622#endif
623 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000624 {
625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100627 }
628
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000629 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100630 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000632 data, rec->data_len );
633
634 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
635
636 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
637 {
638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
639 (unsigned) rec->data_len,
640 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
642 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100643
Hanno Becker92313402020-05-20 13:58:58 +0100644 /* The following two code paths implement the (D)TLSInnerPlaintext
645 * structure present in TLS 1.3 and DTLS 1.2 + CID.
646 *
647 * See ssl_build_inner_plaintext() for more information.
648 *
649 * Note that this changes `rec->data_len`, and hence
650 * `post_avail` needs to be recalculated afterwards.
651 *
652 * Note also that the two code paths cannot occur simultaneously
653 * since they apply to different versions of the protocol. There
654 * is hence no risk of double-addition of the inner plaintext.
655 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100656#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
657 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
658 {
Hanno Becker13996922020-05-28 16:15:19 +0100659 size_t padding =
660 ssl_compute_padding_length( rec->data_len,
661 MBEDTLS_SSL_TLS13_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100662 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100663 &rec->data_len,
664 post_avail,
665 rec->type,
666 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100667 {
668 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
669 }
670
671 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
672 }
673#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
674
Hanno Beckera0e20d02019-05-15 14:03:01 +0100675#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100676 /*
677 * Add CID information
678 */
679 rec->cid_len = transform->out_cid_len;
680 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
681 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100682
683 if( rec->cid_len != 0 )
684 {
Hanno Becker13996922020-05-28 16:15:19 +0100685 size_t padding =
686 ssl_compute_padding_length( rec->data_len,
687 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100688 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100689 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100690 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100691 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100692 * Note that this changes `rec->data_len`, and hence
693 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100694 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100695 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100696 &rec->data_len,
697 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100698 rec->type,
699 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100700 {
701 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
702 }
703
704 rec->type = MBEDTLS_SSL_MSG_CID;
705 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100706#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100707
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100708 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
709
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100711 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 */
Hanno Becker52344c22018-01-03 15:24:20 +0000713#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( mode == MBEDTLS_MODE_STREAM ||
715 ( mode == MBEDTLS_MODE_CBC
716#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000717 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100718#endif
719 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000721 if( post_avail < transform->maclen )
722 {
723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
724 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
725 }
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000728 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200729 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000730 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000731 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
732 data, rec->data_len, rec->ctr, rec->type, mac );
733 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200734 }
735 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
738 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000739 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200740 {
Hanno Becker992b6872017-11-09 18:57:39 +0000741 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
742
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100743 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
744 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000745
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000746 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100747 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000748 mbedtls_md_hmac_update( &transform->md_ctx_enc,
749 data, rec->data_len );
750 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
751 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
752
753 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200754 }
755 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200756#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200760 }
761
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000762 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
763 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200764
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000765 rec->data_len += transform->maclen;
766 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100767 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200768 }
Hanno Becker52344c22018-01-03 15:24:20 +0000769#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200771 /*
772 * Encrypt
773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
775 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000778 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000780 "including %d bytes of padding",
781 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000783 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
784 transform->iv_enc, transform->ivlen,
785 data, rec->data_len,
786 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200789 return( ret );
790 }
791
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000792 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200796 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 }
Paul Bakker68884e32013-01-07 18:20:04 +0100798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000800
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200801#if defined(MBEDTLS_GCM_C) || \
802 defined(MBEDTLS_CCM_C) || \
803 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200805 mode == MBEDTLS_MODE_CCM ||
806 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200809 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100810 unsigned char *dynamic_iv;
811 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100812 int dynamic_iv_is_explicit =
813 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000814
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100815 /* Check that there's space for the authentication tag. */
816 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000817 {
818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
819 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
820 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000821
Paul Bakker68884e32013-01-07 18:20:04 +0100822 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100823 * Build nonce for AEAD encryption.
824 *
825 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
826 * part of the IV is prepended to the ciphertext and
827 * can be chosen freely - in particular, it need not
828 * agree with the record sequence number.
829 * However, since ChaChaPoly as well as all AEAD modes
830 * in TLS 1.3 use the record sequence number as the
831 * dynamic part of the nonce, we uniformly use the
832 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100833 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100834 dynamic_iv = rec->ctr;
835 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200836
Hanno Becker17263802020-05-28 07:05:48 +0100837 ssl_build_record_nonce( iv, sizeof( iv ),
838 transform->iv_enc,
839 transform->fixed_ivlen,
840 dynamic_iv,
841 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100842
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100843 /*
844 * Build additional data for AEAD encryption.
845 * This depends on the TLS version.
846 */
847 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
848 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100849
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
851 iv, transform->ivlen );
852 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Beckerdf8be222020-05-21 15:30:57 +0100853 data - dynamic_iv_len * dynamic_iv_is_explicit,
854 dynamic_iv_len * dynamic_iv_is_explicit );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000855 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100856 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200858 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000859 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000860
Paul Bakker68884e32013-01-07 18:20:04 +0100861 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200862 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200863 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000864
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200865 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000866 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100867 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000868 data, rec->data_len, /* source */
869 data, &rec->data_len, /* destination */
870 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200873 return( ret );
874 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000875 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
876 data + rec->data_len, transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100877 /* Account for authentication tag. */
878 rec->data_len += transform->taglen;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000879 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100880
881 /*
882 * Prefix record content with dynamic IV in case it is explicit.
883 */
Hanno Becker17263802020-05-28 07:05:48 +0100884 if( dynamic_iv_is_explicit )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100885 {
886 if( rec->data_offset < dynamic_iv_len )
887 {
888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
889 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
890 }
891
892 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
893 rec->data_offset -= dynamic_iv_len;
894 rec->data_len += dynamic_iv_len;
895 }
896
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100897 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000898 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
901#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000902 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000906 size_t padlen, i;
907 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000908
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000909 /* Currently we're always using minimal padding
910 * (up to 255 bytes would be allowed). */
911 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
912 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 padlen = 0;
914
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000915 /* Check there's enough space in the buffer for the padding. */
916 if( post_avail < padlen + 1 )
917 {
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
919 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
920 }
921
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000923 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000925 rec->data_len += padlen + 1;
926 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000929 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000930 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
931 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000932 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000933 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000934 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000935 if( f_rng == NULL )
936 {
937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
939 }
940
941 if( rec->data_offset < transform->ivlen )
942 {
943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
944 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
945 }
946
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000947 /*
948 * Generate IV
949 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000950 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000951 if( ret != 0 )
952 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000953
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000954 memcpy( data - transform->ivlen, transform->iv_enc,
955 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000956
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000961 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000962 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200963 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000965 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
966 transform->iv_enc,
967 transform->ivlen,
968 data, rec->data_len,
969 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200972 return( ret );
973 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200974
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000975 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200979 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000982 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200983 {
984 /*
985 * Save IV in SSL3 and TLS1
986 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000987 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
988 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000990 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200991#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000992 {
993 data -= transform->ivlen;
994 rec->data_offset -= transform->ivlen;
995 rec->data_len += transform->ivlen;
996 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100999 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001000 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001001 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1002
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001003 /*
1004 * MAC(MAC_write_key, seq_num +
1005 * TLSCipherText.type +
1006 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001007 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001008 * IV + // except for TLS 1.0
1009 * ENC(content + padding + padding_length));
1010 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001011
1012 if( post_avail < transform->maclen)
1013 {
1014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1015 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1016 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001017
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001018 ssl_extract_add_data_from_record( add_data, &add_data_len,
1019 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001022 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001023 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001024
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001025 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001026 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001027 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1028 data, rec->data_len );
1029 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1030 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001031
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001032 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001033
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001034 rec->data_len += transform->maclen;
1035 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001036 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001042 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001048 /* Make extra sure authentication was performed, exactly once */
1049 if( auth_done != 1 )
1050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001053 }
1054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
1057 return( 0 );
1058}
1059
Hanno Becker605949f2019-07-12 08:23:59 +01001060int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001061 mbedtls_ssl_transform *transform,
1062 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001064 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001066 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001067#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001068 size_t padlen = 0, correct = 1;
1069#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001070 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001071 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001072 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001073
Hanno Beckera18d1322018-01-03 14:27:32 +00001074#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001075 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001076 ((void) ssl);
1077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001080 if( rec == NULL ||
1081 rec->buf == NULL ||
1082 rec->buf_len < rec->data_offset ||
1083 rec->buf_len - rec->data_offset < rec->data_len )
1084 {
1085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001087 }
1088
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001089 data = rec->buf + rec->data_offset;
1090 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Hanno Beckera0e20d02019-05-15 14:03:01 +01001092#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001093 /*
1094 * Match record's CID with incoming CID.
1095 */
Hanno Becker938489a2019-05-08 13:02:22 +01001096 if( rec->cid_len != transform->in_cid_len ||
1097 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1098 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001099 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001100 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001101#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1104 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001105 {
1106 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001107 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1108 transform->iv_dec,
1109 transform->ivlen,
1110 data, rec->data_len,
1111 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001114 return( ret );
1115 }
1116
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001117 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 }
Paul Bakker68884e32013-01-07 18:20:04 +01001123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001125#if defined(MBEDTLS_GCM_C) || \
1126 defined(MBEDTLS_CCM_C) || \
1127 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001129 mode == MBEDTLS_MODE_CCM ||
1130 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001132 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001133 unsigned char *dynamic_iv;
1134 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001136 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001137 * Extract dynamic part of nonce for AEAD decryption.
1138 *
1139 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1140 * part of the IV is prepended to the ciphertext and
1141 * can be chosen freely - in particular, it need not
1142 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001143 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001144 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001145 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001146 {
1147 if( rec->data_len < dynamic_iv_len )
1148 {
1149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) ",
1150 rec->data_len,
1151 dynamic_iv_len ) );
1152 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1153 }
1154 dynamic_iv = data;
1155
1156 data += dynamic_iv_len;
1157 rec->data_offset += dynamic_iv_len;
1158 rec->data_len -= dynamic_iv_len;
1159 }
Hanno Becker17263802020-05-28 07:05:48 +01001160 else
1161 {
1162 dynamic_iv = rec->ctr;
1163 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001164
1165 /* Check that there's space for the authentication tag. */
1166 if( rec->data_len < transform->taglen )
1167 {
1168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < taglen (%d) " ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001170 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001171 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001172
Hanno Beckerdf8be222020-05-21 15:30:57 +01001173 /*
1174 * Prepare nonce from dynamic and static parts.
1175 */
Hanno Becker17263802020-05-28 07:05:48 +01001176 ssl_build_record_nonce( iv, sizeof( iv ),
1177 transform->iv_dec,
1178 transform->fixed_ivlen,
1179 dynamic_iv,
1180 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001181
Hanno Beckerdf8be222020-05-21 15:30:57 +01001182 /*
1183 * Build additional data for AEAD encryption.
1184 * This depends on the TLS version.
1185 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001186 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1187 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001188 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001189 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001190
Hanno Beckerd96a6522019-07-10 13:55:25 +01001191 /* Because of the check above, we know that there are
1192 * explicit_iv_len Bytes preceeding data, and taglen
1193 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001194 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001195 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001196
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001197 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001198 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001199 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001200
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001201 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001202 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001203 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001204 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1205 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001206 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001207 data, rec->data_len,
1208 data, &olen,
1209 data + rec->data_len,
1210 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1215 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001216
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001217 return( ret );
1218 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001219 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Hanno Beckerd96a6522019-07-10 13:55:25 +01001221 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001222 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001226 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1230#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001231 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001234 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 /*
Paul Bakker45829992013-01-03 14:52:21 +01001237 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001240 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1241 {
1242 /* The ciphertext is prefixed with the CBC IV. */
1243 minlen += transform->ivlen;
1244 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001245#endif
Paul Bakker45829992013-01-03 14:52:21 +01001246
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001247 /* Size considerations:
1248 *
1249 * - The CBC cipher text must not be empty and hence
1250 * at least of size transform->ivlen.
1251 *
1252 * Together with the potential IV-prefix, this explains
1253 * the first of the two checks below.
1254 *
1255 * - The record must contain a MAC, either in plain or
1256 * encrypted, depending on whether Encrypt-then-MAC
1257 * is used or not.
1258 * - If it is, the message contains the IV-prefix,
1259 * the CBC ciphertext, and the MAC.
1260 * - If it is not, the padded plaintext, and hence
1261 * the CBC ciphertext, has at least length maclen + 1
1262 * because there is at least the padding length byte.
1263 *
1264 * As the CBC ciphertext is not empty, both cases give the
1265 * lower bound minlen + maclen + 1 on the record size, which
1266 * we test for in the second check below.
1267 */
1268 if( rec->data_len < minlen + transform->ivlen ||
1269 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001272 "+ 1 ) ( + expl IV )", rec->data_len,
1273 transform->ivlen,
1274 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001276 }
1277
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001278 /*
1279 * Authenticate before decrypt if enabled
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001282 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001283 {
Hanno Becker992b6872017-11-09 18:57:39 +00001284 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001287
Hanno Beckerd96a6522019-07-10 13:55:25 +01001288 /* Update data_len in tandem with add_data.
1289 *
1290 * The subtraction is safe because of the previous check
1291 * data_len >= minlen + maclen + 1.
1292 *
1293 * Afterwards, we know that data + data_len is followed by at
1294 * least maclen Bytes, which justifies the call to
1295 * mbedtls_ssl_safer_memcmp() below.
1296 *
1297 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001298 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001299 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1300 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001301
Hanno Beckerd96a6522019-07-10 13:55:25 +01001302 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001303 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1304 add_data_len );
1305 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1306 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001307 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1308 data, rec->data_len );
1309 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1310 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001311
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001312 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1313 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001314 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001315 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001316
Hanno Beckerd96a6522019-07-10 13:55:25 +01001317 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001318 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1319 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001323 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001324 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001327
1328 /*
1329 * Check length sanity
1330 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001331
1332 /* We know from above that data_len > minlen >= 0,
1333 * so the following check in particular implies that
1334 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001335 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001338 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001340 }
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001343 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001344 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001345 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001346 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001347 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001348 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001349 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001350
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001351 data += transform->ivlen;
1352 rec->data_offset += transform->ivlen;
1353 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001356
Hanno Beckerd96a6522019-07-10 13:55:25 +01001357 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1358
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001359 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1360 transform->iv_dec, transform->ivlen,
1361 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001364 return( ret );
1365 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001366
Hanno Beckerd96a6522019-07-10 13:55:25 +01001367 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001368 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001372 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001375 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001376 {
1377 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001378 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1379 * records is equivalent to CBC decryption of the concatenation
1380 * of the records; in other words, IVs are maintained across
1381 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001382 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001383 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1384 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001386#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001388 /* Safe since data_len >= minlen + maclen + 1, so after having
1389 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001390 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1391 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001392 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001393
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001394 if( auth_done == 1 )
1395 {
1396 correct *= ( rec->data_len >= padlen + 1 );
1397 padlen *= ( rec->data_len >= padlen + 1 );
1398 }
1399 else
Paul Bakker45829992013-01-03 14:52:21 +01001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001402 if( rec->data_len < transform->maclen + padlen + 1 )
1403 {
1404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1405 rec->data_len,
1406 transform->maclen,
1407 padlen + 1 ) );
1408 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001409#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001410
1411 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
1412 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01001413 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001415 padlen++;
1416
1417 /* Regardless of the validity of the padding,
1418 * we have data_len >= padlen here. */
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001421 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001423 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_DEBUG_ALL)
1426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001427 "should be no more than %d",
1428 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001429#endif
Paul Bakker45829992013-01-03 14:52:21 +01001430 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 }
1432 }
1433 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1435#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1436 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001437 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001439 /* The padding check involves a series of up to 256
1440 * consecutive memory reads at the end of the record
1441 * plaintext buffer. In order to hide the length and
1442 * validity of the padding, always perform exactly
1443 * `min(256,plaintext_len)` reads (but take into account
1444 * only the last `padlen` bytes for the padding check). */
1445 size_t pad_count = 0;
1446 size_t real_count = 0;
1447 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001448
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001449 /* Index of first padding byte; it has been ensured above
1450 * that the subtraction is safe. */
1451 size_t const padding_idx = rec->data_len - padlen;
1452 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1453 size_t const start_idx = rec->data_len - num_checks;
1454 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001455
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001456 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001457 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001458 real_count |= ( idx >= padding_idx );
1459 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001460 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001461 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001464 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001466#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001467 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1471 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001475 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001476
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001477 /* If the padding was found to be invalid, padlen == 0
1478 * and the subtraction is safe. If the padding was found valid,
1479 * padlen hasn't been changed and the previous assertion
1480 * data_len >= padlen still holds. */
1481 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001485 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001490
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001491#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001493 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001495
1496 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001497 * Authenticate if not done yet.
1498 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 */
Hanno Becker52344c22018-01-03 15:24:20 +00001500#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001501 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001502 {
Hanno Becker992b6872017-11-09 18:57:39 +00001503 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001504
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001505 /* If the initial value of padlen was such that
1506 * data_len < maclen + padlen + 1, then padlen
1507 * got reset to 1, and the initial check
1508 * data_len >= minlen + maclen + 1
1509 * guarantees that at this point we still
1510 * have at least data_len >= maclen.
1511 *
1512 * If the initial value of padlen was such that
1513 * data_len >= maclen + padlen + 1, then we have
1514 * subtracted either padlen + 1 (if the padding was correct)
1515 * or 0 (if the padding was incorrect) since then,
1516 * hence data_len >= maclen in any case.
1517 */
1518 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001519 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1520 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001523 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001524 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001525 ssl_mac( &transform->md_ctx_dec,
1526 transform->mac_dec,
1527 data, rec->data_len,
1528 rec->ctr, rec->type,
1529 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001530 }
1531 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1533#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1534 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001535 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001536 {
1537 /*
1538 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02001539 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001540 *
1541 * Known timing attacks:
1542 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1543 *
Gilles Peskine20b44082018-05-29 14:06:49 +02001544 * To compensate for different timings for the MAC calculation
1545 * depending on how much padding was removed (which is determined
1546 * by padlen), process extra_run more blocks through the hash
1547 * function.
1548 *
1549 * The formula in the paper is
1550 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1551 * where L1 is the size of the header plus the decrypted message
1552 * plus CBC padding and L2 is the size of the header plus the
1553 * decrypted message. This is for an underlying hash function
1554 * with 64-byte blocks.
1555 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1556 * correctly. We round down instead of up, so -56 is the correct
1557 * value for our calculations instead of -55.
1558 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02001559 * Repeat the formula rather than defining a block_size variable.
1560 * This avoids requiring division by a variable at runtime
1561 * (which would be marginally less efficient and would require
1562 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001563 */
1564 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001565 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001566
1567 /*
1568 * The next two sizes are the minimum and maximum values of
1569 * in_msglen over all padlen values.
1570 *
1571 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001572 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001573 *
1574 * Note that max_len + maclen is never more than the buffer
1575 * length, as we previously did in_msglen -= maclen too.
1576 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001577 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001578 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1579
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001580 memset( tmp, 0, sizeof( tmp ) );
1581
1582 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001583 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001584#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1585 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001586 case MBEDTLS_MD_MD5:
1587 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001588 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001589 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001590 extra_run =
1591 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1592 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001593 break;
1594#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001595#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001596 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001597 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001598 extra_run =
1599 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1600 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001601 break;
1602#endif
1603 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1606 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001607
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001608 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001609
Hanno Beckercab87e62019-04-29 13:52:53 +01001610 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1611 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001612 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1613 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001614 /* Make sure we access everything even when padlen > 0. This
1615 * makes the synchronisation requirements for just-in-time
1616 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001617 ssl_read_memory( data + rec->data_len, padlen );
1618 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001619
1620 /* Call mbedtls_md_process at least once due to cache attacks
1621 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001622 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001623 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001624
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001625 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001626
1627 /* Make sure we access all the memory that could contain the MAC,
1628 * before we check it in the next code block. This makes the
1629 * synchronisation requirements for just-in-time Prime+Probe
1630 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001631 ssl_read_memory( data + min_len,
1632 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 }
1634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1636 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001642#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001643 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1644 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001645#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001647 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1648 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_SSL_DEBUG_ALL)
1651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001652#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001653 correct = 0;
1654 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001655 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001657
1658 /*
1659 * Finally check the correct flag
1660 */
1661 if( correct == 0 )
1662 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001663#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001664
1665 /* Make extra sure authentication was performed, exactly once */
1666 if( auth_done != 1 )
1667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Hanno Beckerccc13d02020-05-04 12:30:04 +01001672#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1673 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1674 {
1675 /* Remove inner padding and infer true content type. */
1676 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1677 &rec->type );
1678
1679 if( ret != 0 )
1680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1681 }
1682#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1683
Hanno Beckera0e20d02019-05-15 14:03:01 +01001684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001685 if( rec->cid_len != 0 )
1686 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001687 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1688 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001689 if( ret != 0 )
1690 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1691 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001692#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
1696 return( 0 );
1697}
1698
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001699#undef MAC_NONE
1700#undef MAC_PLAINTEXT
1701#undef MAC_CIPHERTEXT
1702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001704/*
1705 * Compression/decompression functions
1706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001708{
Janos Follath865b3eb2019-12-16 11:46:15 +00001709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001710 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001711 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001712 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001713 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001714#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1715 size_t out_buf_len = ssl->out_buf_len;
1716#else
1717 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1718#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001721
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001722 if( len_pre == 0 )
1723 return( 0 );
1724
Paul Bakker2770fbd2012-07-03 13:30:23 +00001725 memcpy( msg_pre, ssl->out_msg, len_pre );
1726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001728 ssl->out_msglen ) );
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731 ssl->out_msg, ssl->out_msglen );
1732
Paul Bakker48916f92012-09-16 19:57:18 +00001733 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1734 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1735 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001736 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001737
Paul Bakker48916f92012-09-16 19:57:18 +00001738 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001739 if( ret != Z_OK )
1740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1742 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743 }
1744
Darryl Greenb33cc762019-11-28 14:29:44 +00001745 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001746 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749 ssl->out_msglen ) );
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752 ssl->out_msg, ssl->out_msglen );
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755
1756 return( 0 );
1757}
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760{
Janos Follath865b3eb2019-12-16 11:46:15 +00001761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001763 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001765 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001766#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1767 size_t in_buf_len = ssl->in_buf_len;
1768#else
1769 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1770#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001774 if( len_pre == 0 )
1775 return( 0 );
1776
Paul Bakker2770fbd2012-07-03 13:30:23 +00001777 memcpy( msg_pre, ssl->in_msg, len_pre );
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001780 ssl->in_msglen ) );
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783 ssl->in_msg, ssl->in_msglen );
1784
Paul Bakker48916f92012-09-16 19:57:18 +00001785 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1786 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1787 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001788 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789
Paul Bakker48916f92012-09-16 19:57:18 +00001790 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001791 if( ret != Z_OK )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1794 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 }
1796
Darryl Greenb33cc762019-11-28 14:29:44 +00001797 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001798 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801 ssl->in_msglen ) );
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804 ssl->in_msg, ssl->in_msglen );
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
1808 return( 0 );
1809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Paul Bakker5121ce52009-01-03 21:22:43 +00001812/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001813 * Fill the input message buffer by appending data to it.
1814 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001815 *
1816 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1817 * available (from this read and/or a previous one). Otherwise, an error code
1818 * is returned (possibly EOF or WANT_READ).
1819 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001820 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1821 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1822 * since we always read a whole datagram at once.
1823 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001824 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001825 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828{
Janos Follath865b3eb2019-12-16 11:46:15 +00001829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001830 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001831#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1832 size_t in_buf_len = ssl->in_buf_len;
1833#else
1834 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001839 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001842 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001844 }
1845
Darryl Greenb33cc762019-11-28 14:29:44 +00001846 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001850 }
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001855 uint32_t timeout;
1856
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001857 /* Just to be sure */
1858 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1859 {
1860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1861 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1863 }
1864
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001865 /*
1866 * The point is, we need to always read a full datagram at once, so we
1867 * sometimes read more then requested, and handle the additional data.
1868 * It could be the rest of the current record (while fetching the
1869 * header) and/or some other records in the same datagram.
1870 */
1871
1872 /*
1873 * Move to the next record in the already read datagram if applicable
1874 */
1875 if( ssl->next_record_offset != 0 )
1876 {
1877 if( ssl->in_left < ssl->next_record_offset )
1878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001881 }
1882
1883 ssl->in_left -= ssl->next_record_offset;
1884
1885 if( ssl->in_left != 0 )
1886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001888 ssl->next_record_offset ) );
1889 memmove( ssl->in_hdr,
1890 ssl->in_hdr + ssl->next_record_offset,
1891 ssl->in_left );
1892 }
1893
1894 ssl->next_record_offset = 0;
1895 }
1896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001899
1900 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001901 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001902 */
1903 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001906 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001907 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001908
1909 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001910 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001911 * are not at the beginning of a new record, the caller did something
1912 * wrong.
1913 */
1914 if( ssl->in_left != 0 )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001918 }
1919
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001920 /*
1921 * Don't even try to read if time's out already.
1922 * This avoids by-passing the timer when repeatedly receiving messages
1923 * that will end up being dropped.
1924 */
Hanno Becker7876d122020-02-05 10:39:31 +00001925 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001926 {
1927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001928 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001929 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001930 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001931 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001932 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001935 timeout = ssl->handshake->retransmit_timeout;
1936 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001937 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001940
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001941 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001942 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1943 timeout );
1944 else
1945 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001948
1949 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001951 }
1952
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001953 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001956 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001959 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001960 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001963 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001969 return( ret );
1970 }
1971
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001972 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001975 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001977 {
Hanno Becker786300f2020-02-05 10:46:40 +00001978 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001979 {
Hanno Becker786300f2020-02-05 10:46:40 +00001980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1981 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001982 return( ret );
1983 }
1984
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001985 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001986 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001988 }
1989
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 if( ret < 0 )
1991 return( ret );
1992
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001993 ssl->in_left = ret;
1994 }
1995 else
1996#endif
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001999 ssl->in_left, nb_want ) );
2000
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002001 while( ssl->in_left < nb_want )
2002 {
2003 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002004
Hanno Becker7876d122020-02-05 10:39:31 +00002005 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002006 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2007 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002008 {
2009 if( ssl->f_recv_timeout != NULL )
2010 {
2011 ret = ssl->f_recv_timeout( ssl->p_bio,
2012 ssl->in_hdr + ssl->in_left, len,
2013 ssl->conf->read_timeout );
2014 }
2015 else
2016 {
2017 ret = ssl->f_recv( ssl->p_bio,
2018 ssl->in_hdr + ssl->in_left, len );
2019 }
2020 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002023 ssl->in_left, nb_want ) );
2024 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002025
2026 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002028
2029 if( ret < 0 )
2030 return( ret );
2031
mohammad160352aecb92018-03-28 23:41:40 -07002032 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002033 {
Darryl Green11999bb2018-03-13 15:22:58 +00002034 MBEDTLS_SSL_DEBUG_MSG( 1,
2035 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002036 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2038 }
2039
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002040 ssl->in_left += ret;
2041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 }
2043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
2046 return( 0 );
2047}
2048
2049/*
2050 * Flush any data not yet written
2051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053{
Janos Follath865b3eb2019-12-16 11:46:15 +00002054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002055 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002059 if( ssl->f_send == NULL )
2060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002062 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002064 }
2065
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002066 /* Avoid incrementing counter if data is flushed */
2067 if( ssl->out_left == 0 )
2068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002070 return( 0 );
2071 }
2072
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 while( ssl->out_left > 0 )
2074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002076 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Hanno Becker2b1e3542018-08-06 11:19:13 +01002078 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002079 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
2083 if( ret <= 0 )
2084 return( ret );
2085
mohammad160352aecb92018-03-28 23:41:40 -07002086 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002087 {
Darryl Green11999bb2018-03-13 15:22:58 +00002088 MBEDTLS_SSL_DEBUG_MSG( 1,
2089 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002090 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2092 }
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 ssl->out_left -= ret;
2095 }
2096
Hanno Becker2b1e3542018-08-06 11:19:13 +01002097#if defined(MBEDTLS_SSL_PROTO_DTLS)
2098 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002099 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002100 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002101 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002102 else
2103#endif
2104 {
2105 ssl->out_hdr = ssl->out_buf + 8;
2106 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002107 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
2111 return( 0 );
2112}
2113
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002114/*
2115 * Functions to handle the DTLS retransmission state machine
2116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002118/*
2119 * Append current handshake message to current outgoing flight
2120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2125 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2126 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002127
2128 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002129 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002130 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002133 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002134 }
2135
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002136 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002137 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002140 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002141 }
2142
2143 /* Copy current handshake message with headers */
2144 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2145 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002146 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002147 msg->next = NULL;
2148
2149 /* Append to the current flight */
2150 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002151 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002152 else
2153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002155 while( cur->next != NULL )
2156 cur = cur->next;
2157 cur->next = msg;
2158 }
2159
Hanno Becker3b235902018-08-06 09:54:53 +01002160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002161 return( 0 );
2162}
2163
2164/*
2165 * Free the current flight of handshake messages
2166 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002167void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_ssl_flight_item *cur = flight;
2170 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002171
2172 while( cur != NULL )
2173 {
2174 next = cur->next;
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 mbedtls_free( cur->p );
2177 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002178
2179 cur = next;
2180 }
2181}
2182
2183/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002184 * Swap transform_out and out_ctr with the alternative ones
2185 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002186static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002189 unsigned char tmp_out_ctr[8];
2190
2191 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002194 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002195 }
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002198
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002199 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002200 tmp_transform = ssl->transform_out;
2201 ssl->transform_out = ssl->handshake->alt_transform_out;
2202 ssl->handshake->alt_transform_out = tmp_transform;
2203
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002204 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002205 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2206 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002208
2209 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002210 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2213 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002214 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002215 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2216 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2219 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002220 }
2221 }
2222#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002223
2224 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002225}
2226
2227/*
2228 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002229 */
2230int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2231{
2232 int ret = 0;
2233
2234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2235
2236 ret = mbedtls_ssl_flight_transmit( ssl );
2237
2238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2239
2240 return( ret );
2241}
2242
2243/*
2244 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002245 *
2246 * Need to remember the current message in case flush_output returns
2247 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002248 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002249 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002250int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002251{
Janos Follath865b3eb2019-12-16 11:46:15 +00002252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002256 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002258
2259 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002260 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002261 ret = ssl_swap_epochs( ssl );
2262 if( ret != 0 )
2263 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002266 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002267
2268 while( ssl->handshake->cur_msg != NULL )
2269 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002270 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002271 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002272
Hanno Beckere1dcb032018-08-17 16:47:58 +01002273 int const is_finished =
2274 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2275 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2276
Hanno Becker04da1892018-08-14 13:22:10 +01002277 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2278 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2279
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002280 /* Swap epochs before sending Finished: we can't do it after
2281 * sending ChangeCipherSpec, in case write returns WANT_READ.
2282 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002283 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002284 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002286 ret = ssl_swap_epochs( ssl );
2287 if( ret != 0 )
2288 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002289 }
2290
Hanno Becker67bc7c32018-08-06 11:33:50 +01002291 ret = ssl_get_remaining_payload_in_datagram( ssl );
2292 if( ret < 0 )
2293 return( ret );
2294 max_frag_len = (size_t) ret;
2295
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002296 /* CCS is copied as is, while HS messages may need fragmentation */
2297 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2298 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002299 if( max_frag_len == 0 )
2300 {
2301 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2302 return( ret );
2303
2304 continue;
2305 }
2306
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002307 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002308 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002309 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002310
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002311 /* Update position inside current message */
2312 ssl->handshake->cur_msg_p += cur->len;
2313 }
2314 else
2315 {
2316 const unsigned char * const p = ssl->handshake->cur_msg_p;
2317 const size_t hs_len = cur->len - 12;
2318 const size_t frag_off = p - ( cur->p + 12 );
2319 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002320 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002321
Hanno Beckere1dcb032018-08-17 16:47:58 +01002322 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002323 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002324 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002325 {
2326 ret = ssl_swap_epochs( ssl );
2327 if( ret != 0 )
2328 return( ret );
2329 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002330
Hanno Becker67bc7c32018-08-06 11:33:50 +01002331 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2332 return( ret );
2333
2334 continue;
2335 }
2336 max_hs_frag_len = max_frag_len - 12;
2337
2338 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2339 max_hs_frag_len : rem_len;
2340
2341 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002342 {
2343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002344 (unsigned) cur_hs_frag_len,
2345 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002346 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002348 /* Messages are stored with handshake headers as if not fragmented,
2349 * copy beginning of headers then fill fragmentation fields.
2350 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2351 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002352
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002353 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2354 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2355 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2356
Hanno Becker67bc7c32018-08-06 11:33:50 +01002357 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2358 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2359 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002360
2361 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2362
Hanno Becker3f7b9732018-08-28 09:53:25 +01002363 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002364 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2365 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002366 ssl->out_msgtype = cur->type;
2367
2368 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002369 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002370 }
2371
2372 /* If done with the current message move to the next one if any */
2373 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2374 {
2375 if( cur->next != NULL )
2376 {
2377 ssl->handshake->cur_msg = cur->next;
2378 ssl->handshake->cur_msg_p = cur->next->p + 12;
2379 }
2380 else
2381 {
2382 ssl->handshake->cur_msg = NULL;
2383 ssl->handshake->cur_msg_p = NULL;
2384 }
2385 }
2386
2387 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002388 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002391 return( ret );
2392 }
2393 }
2394
Hanno Becker67bc7c32018-08-06 11:33:50 +01002395 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2396 return( ret );
2397
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002398 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2400 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002401 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002404 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002405 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002406
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002408
2409 return( 0 );
2410}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002411
2412/*
2413 * To be called when the last message of an incoming flight is received.
2414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002416{
2417 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002418 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002419 ssl->handshake->flight = NULL;
2420 ssl->handshake->cur_msg = NULL;
2421
2422 /* The next incoming flight will start with this msg_seq */
2423 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2424
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002425 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002426 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002427
Hanno Becker0271f962018-08-16 13:23:47 +01002428 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002429 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002430
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002431 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002432 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2435 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002438 }
2439 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002441}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002442
2443/*
2444 * To be called when the last message of an outgoing flight is send.
2445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002447{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002448 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002449 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2452 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002455 }
2456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002460
Paul Bakker5121ce52009-01-03 21:22:43 +00002461/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002462 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002464
2465/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002466 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002467 *
2468 * - fill in handshake headers
2469 * - update handshake checksum
2470 * - DTLS: save message for resending
2471 * - then pass to the record layer
2472 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002473 * DTLS: except for HelloRequest, messages are only queued, and will only be
2474 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002475 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002476 * Inputs:
2477 * - ssl->out_msglen: 4 + actual handshake message len
2478 * (4 is the size of handshake headers for TLS)
2479 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2480 * - ssl->out_msg + 4: the handshake message body
2481 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002482 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002483 * - ssl->out_msglen: the length of the record contents
2484 * (including handshake headers but excluding record headers)
2485 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002486 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002487int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488{
Janos Follath865b3eb2019-12-16 11:46:15 +00002489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002490 const size_t hs_len = ssl->out_msglen - 4;
2491 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2494
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002495 /*
2496 * Sanity checks
2497 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002498 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002499 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2500 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002501 /* In SSLv3, the client might send a NoCertificate alert. */
2502#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2503 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2504 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2505 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2506#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2507 {
2508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2510 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002513 /* Whenever we send anything different from a
2514 * HelloRequest we should be in a handshake - double check. */
2515 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2516 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002517 ssl->handshake == NULL )
2518 {
2519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002525 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002527 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002530 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002531#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002532
Hanno Beckerb50a2532018-08-06 11:52:54 +01002533 /* Double-check that we did not exceed the bounds
2534 * of the outgoing record buffer.
2535 * This should never fail as the various message
2536 * writing functions must obey the bounds of the
2537 * outgoing record buffer, but better be safe.
2538 *
2539 * Note: We deliberately do not check for the MTU or MFL here.
2540 */
2541 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2542 {
2543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2544 "size %u, maximum %u",
2545 (unsigned) ssl->out_msglen,
2546 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2548 }
2549
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002550 /*
2551 * Fill handshake headers
2552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002555 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2556 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2557 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002559 /*
2560 * DTLS has additional fields in the Handshake layer,
2561 * between the length field and the actual payload:
2562 * uint16 message_seq;
2563 * uint24 fragment_offset;
2564 * uint24 fragment_length;
2565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002568 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002569 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002570 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2573 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002574 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002575 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2577 }
2578
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002579 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002580 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002581
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002582 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002583 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002584 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002585 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2586 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2587 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002588 }
2589 else
2590 {
2591 ssl->out_msg[4] = 0;
2592 ssl->out_msg[5] = 0;
2593 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002594
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002595 /* Handshake hashes are computed without fragmentation,
2596 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002597 memset( ssl->out_msg + 6, 0x00, 3 );
2598 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002601
Hanno Becker0207e532018-08-28 10:28:28 +01002602 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002603 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2604 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002607 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002610 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2611 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002612 {
2613 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002616 return( ret );
2617 }
2618 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002619 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002620#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002621 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002622 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002623 {
2624 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2625 return( ret );
2626 }
2627 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002628
2629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2630
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002631 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002632}
2633
2634/*
2635 * Record layer functions
2636 */
2637
2638/*
2639 * Write current record.
2640 *
2641 * Uses:
2642 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2643 * - ssl->out_msglen: length of the record content (excl headers)
2644 * - ssl->out_msg: record content
2645 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002646int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002647{
2648 int ret, done = 0;
2649 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002650 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002651
2652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002655 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002657 {
2658 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002661 return( ret );
2662 }
2663
2664 len = ssl->out_msglen;
2665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2669 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 ret = mbedtls_ssl_hw_record_write( ssl );
2674 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2677 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002678 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002679
2680 if( ret == 0 )
2681 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002684 if( !done )
2685 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002686 unsigned i;
2687 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002688#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2689 size_t out_buf_len = ssl->out_buf_len;
2690#else
2691 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2692#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002693 /* Skip writing the record content type to after the encryption,
2694 * as it may change when using the CID extension. */
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002697 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002698
Hanno Becker19859472018-08-06 09:40:20 +01002699 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002700 ssl->out_len[0] = (unsigned char)( len >> 8 );
2701 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002702
Paul Bakker48916f92012-09-16 19:57:18 +00002703 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002704 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002705 mbedtls_record rec;
2706
2707 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002708 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002709 rec.data_len = ssl->out_msglen;
2710 rec.data_offset = ssl->out_msg - rec.buf;
2711
2712 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2713 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2714 ssl->conf->transport, rec.ver );
2715 rec.type = ssl->out_msgtype;
2716
Hanno Beckera0e20d02019-05-15 14:03:01 +01002717#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002718 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002719 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002720#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002721
Hanno Beckera18d1322018-01-03 14:27:32 +00002722 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002723 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002726 return( ret );
2727 }
2728
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002729 if( rec.data_offset != 0 )
2730 {
2731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2733 }
2734
Hanno Becker6430faf2019-05-08 11:57:13 +01002735 /* Update the record content type and CID. */
2736 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002738 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002739#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002740 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002741 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2742 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002743 }
2744
Hanno Becker5903de42019-05-03 14:46:38 +01002745 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002746
2747#if defined(MBEDTLS_SSL_PROTO_DTLS)
2748 /* In case of DTLS, double-check that we don't exceed
2749 * the remaining space in the datagram. */
2750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2751 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002752 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002753 if( ret < 0 )
2754 return( ret );
2755
2756 if( protected_record_size > (size_t) ret )
2757 {
2758 /* Should never happen */
2759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2760 }
2761 }
2762#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002763
Hanno Becker6430faf2019-05-08 11:57:13 +01002764 /* Now write the potentially updated record content type. */
2765 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002768 "version = [%d:%d], msglen = %d",
2769 ssl->out_hdr[0], ssl->out_hdr[1],
2770 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002773 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002774
2775 ssl->out_left += protected_record_size;
2776 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002777 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002778
Hanno Beckerdd772292020-02-05 10:38:31 +00002779 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002780 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2781 break;
2782
2783 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002784 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002785 {
2786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2787 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2788 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 }
2790
Hanno Becker67bc7c32018-08-06 11:33:50 +01002791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2793 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002794 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002795 size_t remaining;
2796 ret = ssl_get_remaining_payload_in_datagram( ssl );
2797 if( ret < 0 )
2798 {
2799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2800 ret );
2801 return( ret );
2802 }
2803
2804 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002805 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002806 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002807 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002808 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002809 else
2810 {
Hanno Becker513815a2018-08-20 11:56:09 +01002811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002812 }
2813 }
2814#endif /* MBEDTLS_SSL_PROTO_DTLS */
2815
2816 if( ( flush == SSL_FORCE_FLUSH ) &&
2817 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 return( ret );
2821 }
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
2825 return( 0 );
2826}
2827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002829
2830static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2831{
2832 if( ssl->in_msglen < ssl->in_hslen ||
2833 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2834 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2835 {
2836 return( 1 );
2837 }
2838 return( 0 );
2839}
Hanno Becker44650b72018-08-16 12:51:11 +01002840
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002841static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002842{
2843 return( ( ssl->in_msg[9] << 16 ) |
2844 ( ssl->in_msg[10] << 8 ) |
2845 ssl->in_msg[11] );
2846}
2847
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002848static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002849{
2850 return( ( ssl->in_msg[6] << 16 ) |
2851 ( ssl->in_msg[7] << 8 ) |
2852 ssl->in_msg[8] );
2853}
2854
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002855static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002856{
2857 uint32_t msg_len, frag_off, frag_len;
2858
2859 msg_len = ssl_get_hs_total_len( ssl );
2860 frag_off = ssl_get_hs_frag_off( ssl );
2861 frag_len = ssl_get_hs_frag_len( ssl );
2862
2863 if( frag_off > msg_len )
2864 return( -1 );
2865
2866 if( frag_len > msg_len - frag_off )
2867 return( -1 );
2868
2869 if( frag_len + 12 > ssl->in_msglen )
2870 return( -1 );
2871
2872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002875/*
2876 * Mark bits in bitmask (used for DTLS HS reassembly)
2877 */
2878static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2879{
2880 unsigned int start_bits, end_bits;
2881
2882 start_bits = 8 - ( offset % 8 );
2883 if( start_bits != 8 )
2884 {
2885 size_t first_byte_idx = offset / 8;
2886
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002887 /* Special case */
2888 if( len <= start_bits )
2889 {
2890 for( ; len != 0; len-- )
2891 mask[first_byte_idx] |= 1 << ( start_bits - len );
2892
2893 /* Avoid potential issues with offset or len becoming invalid */
2894 return;
2895 }
2896
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002897 offset += start_bits; /* Now offset % 8 == 0 */
2898 len -= start_bits;
2899
2900 for( ; start_bits != 0; start_bits-- )
2901 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2902 }
2903
2904 end_bits = len % 8;
2905 if( end_bits != 0 )
2906 {
2907 size_t last_byte_idx = ( offset + len ) / 8;
2908
2909 len -= end_bits; /* Now len % 8 == 0 */
2910
2911 for( ; end_bits != 0; end_bits-- )
2912 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2913 }
2914
2915 memset( mask + offset / 8, 0xFF, len / 8 );
2916}
2917
2918/*
2919 * Check that bitmask is full
2920 */
2921static int ssl_bitmask_check( unsigned char *mask, size_t len )
2922{
2923 size_t i;
2924
2925 for( i = 0; i < len / 8; i++ )
2926 if( mask[i] != 0xFF )
2927 return( -1 );
2928
2929 for( i = 0; i < len % 8; i++ )
2930 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2931 return( -1 );
2932
2933 return( 0 );
2934}
2935
Hanno Becker56e205e2018-08-16 09:06:12 +01002936/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002937static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002938 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002939{
Hanno Becker56e205e2018-08-16 09:06:12 +01002940 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002941
Hanno Becker56e205e2018-08-16 09:06:12 +01002942 alloc_len = 12; /* Handshake header */
2943 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002944
Hanno Beckerd07df862018-08-16 09:14:58 +01002945 if( add_bitmap )
2946 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002947
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002948 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002949}
Hanno Becker56e205e2018-08-16 09:06:12 +01002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002952
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002953static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002954{
2955 return( ( ssl->in_msg[1] << 16 ) |
2956 ( ssl->in_msg[2] << 8 ) |
2957 ssl->in_msg[3] );
2958}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002959
Simon Butcher99000142016-10-13 17:21:01 +01002960int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002965 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002967 }
2968
Hanno Becker12555c62018-08-16 12:47:53 +01002969 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002972 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002973 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002977 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002979 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002980
Hanno Becker44650b72018-08-16 12:51:11 +01002981 if( ssl_check_hs_header( ssl ) != 0 )
2982 {
2983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2984 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2985 }
2986
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002987 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002988 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2989 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2990 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2991 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002992 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002993 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2994 {
2995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2996 recv_msg_seq,
2997 ssl->handshake->in_msg_seq ) );
2998 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2999 }
3000
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003001 /* Retransmit only on last message from previous flight, to avoid
3002 * too many retransmissions.
3003 * Besides, No sane server ever retransmits HelloVerifyRequest */
3004 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003008 "message_seq = %d, start_of_flight = %d",
3009 recv_msg_seq,
3010 ssl->handshake->in_flight_start_seq ) );
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003015 return( ret );
3016 }
3017 }
3018 else
3019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003021 "message_seq = %d, expected = %d",
3022 recv_msg_seq,
3023 ssl->handshake->in_msg_seq ) );
3024 }
3025
Hanno Becker90333da2017-10-10 11:27:13 +01003026 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003027 }
3028 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003029
Hanno Becker6d97ef52018-08-16 13:09:04 +01003030 /* Message reassembly is handled alongside buffering of future
3031 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003032 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003033 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003034 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003037 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003038 }
3039 }
3040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003042 /* With TLS we don't handle fragmentation (for now) */
3043 if( ssl->in_msglen < ssl->in_hslen )
3044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3046 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003047 }
3048
Simon Butcher99000142016-10-13 17:21:01 +01003049 return( 0 );
3050}
3051
3052void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3053{
Hanno Becker0271f962018-08-16 13:23:47 +01003054 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003055
Hanno Becker0271f962018-08-16 13:23:47 +01003056 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003057 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003058 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003059 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003060
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003061 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003064 ssl->handshake != NULL )
3065 {
Hanno Becker0271f962018-08-16 13:23:47 +01003066 unsigned offset;
3067 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003068
Hanno Becker0271f962018-08-16 13:23:47 +01003069 /* Increment handshake sequence number */
3070 hs->in_msg_seq++;
3071
3072 /*
3073 * Clear up handshake buffering and reassembly structure.
3074 */
3075
3076 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003077 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003078
3079 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003080 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3081 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003082 offset++, hs_buf++ )
3083 {
3084 *hs_buf = *(hs_buf + 1);
3085 }
3086
3087 /* Create a fresh last entry */
3088 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003089 }
3090#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003091}
3092
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003093/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003094 * DTLS anti-replay: RFC 6347 4.1.2.6
3095 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003096 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3097 * Bit n is set iff record number in_window_top - n has been seen.
3098 *
3099 * Usually, in_window_top is the last record number seen and the lsb of
3100 * in_window is set. The only exception is the initial state (record number 0
3101 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003104void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003105{
3106 ssl->in_window_top = 0;
3107 ssl->in_window = 0;
3108}
3109
3110static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3111{
3112 return( ( (uint64_t) buf[0] << 40 ) |
3113 ( (uint64_t) buf[1] << 32 ) |
3114 ( (uint64_t) buf[2] << 24 ) |
3115 ( (uint64_t) buf[3] << 16 ) |
3116 ( (uint64_t) buf[4] << 8 ) |
3117 ( (uint64_t) buf[5] ) );
3118}
3119
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003120static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3121{
Janos Follath865b3eb2019-12-16 11:46:15 +00003122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003123 unsigned char *original_in_ctr;
3124
3125 // save original in_ctr
3126 original_in_ctr = ssl->in_ctr;
3127
3128 // use counter from record
3129 ssl->in_ctr = record_in_ctr;
3130
3131 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3132
3133 // restore the counter
3134 ssl->in_ctr = original_in_ctr;
3135
3136 return ret;
3137}
3138
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003139/*
3140 * Return 0 if sequence number is acceptable, -1 otherwise
3141 */
Hanno Becker0183d692019-07-12 08:50:37 +01003142int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003143{
3144 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3145 uint64_t bit;
3146
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003147 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003148 return( 0 );
3149
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003150 if( rec_seqnum > ssl->in_window_top )
3151 return( 0 );
3152
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003153 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003154
3155 if( bit >= 64 )
3156 return( -1 );
3157
3158 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3159 return( -1 );
3160
3161 return( 0 );
3162}
3163
3164/*
3165 * Update replay window on new validated record
3166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003168{
3169 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3170
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003171 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003172 return;
3173
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003174 if( rec_seqnum > ssl->in_window_top )
3175 {
3176 /* Update window_top and the contents of the window */
3177 uint64_t shift = rec_seqnum - ssl->in_window_top;
3178
3179 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003180 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003181 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003182 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003183 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003184 ssl->in_window |= 1;
3185 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003186
3187 ssl->in_window_top = rec_seqnum;
3188 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003189 else
3190 {
3191 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003192 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193
3194 if( bit < 64 ) /* Always true, but be extra sure */
3195 ssl->in_window |= (uint64_t) 1 << bit;
3196 }
3197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003199
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003200#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003201/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003202 * Without any SSL context, check if a datagram looks like a ClientHello with
3203 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003204 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003205 *
3206 * - if cookie is valid, return 0
3207 * - if ClientHello looks superficially valid but cookie is not,
3208 * fill obuf and set olen, then
3209 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3210 * - otherwise return a specific error code
3211 */
3212static int ssl_check_dtls_clihlo_cookie(
3213 mbedtls_ssl_cookie_write_t *f_cookie_write,
3214 mbedtls_ssl_cookie_check_t *f_cookie_check,
3215 void *p_cookie,
3216 const unsigned char *cli_id, size_t cli_id_len,
3217 const unsigned char *in, size_t in_len,
3218 unsigned char *obuf, size_t buf_len, size_t *olen )
3219{
3220 size_t sid_len, cookie_len;
3221 unsigned char *p;
3222
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003223 /*
3224 * Structure of ClientHello with record and handshake headers,
3225 * and expected values. We don't need to check a lot, more checks will be
3226 * done when actually parsing the ClientHello - skipping those checks
3227 * avoids code duplication and does not make cookie forging any easier.
3228 *
3229 * 0-0 ContentType type; copied, must be handshake
3230 * 1-2 ProtocolVersion version; copied
3231 * 3-4 uint16 epoch; copied, must be 0
3232 * 5-10 uint48 sequence_number; copied
3233 * 11-12 uint16 length; (ignored)
3234 *
3235 * 13-13 HandshakeType msg_type; (ignored)
3236 * 14-16 uint24 length; (ignored)
3237 * 17-18 uint16 message_seq; copied
3238 * 19-21 uint24 fragment_offset; copied, must be 0
3239 * 22-24 uint24 fragment_length; (ignored)
3240 *
3241 * 25-26 ProtocolVersion client_version; (ignored)
3242 * 27-58 Random random; (ignored)
3243 * 59-xx SessionID session_id; 1 byte len + sid_len content
3244 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3245 * ...
3246 *
3247 * Minimum length is 61 bytes.
3248 */
3249 if( in_len < 61 ||
3250 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3251 in[3] != 0 || in[4] != 0 ||
3252 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3253 {
3254 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3255 }
3256
3257 sid_len = in[59];
3258 if( sid_len > in_len - 61 )
3259 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3260
3261 cookie_len = in[60 + sid_len];
3262 if( cookie_len > in_len - 60 )
3263 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3264
3265 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3266 cli_id, cli_id_len ) == 0 )
3267 {
3268 /* Valid cookie */
3269 return( 0 );
3270 }
3271
3272 /*
3273 * If we get here, we've got an invalid cookie, let's prepare HVR.
3274 *
3275 * 0-0 ContentType type; copied
3276 * 1-2 ProtocolVersion version; copied
3277 * 3-4 uint16 epoch; copied
3278 * 5-10 uint48 sequence_number; copied
3279 * 11-12 uint16 length; olen - 13
3280 *
3281 * 13-13 HandshakeType msg_type; hello_verify_request
3282 * 14-16 uint24 length; olen - 25
3283 * 17-18 uint16 message_seq; copied
3284 * 19-21 uint24 fragment_offset; copied
3285 * 22-24 uint24 fragment_length; olen - 25
3286 *
3287 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3288 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3289 *
3290 * Minimum length is 28.
3291 */
3292 if( buf_len < 28 )
3293 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3294
3295 /* Copy most fields and adapt others */
3296 memcpy( obuf, in, 25 );
3297 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3298 obuf[25] = 0xfe;
3299 obuf[26] = 0xff;
3300
3301 /* Generate and write actual cookie */
3302 p = obuf + 28;
3303 if( f_cookie_write( p_cookie,
3304 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3305 {
3306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3307 }
3308
3309 *olen = p - obuf;
3310
3311 /* Go back and fill length fields */
3312 obuf[27] = (unsigned char)( *olen - 28 );
3313
3314 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3315 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3316 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3317
3318 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3319 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3320
3321 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3322}
3323
3324/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003325 * Handle possible client reconnect with the same UDP quadruplet
3326 * (RFC 6347 Section 4.2.8).
3327 *
3328 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3329 * that looks like a ClientHello.
3330 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003331 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003332 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003333 * - if the input looks like a ClientHello with a valid cookie,
3334 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003335 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003336 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003337 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003338 * This function is called (through ssl_check_client_reconnect()) when an
3339 * unexpected record is found in ssl_get_next_record(), which will discard the
3340 * record if we return 0, and bubble up the return value otherwise (this
3341 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3342 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003343 */
3344static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3345{
Janos Follath865b3eb2019-12-16 11:46:15 +00003346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003347 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003348
Hanno Becker2fddd372019-07-10 14:37:41 +01003349 if( ssl->conf->f_cookie_write == NULL ||
3350 ssl->conf->f_cookie_check == NULL )
3351 {
3352 /* If we can't use cookies to verify reachability of the peer,
3353 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3355 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003356 return( 0 );
3357 }
3358
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003359 ret = ssl_check_dtls_clihlo_cookie(
3360 ssl->conf->f_cookie_write,
3361 ssl->conf->f_cookie_check,
3362 ssl->conf->p_cookie,
3363 ssl->cli_id, ssl->cli_id_len,
3364 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003365 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003366
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003367 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3368
3369 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003370 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003371 int send_ret;
3372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3373 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3374 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003375 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003376 * If the error is permanent we'll catch it later,
3377 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003378 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3379 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3380 (void) send_ret;
3381
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003382 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003383 }
3384
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003385 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003386 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003388 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003389 {
3390 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3391 return( ret );
3392 }
3393
3394 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003395 }
3396
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003397 return( ret );
3398}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003399#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003400
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003401static int ssl_check_record_type( uint8_t record_type )
3402{
3403 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3404 record_type != MBEDTLS_SSL_MSG_ALERT &&
3405 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3406 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3407 {
3408 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3409 }
3410
3411 return( 0 );
3412}
3413
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003414/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003415 * ContentType type;
3416 * ProtocolVersion version;
3417 * uint16 epoch; // DTLS only
3418 * uint48 sequence_number; // DTLS only
3419 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003420 *
3421 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003422 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003423 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3424 *
3425 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003426 * 1. proceed with the record if this function returns 0
3427 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3428 * 3. return CLIENT_RECONNECT if this function return that value
3429 * 4. drop the whole datagram if this function returns anything else.
3430 * Point 2 is needed when the peer is resending, and we have already received
3431 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003432 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003433static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003434 unsigned char *buf,
3435 size_t len,
3436 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003437{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003438 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
Hanno Beckere5e7e782019-07-11 12:29:35 +01003440 size_t const rec_hdr_type_offset = 0;
3441 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003442
Hanno Beckere5e7e782019-07-11 12:29:35 +01003443 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3444 rec_hdr_type_len;
3445 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
Hanno Beckere5e7e782019-07-11 12:29:35 +01003447 size_t const rec_hdr_ctr_len = 8;
3448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003449 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003450 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3451 rec_hdr_version_len;
3452
Hanno Beckera0e20d02019-05-15 14:03:01 +01003453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003454 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3455 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003456 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003457#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3458#endif /* MBEDTLS_SSL_PROTO_DTLS */
3459
3460 size_t rec_hdr_len_offset; /* To be determined */
3461 size_t const rec_hdr_len_len = 2;
3462
3463 /*
3464 * Check minimum lengths for record header.
3465 */
3466
3467#if defined(MBEDTLS_SSL_PROTO_DTLS)
3468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3469 {
3470 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3471 }
3472 else
3473#endif /* MBEDTLS_SSL_PROTO_DTLS */
3474 {
3475 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3476 }
3477
3478 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3479 {
3480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3481 (unsigned) len,
3482 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3483 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3484 }
3485
3486 /*
3487 * Parse and validate record content type
3488 */
3489
3490 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003491
3492 /* Check record content type */
3493#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3494 rec->cid_len = 0;
3495
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003497 ssl->conf->cid_len != 0 &&
3498 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003499 {
3500 /* Shift pointers to account for record header including CID
3501 * struct {
3502 * ContentType special_type = tls12_cid;
3503 * ProtocolVersion version;
3504 * uint16 epoch;
3505 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003506 * opaque cid[cid_length]; // Additional field compared to
3507 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003508 * uint16 length;
3509 * opaque enc_content[DTLSCiphertext.length];
3510 * } DTLSCiphertext;
3511 */
3512
3513 /* So far, we only support static CID lengths
3514 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003515 rec_hdr_cid_len = ssl->conf->cid_len;
3516 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003517
Hanno Beckere5e7e782019-07-11 12:29:35 +01003518 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003519 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3521 (unsigned) len,
3522 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003523 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003524 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003525
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003526 /* configured CID len is guaranteed at most 255, see
3527 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3528 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003529 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003530 }
3531 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003532#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003533 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003534 if( ssl_check_record_type( rec->type ) )
3535 {
Hanno Becker54229812019-07-12 14:40:00 +01003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3537 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003538 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3539 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003540 }
3541
Hanno Beckere5e7e782019-07-11 12:29:35 +01003542 /*
3543 * Parse and validate record version
3544 */
3545
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003546 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3547 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003548 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3549 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003550 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003551
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003552 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3555 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 }
3557
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003558 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3561 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 }
3563
Hanno Beckere5e7e782019-07-11 12:29:35 +01003564 /*
3565 * Parse/Copy record sequence number.
3566 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003567
Hanno Beckere5e7e782019-07-11 12:29:35 +01003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
3569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003570 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003571 /* Copy explicit record sequence number from input buffer. */
3572 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3573 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003574 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003575 else
3576#endif /* MBEDTLS_SSL_PROTO_DTLS */
3577 {
3578 /* Copy implicit record sequence number from SSL context structure. */
3579 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3580 }
Paul Bakker40e46942009-01-03 21:51:57 +00003581
Hanno Beckere5e7e782019-07-11 12:29:35 +01003582 /*
3583 * Parse record length.
3584 */
3585
Hanno Beckere5e7e782019-07-11 12:29:35 +01003586 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003587 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3588 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003589 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003592 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003593 rec->type,
3594 major_ver, minor_ver, rec->data_len ) );
3595
3596 rec->buf = buf;
3597 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003598
Hanno Beckerd417cc92019-07-26 08:20:27 +01003599 if( rec->data_len == 0 )
3600 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003602 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003603 * DTLS-related tests.
3604 * Check epoch before checking length constraint because
3605 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3606 * message gets duplicated before the corresponding Finished message,
3607 * the second ChangeCipherSpec should be discarded because it belongs
3608 * to an old epoch, but not because its length is shorter than
3609 * the minimum record length for packets using the new record transform.
3610 * Note that these two kinds of failures are handled differently,
3611 * as an unexpected record is silently skipped but an invalid
3612 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003613 */
3614#if defined(MBEDTLS_SSL_PROTO_DTLS)
3615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3616 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003617 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003618
Hanno Becker955a5c92019-07-10 17:12:07 +01003619 /* Check that the datagram is large enough to contain a record
3620 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003621 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003622 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3624 (unsigned) len,
3625 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3627 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003628
Hanno Becker37cfe732019-07-10 17:20:01 +01003629 /* Records from other, non-matching epochs are silently discarded.
3630 * (The case of same-port Client reconnects must be considered in
3631 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003632 if( rec_epoch != ssl->in_epoch )
3633 {
3634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3635 "expected %d, received %d",
3636 ssl->in_epoch, rec_epoch ) );
3637
Hanno Becker552f7472019-07-19 10:59:12 +01003638 /* Records from the next epoch are considered for buffering
3639 * (concretely: early Finished messages). */
3640 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003641 {
Hanno Becker552f7472019-07-19 10:59:12 +01003642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3643 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003644 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003645
Hanno Becker2fddd372019-07-10 14:37:41 +01003646 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003647 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003648#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003649 /* For records from the correct epoch, check whether their
3650 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003651 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3652 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003653 {
3654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3655 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3656 }
3657#endif
3658 }
3659#endif /* MBEDTLS_SSL_PROTO_DTLS */
3660
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003661 return( 0 );
3662}
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Paul Bakker5121ce52009-01-03 21:22:43 +00003664
Hanno Becker2fddd372019-07-10 14:37:41 +01003665#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3666static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3667{
3668 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3669
3670 /*
3671 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3672 * access the first byte of record content (handshake type), as we
3673 * have an active transform (possibly iv_len != 0), so use the
3674 * fact that the record header len is 13 instead.
3675 */
3676 if( rec_epoch == 0 &&
3677 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3678 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3679 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3680 ssl->in_left > 13 &&
3681 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3682 {
3683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3684 "from the same port" ) );
3685 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 }
3687
3688 return( 0 );
3689}
Hanno Becker2fddd372019-07-10 14:37:41 +01003690#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003691
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003692/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003693 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003694 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003695static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3696 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003697{
3698 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003701 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3704 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ret = mbedtls_ssl_hw_record_read( ssl );
3709 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3712 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003713 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003714
3715 if( ret == 0 )
3716 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003719 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003721 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003722
Hanno Beckera18d1322018-01-03 14:27:32 +00003723 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003724 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003727
Hanno Beckera0e20d02019-05-15 14:03:01 +01003728#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003729 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3730 ssl->conf->ignore_unexpected_cid
3731 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3732 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003734 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003735 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003736#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003737
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 return( ret );
3739 }
3740
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003741 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003744 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003745 }
3746
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003747 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003748 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003749
Hanno Beckera0e20d02019-05-15 14:03:01 +01003750#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003751 /* We have already checked the record content type
3752 * in ssl_parse_record_header(), failing or silently
3753 * dropping the record in the case of an unknown type.
3754 *
3755 * Since with the use of CIDs, the record content type
3756 * might change during decryption, re-check the record
3757 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003758 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003759 {
3760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3761 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3762 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003763#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003764
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003765 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003766 {
3767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3768 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003769 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003770 {
3771 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3774 }
3775#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3776
3777 ssl->nb_zero++;
3778
3779 /*
3780 * Three or more empty messages may be a DoS attack
3781 * (excessive CPU consumption).
3782 */
3783 if( ssl->nb_zero > 3 )
3784 {
3785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003786 "messages, possible DoS attack" ) );
3787 /* Treat the records as if they were not properly authenticated,
3788 * thereby failing the connection if we see more than allowed
3789 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003790 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3791 }
3792 }
3793 else
3794 ssl->nb_zero = 0;
3795
3796#if defined(MBEDTLS_SSL_PROTO_DTLS)
3797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3798 {
3799 ; /* in_ctr read from peer, not maintained internally */
3800 }
3801 else
3802#endif
3803 {
3804 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003805 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003806 if( ++ssl->in_ctr[i - 1] != 0 )
3807 break;
3808
3809 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003810 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003811 {
3812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3813 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3814 }
3815 }
3816
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 }
3818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003820 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003823 }
3824#endif
3825
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003826 /* Check actual (decrypted) record content length against
3827 * configured maximum. */
3828 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3829 {
3830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3831 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3832 }
3833
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003834 return( 0 );
3835}
3836
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003837/*
3838 * Read a record.
3839 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003840 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3841 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3842 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003843 */
Hanno Becker1097b342018-08-15 14:09:41 +01003844
3845/* Helper functions for mbedtls_ssl_read_record(). */
3846static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003847static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3848static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003849
Hanno Becker327c93b2018-08-15 13:56:18 +01003850int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003851 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003852{
Janos Follath865b3eb2019-12-16 11:46:15 +00003853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003856
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003857 if( ssl->keep_current_message == 0 )
3858 {
3859 do {
Simon Butcher99000142016-10-13 17:21:01 +01003860
Hanno Becker26994592018-08-15 14:14:59 +01003861 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003862 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003863 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003864
Hanno Beckere74d5562018-08-15 14:26:08 +01003865 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003866 {
Hanno Becker40f50842018-08-15 14:48:01 +01003867#if defined(MBEDTLS_SSL_PROTO_DTLS)
3868 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003869
Hanno Becker40f50842018-08-15 14:48:01 +01003870 /* We only check for buffered messages if the
3871 * current datagram is fully consumed. */
3872 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003873 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003874 {
Hanno Becker40f50842018-08-15 14:48:01 +01003875 if( ssl_load_buffered_message( ssl ) == 0 )
3876 have_buffered = 1;
3877 }
3878
3879 if( have_buffered == 0 )
3880#endif /* MBEDTLS_SSL_PROTO_DTLS */
3881 {
3882 ret = ssl_get_next_record( ssl );
3883 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3884 continue;
3885
3886 if( ret != 0 )
3887 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003888 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003889 return( ret );
3890 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003891 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003892 }
3893
3894 ret = mbedtls_ssl_handle_message_type( ssl );
3895
Hanno Becker40f50842018-08-15 14:48:01 +01003896#if defined(MBEDTLS_SSL_PROTO_DTLS)
3897 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3898 {
3899 /* Buffer future message */
3900 ret = ssl_buffer_message( ssl );
3901 if( ret != 0 )
3902 return( ret );
3903
3904 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3905 }
3906#endif /* MBEDTLS_SSL_PROTO_DTLS */
3907
Hanno Becker90333da2017-10-10 11:27:13 +01003908 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3909 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003910
3911 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003912 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003913 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003914 return( ret );
3915 }
3916
Hanno Becker327c93b2018-08-15 13:56:18 +01003917 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003918 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003919 {
3920 mbedtls_ssl_update_handshake_status( ssl );
3921 }
Simon Butcher99000142016-10-13 17:21:01 +01003922 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003923 else
Simon Butcher99000142016-10-13 17:21:01 +01003924 {
Hanno Becker02f59072018-08-15 14:00:24 +01003925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003926 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003927 }
3928
3929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3930
3931 return( 0 );
3932}
3933
Hanno Becker40f50842018-08-15 14:48:01 +01003934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003935static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003936{
Hanno Becker40f50842018-08-15 14:48:01 +01003937 if( ssl->in_left > ssl->next_record_offset )
3938 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003939
Hanno Becker40f50842018-08-15 14:48:01 +01003940 return( 0 );
3941}
3942
3943static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3944{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003945 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003946 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003947 int ret = 0;
3948
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003949 if( hs == NULL )
3950 return( -1 );
3951
Hanno Beckere00ae372018-08-20 09:39:42 +01003952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3953
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003954 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3955 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3956 {
3957 /* Check if we have seen a ChangeCipherSpec before.
3958 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003959 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003960 {
3961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3962 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003963 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003964 }
3965
Hanno Becker39b8bc92018-08-28 17:17:13 +01003966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003967 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3968 ssl->in_msglen = 1;
3969 ssl->in_msg[0] = 1;
3970
3971 /* As long as they are equal, the exact value doesn't matter. */
3972 ssl->in_left = 0;
3973 ssl->next_record_offset = 0;
3974
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003975 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003976 goto exit;
3977 }
Hanno Becker37f95322018-08-16 13:55:32 +01003978
Hanno Beckerb8f50142018-08-28 10:01:34 +01003979#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003980 /* Debug only */
3981 {
3982 unsigned offset;
3983 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3984 {
3985 hs_buf = &hs->buffering.hs[offset];
3986 if( hs_buf->is_valid == 1 )
3987 {
3988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3989 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003990 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003991 }
3992 }
3993 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003994#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003995
3996 /* Check if we have buffered and/or fully reassembled the
3997 * next handshake message. */
3998 hs_buf = &hs->buffering.hs[0];
3999 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4000 {
4001 /* Synthesize a record containing the buffered HS message. */
4002 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4003 ( hs_buf->data[2] << 8 ) |
4004 hs_buf->data[3];
4005
4006 /* Double-check that we haven't accidentally buffered
4007 * a message that doesn't fit into the input buffer. */
4008 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4009 {
4010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4012 }
4013
4014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4015 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4016 hs_buf->data, msg_len + 12 );
4017
4018 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4019 ssl->in_hslen = msg_len + 12;
4020 ssl->in_msglen = msg_len + 12;
4021 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4022
4023 ret = 0;
4024 goto exit;
4025 }
4026 else
4027 {
4028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4029 hs->in_msg_seq ) );
4030 }
4031
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004032 ret = -1;
4033
4034exit:
4035
4036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4037 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004038}
4039
Hanno Beckera02b0b42018-08-21 17:20:27 +01004040static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4041 size_t desired )
4042{
4043 int offset;
4044 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4046 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004047
Hanno Becker01315ea2018-08-21 17:22:17 +01004048 /* Get rid of future records epoch first, if such exist. */
4049 ssl_free_buffered_record( ssl );
4050
4051 /* Check if we have enough space available now. */
4052 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4053 hs->buffering.total_bytes_buffered ) )
4054 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004056 return( 0 );
4057 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004058
Hanno Becker4f432ad2018-08-28 10:02:32 +01004059 /* We don't have enough space to buffer the next expected handshake
4060 * message. Remove buffers used for future messages to gain space,
4061 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004062 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4063 offset >= 0; offset-- )
4064 {
4065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4066 offset ) );
4067
Hanno Beckerb309b922018-08-23 13:18:05 +01004068 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004069
4070 /* Check if we have enough space available now. */
4071 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4072 hs->buffering.total_bytes_buffered ) )
4073 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004075 return( 0 );
4076 }
4077 }
4078
4079 return( -1 );
4080}
4081
Hanno Becker40f50842018-08-15 14:48:01 +01004082static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4083{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004084 int ret = 0;
4085 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4086
4087 if( hs == NULL )
4088 return( 0 );
4089
4090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4091
4092 switch( ssl->in_msgtype )
4093 {
4094 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004096
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004097 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004098 break;
4099
4100 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004101 {
4102 unsigned recv_msg_seq_offset;
4103 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4104 mbedtls_ssl_hs_buffer *hs_buf;
4105 size_t msg_len = ssl->in_hslen - 12;
4106
4107 /* We should never receive an old handshake
4108 * message - double-check nonetheless. */
4109 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4110 {
4111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4113 }
4114
4115 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4116 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4117 {
4118 /* Silently ignore -- message too far in the future */
4119 MBEDTLS_SSL_DEBUG_MSG( 2,
4120 ( "Ignore future HS message with sequence number %u, "
4121 "buffering window %u - %u",
4122 recv_msg_seq, ssl->handshake->in_msg_seq,
4123 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4124
4125 goto exit;
4126 }
4127
4128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4129 recv_msg_seq, recv_msg_seq_offset ) );
4130
4131 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4132
4133 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004134 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004135 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004136 size_t reassembly_buf_sz;
4137
Hanno Becker37f95322018-08-16 13:55:32 +01004138 hs_buf->is_fragmented =
4139 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4140
4141 /* We copy the message back into the input buffer
4142 * after reassembly, so check that it's not too large.
4143 * This is an implementation-specific limitation
4144 * and not one from the standard, hence it is not
4145 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004146 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004147 {
4148 /* Ignore message */
4149 goto exit;
4150 }
4151
Hanno Beckere0b150f2018-08-21 15:51:03 +01004152 /* Check if we have enough space to buffer the message. */
4153 if( hs->buffering.total_bytes_buffered >
4154 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4155 {
4156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4158 }
4159
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004160 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4161 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004162
4163 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4164 hs->buffering.total_bytes_buffered ) )
4165 {
4166 if( recv_msg_seq_offset > 0 )
4167 {
4168 /* If we can't buffer a future message because
4169 * of space limitations -- ignore. */
4170 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",
4171 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4172 (unsigned) hs->buffering.total_bytes_buffered ) );
4173 goto exit;
4174 }
Hanno Beckere1801392018-08-21 16:51:05 +01004175 else
4176 {
4177 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",
4178 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4179 (unsigned) hs->buffering.total_bytes_buffered ) );
4180 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004181
Hanno Beckera02b0b42018-08-21 17:20:27 +01004182 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004183 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004184 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",
4185 (unsigned) msg_len,
4186 (unsigned) reassembly_buf_sz,
4187 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004188 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004189 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4190 goto exit;
4191 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004192 }
4193
4194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4195 msg_len ) );
4196
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004197 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4198 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004199 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004200 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004201 goto exit;
4202 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004203 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004204
4205 /* Prepare final header: copy msg_type, length and message_seq,
4206 * then add standardised fragment_offset and fragment_length */
4207 memcpy( hs_buf->data, ssl->in_msg, 6 );
4208 memset( hs_buf->data + 6, 0, 3 );
4209 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4210
4211 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004212
4213 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004214 }
4215 else
4216 {
4217 /* Make sure msg_type and length are consistent */
4218 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4219 {
4220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4221 /* Ignore */
4222 goto exit;
4223 }
4224 }
4225
Hanno Becker4422bbb2018-08-20 09:40:19 +01004226 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004227 {
4228 size_t frag_len, frag_off;
4229 unsigned char * const msg = hs_buf->data + 12;
4230
4231 /*
4232 * Check and copy current fragment
4233 */
4234
4235 /* Validation of header fields already done in
4236 * mbedtls_ssl_prepare_handshake_record(). */
4237 frag_off = ssl_get_hs_frag_off( ssl );
4238 frag_len = ssl_get_hs_frag_len( ssl );
4239
4240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4241 frag_off, frag_len ) );
4242 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4243
4244 if( hs_buf->is_fragmented )
4245 {
4246 unsigned char * const bitmask = msg + msg_len;
4247 ssl_bitmask_set( bitmask, frag_off, frag_len );
4248 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4249 msg_len ) == 0 );
4250 }
4251 else
4252 {
4253 hs_buf->is_complete = 1;
4254 }
4255
4256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4257 hs_buf->is_complete ? "" : "not yet " ) );
4258 }
4259
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004260 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004261 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004262
4263 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004264 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004265 break;
4266 }
4267
4268exit:
4269
4270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4271 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004272}
4273#endif /* MBEDTLS_SSL_PROTO_DTLS */
4274
Hanno Becker1097b342018-08-15 14:09:41 +01004275static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004276{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004277 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004278 * Consume last content-layer message and potentially
4279 * update in_msglen which keeps track of the contents'
4280 * consumption state.
4281 *
4282 * (1) Handshake messages:
4283 * Remove last handshake message, move content
4284 * and adapt in_msglen.
4285 *
4286 * (2) Alert messages:
4287 * Consume whole record content, in_msglen = 0.
4288 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004289 * (3) Change cipher spec:
4290 * Consume whole record content, in_msglen = 0.
4291 *
4292 * (4) Application data:
4293 * Don't do anything - the record layer provides
4294 * the application data as a stream transport
4295 * and consumes through mbedtls_ssl_read only.
4296 *
4297 */
4298
4299 /* Case (1): Handshake messages */
4300 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004301 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004302 /* Hard assertion to be sure that no application data
4303 * is in flight, as corrupting ssl->in_msglen during
4304 * ssl->in_offt != NULL is fatal. */
4305 if( ssl->in_offt != NULL )
4306 {
4307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4309 }
4310
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004311 /*
4312 * Get next Handshake message in the current record
4313 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004314
Hanno Becker4a810fb2017-05-24 16:27:30 +01004315 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004316 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004317 * current handshake content: If DTLS handshake
4318 * fragmentation is used, that's the fragment
4319 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004320 * size here is faulty and should be changed at
4321 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004322 * (2) While it doesn't seem to cause problems, one
4323 * has to be very careful not to assume that in_hslen
4324 * is always <= in_msglen in a sensible communication.
4325 * Again, it's wrong for DTLS handshake fragmentation.
4326 * The following check is therefore mandatory, and
4327 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004328 * Additionally, ssl->in_hslen might be arbitrarily out of
4329 * bounds after handling a DTLS message with an unexpected
4330 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004331 */
4332 if( ssl->in_hslen < ssl->in_msglen )
4333 {
4334 ssl->in_msglen -= ssl->in_hslen;
4335 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4336 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004337
Hanno Becker4a810fb2017-05-24 16:27:30 +01004338 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4339 ssl->in_msg, ssl->in_msglen );
4340 }
4341 else
4342 {
4343 ssl->in_msglen = 0;
4344 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004345
Hanno Becker4a810fb2017-05-24 16:27:30 +01004346 ssl->in_hslen = 0;
4347 }
4348 /* Case (4): Application data */
4349 else if( ssl->in_offt != NULL )
4350 {
4351 return( 0 );
4352 }
4353 /* Everything else (CCS & Alerts) */
4354 else
4355 {
4356 ssl->in_msglen = 0;
4357 }
4358
Hanno Becker1097b342018-08-15 14:09:41 +01004359 return( 0 );
4360}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004361
Hanno Beckere74d5562018-08-15 14:26:08 +01004362static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4363{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004364 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004365 return( 1 );
4366
4367 return( 0 );
4368}
4369
Hanno Becker5f066e72018-08-16 14:56:31 +01004370#if defined(MBEDTLS_SSL_PROTO_DTLS)
4371
4372static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4373{
4374 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4375 if( hs == NULL )
4376 return;
4377
Hanno Becker01315ea2018-08-21 17:22:17 +01004378 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004379 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004380 hs->buffering.total_bytes_buffered -=
4381 hs->buffering.future_record.len;
4382
4383 mbedtls_free( hs->buffering.future_record.data );
4384 hs->buffering.future_record.data = NULL;
4385 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004386}
4387
4388static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4389{
4390 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4391 unsigned char * rec;
4392 size_t rec_len;
4393 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004394#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4395 size_t in_buf_len = ssl->in_buf_len;
4396#else
4397 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4398#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004399 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4400 return( 0 );
4401
4402 if( hs == NULL )
4403 return( 0 );
4404
Hanno Becker5f066e72018-08-16 14:56:31 +01004405 rec = hs->buffering.future_record.data;
4406 rec_len = hs->buffering.future_record.len;
4407 rec_epoch = hs->buffering.future_record.epoch;
4408
4409 if( rec == NULL )
4410 return( 0 );
4411
Hanno Becker4cb782d2018-08-20 11:19:05 +01004412 /* Only consider loading future records if the
4413 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004414 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004415 return( 0 );
4416
Hanno Becker5f066e72018-08-16 14:56:31 +01004417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4418
4419 if( rec_epoch != ssl->in_epoch )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4422 goto exit;
4423 }
4424
4425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4426
4427 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004428 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4432 }
4433
4434 memcpy( ssl->in_hdr, rec, rec_len );
4435 ssl->in_left = rec_len;
4436 ssl->next_record_offset = 0;
4437
4438 ssl_free_buffered_record( ssl );
4439
4440exit:
4441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4442 return( 0 );
4443}
4444
Hanno Becker519f15d2019-07-11 12:43:20 +01004445static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4446 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004447{
4448 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004449
4450 /* Don't buffer future records outside handshakes. */
4451 if( hs == NULL )
4452 return( 0 );
4453
4454 /* Only buffer handshake records (we are only interested
4455 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004456 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004457 return( 0 );
4458
4459 /* Don't buffer more than one future epoch record. */
4460 if( hs->buffering.future_record.data != NULL )
4461 return( 0 );
4462
Hanno Becker01315ea2018-08-21 17:22:17 +01004463 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004464 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004465 hs->buffering.total_bytes_buffered ) )
4466 {
4467 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 +01004468 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004469 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004470 return( 0 );
4471 }
4472
Hanno Becker5f066e72018-08-16 14:56:31 +01004473 /* Buffer record */
4474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4475 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004476 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004477
4478 /* ssl_parse_record_header() only considers records
4479 * of the next epoch as candidates for buffering. */
4480 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004481 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004482
4483 hs->buffering.future_record.data =
4484 mbedtls_calloc( 1, hs->buffering.future_record.len );
4485 if( hs->buffering.future_record.data == NULL )
4486 {
4487 /* If we run out of RAM trying to buffer a
4488 * record from the next epoch, just ignore. */
4489 return( 0 );
4490 }
4491
Hanno Becker519f15d2019-07-11 12:43:20 +01004492 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004493
Hanno Becker519f15d2019-07-11 12:43:20 +01004494 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004495 return( 0 );
4496}
4497
4498#endif /* MBEDTLS_SSL_PROTO_DTLS */
4499
Hanno Beckere74d5562018-08-15 14:26:08 +01004500static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004501{
Janos Follath865b3eb2019-12-16 11:46:15 +00004502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004503 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004504
Hanno Becker5f066e72018-08-16 14:56:31 +01004505#if defined(MBEDTLS_SSL_PROTO_DTLS)
4506 /* We might have buffered a future record; if so,
4507 * and if the epoch matches now, load it.
4508 * On success, this call will set ssl->in_left to
4509 * the length of the buffered record, so that
4510 * the calls to ssl_fetch_input() below will
4511 * essentially be no-ops. */
4512 ret = ssl_load_buffered_record( ssl );
4513 if( ret != 0 )
4514 return( ret );
4515#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004516
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004517 /* Ensure that we have enough space available for the default form
4518 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4519 * with no space for CIDs counted in). */
4520 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4521 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004524 return( ret );
4525 }
4526
Hanno Beckere5e7e782019-07-11 12:29:35 +01004527 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4528 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004532 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004533 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4534 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004535 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004536 if( ret != 0 )
4537 return( ret );
4538
4539 /* Fall through to handling of unexpected records */
4540 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4541 }
4542
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004543 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4544 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004545#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004546 /* Reset in pointers to default state for TLS/DTLS records,
4547 * assuming no CID and no offset between record content and
4548 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004549 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004550
Hanno Becker7ae20e02019-07-12 08:33:49 +01004551 /* Setup internal message pointers from record structure. */
4552 ssl->in_msgtype = rec.type;
4553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4554 ssl->in_len = ssl->in_cid + rec.cid_len;
4555#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4556 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4557 ssl->in_msglen = rec.data_len;
4558
Hanno Becker2fddd372019-07-10 14:37:41 +01004559 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004560 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004561 if( ret != 0 )
4562 return( ret );
4563#endif
4564
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004565 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004566 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004567
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4569 "(header)" ) );
4570 }
4571 else
4572 {
4573 /* Skip invalid record and the rest of the datagram */
4574 ssl->next_record_offset = 0;
4575 ssl->in_left = 0;
4576
4577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4578 "(header)" ) );
4579 }
4580
4581 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004582 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004583 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004584 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004585#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004586 {
4587 return( ret );
4588 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004589 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004592 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004593 {
Hanno Beckera8814792019-07-10 15:01:45 +01004594 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004595 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004596 if( ssl->next_record_offset < ssl->in_left )
4597 {
4598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4599 }
4600 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601 else
4602#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004603 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004604 /*
4605 * Fetch record contents from underlying transport.
4606 */
Hanno Beckera3175662019-07-11 12:50:29 +01004607 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004608 if( ret != 0 )
4609 {
4610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4611 return( ret );
4612 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004613
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004614 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004615 }
4616
4617 /*
4618 * Decrypt record contents.
4619 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004620
Hanno Beckerfdf66042019-07-11 13:07:45 +01004621 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004624 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004625 {
4626 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004627 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004628 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004629 /* Except when waiting for Finished as a bad mac here
4630 * probably means something went wrong in the handshake
4631 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4632 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4633 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4634 {
4635#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4636 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4637 {
4638 mbedtls_ssl_send_alert_message( ssl,
4639 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4640 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4641 }
4642#endif
4643 return( ret );
4644 }
4645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004647 if( ssl->conf->badmac_limit != 0 &&
4648 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004652 }
4653#endif
4654
Hanno Becker4a810fb2017-05-24 16:27:30 +01004655 /* As above, invalid records cause
4656 * dismissal of the whole datagram. */
4657
4658 ssl->next_record_offset = 0;
4659 ssl->in_left = 0;
4660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004662 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004663 }
4664
4665 return( ret );
4666 }
4667 else
4668#endif
4669 {
4670 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4672 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 mbedtls_ssl_send_alert_message( ssl,
4675 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4676 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004677 }
4678#endif
4679 return( ret );
4680 }
4681 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004682
Hanno Becker44d89b22019-07-12 09:40:44 +01004683
4684 /* Reset in pointers to default state for TLS/DTLS records,
4685 * assuming no CID and no offset between record content and
4686 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004687 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4689 ssl->in_len = ssl->in_cid + rec.cid_len;
4690#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004691 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004692
Hanno Becker8685c822019-07-12 09:37:30 +01004693 /* The record content type may change during decryption,
4694 * so re-read it. */
4695 ssl->in_msgtype = rec.type;
4696 /* Also update the input buffer, because unfortunately
4697 * the server-side ssl_parse_client_hello() reparses the
4698 * record header when receiving a ClientHello initiating
4699 * a renegotiation. */
4700 ssl->in_hdr[0] = rec.type;
4701 ssl->in_msg = rec.buf + rec.data_offset;
4702 ssl->in_msglen = rec.data_len;
4703 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4704 ssl->in_len[1] = (unsigned char)( rec.data_len );
4705
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004706#if defined(MBEDTLS_ZLIB_SUPPORT)
4707 if( ssl->transform_in != NULL &&
4708 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4709 {
4710 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4711 {
4712 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4713 return( ret );
4714 }
4715
4716 /* Check actual (decompress) record content length against
4717 * configured maximum. */
4718 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4721 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4722 }
4723 }
4724#endif /* MBEDTLS_ZLIB_SUPPORT */
4725
Simon Butcher99000142016-10-13 17:21:01 +01004726 return( 0 );
4727}
4728
4729int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4730{
Janos Follath865b3eb2019-12-16 11:46:15 +00004731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004732
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004733 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004734 * Handle particular types of records
4735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 {
Simon Butcher99000142016-10-13 17:21:01 +01004738 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4739 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004740 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004742 }
4743
Hanno Beckere678eaa2018-08-21 14:57:46 +01004744 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004745 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004746 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004747 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4749 ssl->in_msglen ) );
4750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004751 }
4752
Hanno Beckere678eaa2018-08-21 14:57:46 +01004753 if( ssl->in_msg[0] != 1 )
4754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4756 ssl->in_msg[0] ) );
4757 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4758 }
4759
4760#if defined(MBEDTLS_SSL_PROTO_DTLS)
4761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4762 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4763 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4764 {
4765 if( ssl->handshake == NULL )
4766 {
4767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4768 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4769 }
4770
4771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4772 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4773 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004774#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004775 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004779 if( ssl->in_msglen != 2 )
4780 {
4781 /* Note: Standard allows for more than one 2 byte alert
4782 to be packed in a single message, but Mbed TLS doesn't
4783 currently support this. */
4784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4785 ssl->in_msglen ) );
4786 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4787 }
4788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 ssl->in_msg[0], ssl->in_msg[1] ) );
4791
4792 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004793 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004798 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004800 }
4801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4803 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4806 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004807 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004808
4809#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4810 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4811 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4812 {
Hanno Becker90333da2017-10-10 11:27:13 +01004813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004814 /* Will be handled when trying to parse ServerHello */
4815 return( 0 );
4816 }
4817#endif
4818
4819#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4820 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4821 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4822 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4823 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4824 {
4825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4826 /* Will be handled in mbedtls_ssl_parse_certificate() */
4827 return( 0 );
4828 }
4829#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4830
4831 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004832 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 }
4834
Hanno Beckerc76c6192017-06-06 10:03:17 +01004835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004837 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004838 /* Drop unexpected ApplicationData records,
4839 * except at the beginning of renegotiations */
4840 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4841 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4842#if defined(MBEDTLS_SSL_RENEGOTIATION)
4843 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4844 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004845#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004846 )
4847 {
4848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4849 return( MBEDTLS_ERR_SSL_NON_FATAL );
4850 }
4851
4852 if( ssl->handshake != NULL &&
4853 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4854 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004855 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004856 }
4857 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004858#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004859
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 return( 0 );
4861}
4862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004864{
irwir6c0da642019-09-26 21:07:41 +03004865 return( mbedtls_ssl_send_alert_message( ssl,
4866 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4867 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004868}
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004871 unsigned char level,
4872 unsigned char message )
4873{
Janos Follath865b3eb2019-12-16 11:46:15 +00004874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004875
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004876 if( ssl == NULL || ssl->conf == NULL )
4877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004883 ssl->out_msglen = 2;
4884 ssl->out_msg[0] = level;
4885 ssl->out_msg[1] = message;
4886
Hanno Becker67bc7c32018-08-06 11:33:50 +01004887 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004890 return( ret );
4891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004893
4894 return( 0 );
4895}
4896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004898{
Janos Follath865b3eb2019-12-16 11:46:15 +00004899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 ssl->out_msglen = 1;
4905 ssl->out_msg[0] = 1;
4906
Paul Bakker5121ce52009-01-03 21:22:43 +00004907 ssl->state++;
4908
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004909 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 return( ret );
4913 }
4914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004916
4917 return( 0 );
4918}
4919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004921{
Janos Follath865b3eb2019-12-16 11:46:15 +00004922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004925
Hanno Becker327c93b2018-08-15 13:56:18 +01004926 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 return( ret );
4930 }
4931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004935 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4936 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 }
4939
Hanno Beckere678eaa2018-08-21 14:57:46 +01004940 /* CCS records are only accepted if they have length 1 and content '1',
4941 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004942
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004943 /*
4944 * Switch to our negotiated transform and session parameters for inbound
4945 * data.
4946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004948 ssl->transform_in = ssl->transform_negotiate;
4949 ssl->session_in = ssl->session_negotiate;
4950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004955 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004956#endif
4957
4958 /* Increment epoch */
4959 if( ++ssl->in_epoch == 0 )
4960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004962 /* This is highly unlikely to happen for legitimate reasons, so
4963 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004965 }
4966 }
4967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004969 memset( ssl->in_ctr, 0, 8 );
4970
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004971 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4974 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4980 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004982 }
4983 }
4984#endif
4985
Paul Bakker5121ce52009-01-03 21:22:43 +00004986 ssl->state++;
4987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004989
4990 return( 0 );
4991}
4992
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004993/* Once ssl->out_hdr as the address of the beginning of the
4994 * next outgoing record is set, deduce the other pointers.
4995 *
4996 * Note: For TLS, we save the implicit record sequence number
4997 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4998 * and the caller has to make sure there's space for this.
4999 */
5000
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005001static size_t ssl_transform_get_explicit_iv_len(
5002 mbedtls_ssl_transform const *transform )
5003{
5004 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5005 return( 0 );
5006
5007 return( transform->ivlen - transform->fixed_ivlen );
5008}
5009
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005010void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5011 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005012{
5013#if defined(MBEDTLS_SSL_PROTO_DTLS)
5014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5015 {
5016 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005017#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005018 ssl->out_cid = ssl->out_ctr + 8;
5019 ssl->out_len = ssl->out_cid;
5020 if( transform != NULL )
5021 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005022#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005023 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005024#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005025 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005026 }
5027 else
5028#endif
5029 {
5030 ssl->out_ctr = ssl->out_hdr - 8;
5031 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005032#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005033 ssl->out_cid = ssl->out_len;
5034#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005035 ssl->out_iv = ssl->out_hdr + 5;
5036 }
5037
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005038 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005039 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005040 if( transform != NULL )
5041 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005042}
5043
5044/* Once ssl->in_hdr as the address of the beginning of the
5045 * next incoming record is set, deduce the other pointers.
5046 *
5047 * Note: For TLS, we save the implicit record sequence number
5048 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5049 * and the caller has to make sure there's space for this.
5050 */
5051
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005052void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005053{
Hanno Becker79594fd2019-05-08 09:38:41 +01005054 /* This function sets the pointers to match the case
5055 * of unprotected TLS/DTLS records, with both ssl->in_iv
5056 * and ssl->in_msg pointing to the beginning of the record
5057 * content.
5058 *
5059 * When decrypting a protected record, ssl->in_msg
5060 * will be shifted to point to the beginning of the
5061 * record plaintext.
5062 */
5063
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005064#if defined(MBEDTLS_SSL_PROTO_DTLS)
5065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5066 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005067 /* This sets the header pointers to match records
5068 * without CID. When we receive a record containing
5069 * a CID, the fields are shifted accordingly in
5070 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005071 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005072#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005073 ssl->in_cid = ssl->in_ctr + 8;
5074 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005075#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005076 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005077#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005078 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005079 }
5080 else
5081#endif
5082 {
5083 ssl->in_ctr = ssl->in_hdr - 8;
5084 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005085#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005086 ssl->in_cid = ssl->in_len;
5087#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005088 ssl->in_iv = ssl->in_hdr + 5;
5089 }
5090
Hanno Becker79594fd2019-05-08 09:38:41 +01005091 /* This will be adjusted at record decryption time. */
5092 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005093}
5094
Paul Bakker5121ce52009-01-03 21:22:43 +00005095/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005096 * Setup an SSL context
5097 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005098
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005099void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005100{
5101 /* Set the incoming and outgoing record pointers. */
5102#if defined(MBEDTLS_SSL_PROTO_DTLS)
5103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5104 {
5105 ssl->out_hdr = ssl->out_buf;
5106 ssl->in_hdr = ssl->in_buf;
5107 }
5108 else
5109#endif /* MBEDTLS_SSL_PROTO_DTLS */
5110 {
5111 ssl->out_hdr = ssl->out_buf + 8;
5112 ssl->in_hdr = ssl->in_buf + 8;
5113 }
5114
5115 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005116 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5117 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005118}
5119
Paul Bakker5121ce52009-01-03 21:22:43 +00005120/*
5121 * SSL get accessors
5122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005124{
5125 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5126}
5127
Hanno Becker8b170a02017-10-10 11:51:19 +01005128int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5129{
5130 /*
5131 * Case A: We're currently holding back
5132 * a message for further processing.
5133 */
5134
5135 if( ssl->keep_current_message == 1 )
5136 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005138 return( 1 );
5139 }
5140
5141 /*
5142 * Case B: Further records are pending in the current datagram.
5143 */
5144
5145#if defined(MBEDTLS_SSL_PROTO_DTLS)
5146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5147 ssl->in_left > ssl->next_record_offset )
5148 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005150 return( 1 );
5151 }
5152#endif /* MBEDTLS_SSL_PROTO_DTLS */
5153
5154 /*
5155 * Case C: A handshake message is being processed.
5156 */
5157
Hanno Becker8b170a02017-10-10 11:51:19 +01005158 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5159 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005161 return( 1 );
5162 }
5163
5164 /*
5165 * Case D: An application data message is being processed
5166 */
5167 if( ssl->in_offt != NULL )
5168 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005170 return( 1 );
5171 }
5172
5173 /*
5174 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005175 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005176 * we implement support for multiple alerts in single records.
5177 */
5178
5179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5180 return( 0 );
5181}
5182
Paul Bakker43ca69c2011-01-15 17:35:19 +00005183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005185{
Hanno Becker3136ede2018-08-17 15:28:19 +01005186 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005188 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005189
Hanno Becker5903de42019-05-03 14:46:38 +01005190 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5191
Hanno Becker78640902018-08-13 16:35:15 +01005192 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005193 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195#if defined(MBEDTLS_ZLIB_SUPPORT)
5196 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5197 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005198#endif
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 case MBEDTLS_MODE_GCM:
5203 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005204 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005206 transform_expansion = transform->minlen;
5207 break;
5208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005210
5211 block_size = mbedtls_cipher_get_block_size(
5212 &transform->cipher_ctx_enc );
5213
Hanno Becker3136ede2018-08-17 15:28:19 +01005214 /* Expansion due to the addition of the MAC. */
5215 transform_expansion += transform->maclen;
5216
5217 /* Expansion due to the addition of CBC padding;
5218 * Theoretically up to 256 bytes, but we never use
5219 * more than the block size of the underlying cipher. */
5220 transform_expansion += block_size;
5221
5222 /* For TLS 1.1 or higher, an explicit IV is added
5223 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005224#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5225 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005226 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005227#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005228
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005229 break;
5230
5231 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005234 }
5235
Hanno Beckera0e20d02019-05-15 14:03:01 +01005236#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005237 if( transform->out_cid_len != 0 )
5238 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005239#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005240
Hanno Becker5903de42019-05-03 14:46:38 +01005241 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005242}
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005245/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005246 * Check record counters and renegotiate if they're above the limit.
5247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005249{
Hanno Beckerdd772292020-02-05 10:38:31 +00005250 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005251 int in_ctr_cmp;
5252 int out_ctr_cmp;
5253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5255 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005256 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005257 {
5258 return( 0 );
5259 }
5260
Andres AG2196c7f2016-12-15 17:01:16 +00005261 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5262 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005263 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005264 ssl->conf->renego_period + ep_len, 8 - ep_len );
5265
5266 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005267 {
5268 return( 0 );
5269 }
5270
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005275
5276/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 * Receive application data decrypted from the SSL layer
5278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280{
Janos Follath865b3eb2019-12-16 11:46:15 +00005281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005282 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005284 if( ssl == NULL || ssl->conf == NULL )
5285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005293 return( ret );
5294
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005295 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005297 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005298 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005299 return( ret );
5300 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005301 }
5302#endif
5303
Hanno Becker4a810fb2017-05-24 16:27:30 +01005304 /*
5305 * Check if renegotiation is necessary and/or handshake is
5306 * in process. If yes, perform/continue, and fall through
5307 * if an unexpected packet is received while the client
5308 * is waiting for the ServerHello.
5309 *
5310 * (There is no equivalent to the last condition on
5311 * the server-side as it is not treated as within
5312 * a handshake while waiting for the ClientHello
5313 * after a renegotiation request.)
5314 */
5315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005317 ret = ssl_check_ctr_renegotiate( ssl );
5318 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5319 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005322 return( ret );
5323 }
5324#endif
5325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005329 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5330 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 return( ret );
5334 }
5335 }
5336
Hanno Beckere41158b2017-10-23 13:30:32 +01005337 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005338 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005340 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005341 if( ssl->f_get_timer != NULL &&
5342 ssl->f_get_timer( ssl->p_timer ) == -1 )
5343 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005344 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005345 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005346
Hanno Becker327c93b2018-08-15 13:56:18 +01005347 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005349 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5350 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005351
Hanno Becker4a810fb2017-05-24 16:27:30 +01005352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5353 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005354 }
5355
5356 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 {
5359 /*
5360 * OpenSSL sends empty messages to randomize the IV
5361 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005365 return( 0 );
5366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005368 return( ret );
5369 }
5370 }
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005375
Hanno Becker4a810fb2017-05-24 16:27:30 +01005376 /*
5377 * - For client-side, expect SERVER_HELLO_REQUEST.
5378 * - For server-side, expect CLIENT_HELLO.
5379 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5380 */
5381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005385 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005388
5389 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005392 {
5393 continue;
5394 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005397 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005398#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005399
Hanno Becker4a810fb2017-05-24 16:27:30 +01005400#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005401 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005405
5406 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005409 {
5410 continue;
5411 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005414 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005415#endif /* MBEDTLS_SSL_SRV_C */
5416
Hanno Becker21df7f92017-10-17 11:03:26 +01005417#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005418 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005419 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5420 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5421 ssl->conf->allow_legacy_renegotiation ==
5422 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5423 {
5424 /*
5425 * Accept renegotiation request
5426 */
Paul Bakker48916f92012-09-16 19:57:18 +00005427
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005428 /* DTLS clients need to know renego is server-initiated */
5429#if defined(MBEDTLS_SSL_PROTO_DTLS)
5430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5431 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5432 {
5433 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5434 }
5435#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005436 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005437 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5438 ret != 0 )
5439 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5441 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005442 return( ret );
5443 }
5444 }
5445 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005446#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005447 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 /*
5449 * Refuse renegotiation
5450 */
5451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454#if defined(MBEDTLS_SSL_PROTO_SSL3)
5455 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005456 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005457 /* SSLv3 does not have a "no_renegotiation" warning, so
5458 we send a fatal alert and abort the connection. */
5459 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5460 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5461 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005462 }
5463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5465#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5466 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5467 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5470 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5471 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005472 {
5473 return( ret );
5474 }
Paul Bakker48916f92012-09-16 19:57:18 +00005475 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005476 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5478 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005482 }
Paul Bakker48916f92012-09-16 19:57:18 +00005483 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005484
Hanno Becker90333da2017-10-10 11:27:13 +01005485 /* At this point, we don't know whether the renegotiation has been
5486 * completed or not. The cases to consider are the following:
5487 * 1) The renegotiation is complete. In this case, no new record
5488 * has been read yet.
5489 * 2) The renegotiation is incomplete because the client received
5490 * an application data record while awaiting the ServerHello.
5491 * 3) The renegotiation is incomplete because the client received
5492 * a non-handshake, non-application data message while awaiting
5493 * the ServerHello.
5494 * In each of these case, looping will be the proper action:
5495 * - For 1), the next iteration will read a new record and check
5496 * if it's application data.
5497 * - For 2), the loop condition isn't satisfied as application data
5498 * is present, hence continue is the same as break
5499 * - For 3), the loop condition is satisfied and read_record
5500 * will re-deliver the message that was held back by the client
5501 * when expecting the ServerHello.
5502 */
5503 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005504 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005505#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005507 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005508 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005509 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005510 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005513 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005515 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005516 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5521 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005524 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005525 }
5526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5530 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005531 }
5532
5533 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005534
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005535 /* We're going to return something now, cancel timer,
5536 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005538 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005539
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005541 /* If we requested renego but received AppData, resend HelloRequest.
5542 * Do it now, after setting in_offt, to avoid taking this branch
5543 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005547 {
Hanno Becker786300f2020-02-05 10:46:40 +00005548 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005549 {
Hanno Becker786300f2020-02-05 10:46:40 +00005550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5551 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005552 return( ret );
5553 }
5554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005556#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 }
5558
5559 n = ( len < ssl->in_msglen )
5560 ? len : ssl->in_msglen;
5561
5562 memcpy( buf, ssl->in_offt, n );
5563 ssl->in_msglen -= n;
5564
5565 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005566 {
5567 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005568 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005569 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005571 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005572 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005573 /* more data available */
5574 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005578
Paul Bakker23986e52011-04-24 08:57:21 +00005579 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005580}
5581
5582/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005583 * Send application data to be encrypted by the SSL layer, taking care of max
5584 * fragment length and buffer size.
5585 *
5586 * According to RFC 5246 Section 6.2.1:
5587 *
5588 * Zero-length fragments of Application data MAY be sent as they are
5589 * potentially useful as a traffic analysis countermeasure.
5590 *
5591 * Therefore, it is possible that the input message length is 0 and the
5592 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005594static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005595 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005597 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5598 const size_t max_len = (size_t) ret;
5599
5600 if( ret < 0 )
5601 {
5602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5603 return( ret );
5604 }
5605
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005606 if( len > max_len )
5607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005612 "maximum fragment length: %d > %d",
5613 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005615 }
5616 else
5617#endif
5618 len = max_len;
5619 }
Paul Bakker887bd502011-06-08 13:10:54 +00005620
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 if( ssl->out_left != 0 )
5622 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005623 /*
5624 * The user has previously tried to send the data and
5625 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5626 * written. In this case, we expect the high-level write function
5627 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 return( ret );
5633 }
5634 }
Paul Bakker887bd502011-06-08 13:10:54 +00005635 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005636 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005637 /*
5638 * The user is trying to send a message the first time, so we need to
5639 * copy the data into the internal buffers and setup the data structure
5640 * to keep track of partial writes
5641 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005642 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005644 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005645
Hanno Becker67bc7c32018-08-06 11:33:50 +01005646 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005649 return( ret );
5650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 }
5652
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005653 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654}
5655
5656/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005657 * Write application data, doing 1/n-1 splitting if necessary.
5658 *
5659 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005660 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005661 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005664static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005665 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005666{
Janos Follath865b3eb2019-12-16 11:46:15 +00005667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005668
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005669 if( ssl->conf->cbc_record_splitting ==
5670 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005671 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5673 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5674 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005675 {
5676 return( ssl_write_real( ssl, buf, len ) );
5677 }
5678
5679 if( ssl->split_done == 0 )
5680 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005681 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005682 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005683 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005684 }
5685
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005686 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5687 return( ret );
5688 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005689
5690 return( ret + 1 );
5691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005693
5694/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005695 * Write application data (public-facing wrapper)
5696 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005697int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005698{
Janos Follath865b3eb2019-12-16 11:46:15 +00005699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005700
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005702
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005703 if( ssl == NULL || ssl->conf == NULL )
5704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5705
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005706#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005707 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5708 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005709 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005710 return( ret );
5711 }
5712#endif
5713
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005714 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005715 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005716 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005717 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005719 return( ret );
5720 }
5721 }
5722
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005723#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005724 ret = ssl_write_split( ssl, buf, len );
5725#else
5726 ret = ssl_write_real( ssl, buf, len );
5727#endif
5728
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005730
5731 return( ret );
5732}
5733
5734/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 * Notify the peer that the connection is being closed
5736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005738{
Janos Follath865b3eb2019-12-16 11:46:15 +00005739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005740
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005741 if( ssl == NULL || ssl->conf == NULL )
5742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005745
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005746 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5752 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5753 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 return( ret );
5757 }
5758 }
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005761
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005762 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005763}
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005766{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005767 if( transform == NULL )
5768 return;
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005771 deflateEnd( &transform->ctx_deflate );
5772 inflateEnd( &transform->ctx_inflate );
5773#endif
5774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5776 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005777
Hanno Beckerd56ed242018-01-03 15:32:51 +00005778#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 mbedtls_md_free( &transform->md_ctx_enc );
5780 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005781#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005782
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005783 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005784}
5785
Hanno Becker0271f962018-08-16 13:23:47 +01005786#if defined(MBEDTLS_SSL_PROTO_DTLS)
5787
Hanno Becker533ab5f2020-02-05 10:49:13 +00005788void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005789{
5790 unsigned offset;
5791 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5792
5793 if( hs == NULL )
5794 return;
5795
Hanno Becker283f5ef2018-08-24 09:34:47 +01005796 ssl_free_buffered_record( ssl );
5797
Hanno Becker0271f962018-08-16 13:23:47 +01005798 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005799 ssl_buffering_free_slot( ssl, offset );
5800}
5801
5802static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5803 uint8_t slot )
5804{
5805 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5806 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005807
5808 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5809 return;
5810
Hanno Beckere605b192018-08-21 15:59:07 +01005811 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005812 {
Hanno Beckere605b192018-08-21 15:59:07 +01005813 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005814 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005815 mbedtls_free( hs_buf->data );
5816 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005817 }
5818}
5819
5820#endif /* MBEDTLS_SSL_PROTO_DTLS */
5821
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005822/*
5823 * Convert version numbers to/from wire format
5824 * and, for DTLS, to/from TLS equivalent.
5825 *
5826 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005827 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005828 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5829 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005832 unsigned char ver[2] )
5833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_PROTO_DTLS)
5835 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005838 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5839
5840 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5841 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5842 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005843 else
5844#else
5845 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005846#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005847 {
5848 ver[0] = (unsigned char) major;
5849 ver[1] = (unsigned char) minor;
5850 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005851}
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005854 const unsigned char ver[2] )
5855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856#if defined(MBEDTLS_SSL_PROTO_DTLS)
5857 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005858 {
5859 *major = 255 - ver[0] + 2;
5860 *minor = 255 - ver[1] + 1;
5861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005863 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5864 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005865 else
5866#else
5867 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005868#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005869 {
5870 *major = ver[0];
5871 *minor = ver[1];
5872 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005873}
5874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#endif /* MBEDTLS_SSL_TLS_C */