blob: 57b39c74c64d36daf1f75812014f1361c60bce83 [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 Becker581bc1b2020-05-04 12:20:03 +0100346/* This functions transforms a (D)TLS plaintext fragment and a record content
347 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
348 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
349 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100350 *
351 * struct {
352 * opaque content[DTLSPlaintext.length];
353 * ContentType real_type;
354 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100355 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100356 *
357 * Input:
358 * - `content`: The beginning of the buffer holding the
359 * plaintext to be wrapped.
360 * - `*content_size`: The length of the plaintext in Bytes.
361 * - `max_len`: The number of Bytes available starting from
362 * `content`. This must be `>= *content_size`.
363 * - `rec_type`: The desired record content type.
364 *
365 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100366 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
367 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100368 *
369 * Returns:
370 * - `0` on success.
371 * - A negative error code if `max_len` didn't offer enough space
372 * for the expansion.
373 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100374static int ssl_build_inner_plaintext( unsigned char *content,
375 size_t *content_size,
376 size_t remaining,
377 uint8_t rec_type )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100378{
379 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +0100380 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
381 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
382 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100383
384 /* Write real content type */
385 if( remaining == 0 )
386 return( -1 );
387 content[ len ] = rec_type;
388 len++;
389 remaining--;
390
391 if( remaining < pad )
392 return( -1 );
393 memset( content + len, 0, pad );
394 len += pad;
395 remaining -= pad;
396
397 *content_size = len;
398 return( 0 );
399}
400
Hanno Becker581bc1b2020-05-04 12:20:03 +0100401/* This function parses a (D)TLSInnerPlaintext structure.
402 * See ssl_build_inner_plaintext() for details. */
403static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100404 size_t *content_size,
405 uint8_t *rec_type )
406{
407 size_t remaining = *content_size;
408
409 /* Determine length of padding by skipping zeroes from the back. */
410 do
411 {
412 if( remaining == 0 )
413 return( -1 );
414 remaining--;
415 } while( content[ remaining ] == 0 );
416
417 *content_size = remaining;
418 *rec_type = content[ remaining ];
419
420 return( 0 );
421}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100422#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
423 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100424
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100425/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100426 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000427static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100428 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100429 mbedtls_record *rec,
430 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000431{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100432 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100433 *
434 * additional_data = seq_num + TLSCompressed.type +
435 * TLSCompressed.version + TLSCompressed.length;
436 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100437 * For the CID extension, this is extended as follows
438 * (quoting draft-ietf-tls-dtls-connection-id-05,
439 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100440 *
441 * additional_data = seq_num + DTLSPlaintext.type +
442 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100443 * cid +
444 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100445 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100446 *
447 * For TLS 1.3, the record sequence number is dropped from the AAD
448 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100449 */
450
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100451 unsigned char *cur = add_data;
452
453#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
454 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
455#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
456 {
457 ((void) minor_ver);
458 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
459 cur += sizeof( rec->ctr );
460 }
461
462 *cur = rec->type;
463 cur++;
464
465 memcpy( cur, rec->ver, sizeof( rec->ver ) );
466 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100467
Hanno Beckera0e20d02019-05-15 14:03:01 +0100468#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100469 if( rec->cid_len != 0 )
470 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100471 memcpy( cur, rec->cid, rec->cid_len );
472 cur += rec->cid_len;
473
474 *cur = rec->cid_len;
475 cur++;
476
477 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
478 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
479 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100480 }
481 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100483 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100484 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
485 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
486 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100487 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100488
489 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000490}
491
Hanno Becker9d062f92020-02-07 10:26:36 +0000492#if defined(MBEDTLS_SSL_PROTO_SSL3)
493
494#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
495
496/*
497 * SSLv3.0 MAC functions
498 */
499static void ssl_mac( mbedtls_md_context_t *md_ctx,
500 const unsigned char *secret,
501 const unsigned char *buf, size_t len,
502 const unsigned char *ctr, int type,
503 unsigned char out[SSL3_MAC_MAX_BYTES] )
504{
505 unsigned char header[11];
506 unsigned char padding[48];
507 int padlen;
508 int md_size = mbedtls_md_get_size( md_ctx->md_info );
509 int md_type = mbedtls_md_get_type( md_ctx->md_info );
510
511 /* Only MD5 and SHA-1 supported */
512 if( md_type == MBEDTLS_MD_MD5 )
513 padlen = 48;
514 else
515 padlen = 40;
516
517 memcpy( header, ctr, 8 );
518 header[ 8] = (unsigned char) type;
519 header[ 9] = (unsigned char)( len >> 8 );
520 header[10] = (unsigned char)( len );
521
522 memset( padding, 0x36, padlen );
523 mbedtls_md_starts( md_ctx );
524 mbedtls_md_update( md_ctx, secret, md_size );
525 mbedtls_md_update( md_ctx, padding, padlen );
526 mbedtls_md_update( md_ctx, header, 11 );
527 mbedtls_md_update( md_ctx, buf, len );
528 mbedtls_md_finish( md_ctx, out );
529
530 memset( padding, 0x5C, padlen );
531 mbedtls_md_starts( md_ctx );
532 mbedtls_md_update( md_ctx, secret, md_size );
533 mbedtls_md_update( md_ctx, padding, padlen );
534 mbedtls_md_update( md_ctx, out, md_size );
535 mbedtls_md_finish( md_ctx, out );
536}
537#endif /* MBEDTLS_SSL_PROTO_SSL3 */
538
Hanno Beckera18d1322018-01-03 14:27:32 +0000539int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
540 mbedtls_ssl_transform *transform,
541 mbedtls_record *rec,
542 int (*f_rng)(void *, unsigned char *, size_t),
543 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100546 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000547 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100548 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100549 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000550 size_t post_avail;
551
552 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000553#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200554 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000555 ((void) ssl);
556#endif
557
558 /* The PRNG is used for dynamic IV generation that's used
559 * for CBC transformations in TLS 1.1 and TLS 1.2. */
560#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
561 ( defined(MBEDTLS_AES_C) || \
562 defined(MBEDTLS_ARIA_C) || \
563 defined(MBEDTLS_CAMELLIA_C) ) && \
564 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
565 ((void) f_rng);
566 ((void) p_rng);
567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000571 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100572 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
575 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100576 if( rec == NULL
577 || rec->buf == NULL
578 || rec->buf_len < rec->data_offset
579 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100580#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100581 || rec->cid_len != 0
582#endif
583 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000584 {
585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100587 }
588
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000589 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100590 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000592 data, rec->data_len );
593
594 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
595
596 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
597 {
598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
599 (unsigned) rec->data_len,
600 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
602 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100603
Hanno Becker92313402020-05-20 13:58:58 +0100604 /* The following two code paths implement the (D)TLSInnerPlaintext
605 * structure present in TLS 1.3 and DTLS 1.2 + CID.
606 *
607 * See ssl_build_inner_plaintext() for more information.
608 *
609 * Note that this changes `rec->data_len`, and hence
610 * `post_avail` needs to be recalculated afterwards.
611 *
612 * Note also that the two code paths cannot occur simultaneously
613 * since they apply to different versions of the protocol. There
614 * is hence no risk of double-addition of the inner plaintext.
615 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100616#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
617 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
618 {
Hanno Beckerccc13d02020-05-04 12:30:04 +0100619 if( ssl_build_inner_plaintext( data,
620 &rec->data_len,
621 post_avail,
622 rec->type ) != 0 )
623 {
624 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
625 }
626
627 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
628 }
629#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
630
Hanno Beckera0e20d02019-05-15 14:03:01 +0100631#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100632 /*
633 * Add CID information
634 */
635 rec->cid_len = transform->out_cid_len;
636 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
637 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100638
639 if( rec->cid_len != 0 )
640 {
641 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100642 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100643 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100644 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100645 * Note that this changes `rec->data_len`, and hence
646 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100647 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100648 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100649 &rec->data_len,
650 post_avail,
651 rec->type ) != 0 )
652 {
653 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
654 }
655
656 rec->type = MBEDTLS_SSL_MSG_CID;
657 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100658#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100659
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100660 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
661
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100663 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 */
Hanno Becker52344c22018-01-03 15:24:20 +0000665#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( mode == MBEDTLS_MODE_STREAM ||
667 ( mode == MBEDTLS_MODE_CBC
668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000669 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100670#endif
671 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000673 if( post_avail < transform->maclen )
674 {
675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
676 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
677 }
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000680 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200681 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000682 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000683 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
684 data, rec->data_len, rec->ctr, rec->type, mac );
685 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200686 }
687 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
690 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000691 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200692 {
Hanno Becker992b6872017-11-09 18:57:39 +0000693 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
694
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100695 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
696 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000697
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000698 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100699 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000700 mbedtls_md_hmac_update( &transform->md_ctx_enc,
701 data, rec->data_len );
702 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
703 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
704
705 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200706 }
707 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200712 }
713
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000714 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
715 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200716
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000717 rec->data_len += transform->maclen;
718 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100719 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200720 }
Hanno Becker52344c22018-01-03 15:24:20 +0000721#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200723 /*
724 * Encrypt
725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
727 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000730 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000732 "including %d bytes of padding",
733 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000735 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
736 transform->iv_enc, transform->ivlen,
737 data, rec->data_len,
738 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200741 return( ret );
742 }
743
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000744 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200748 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 }
Paul Bakker68884e32013-01-07 18:20:04 +0100750 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000752
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200753#if defined(MBEDTLS_GCM_C) || \
754 defined(MBEDTLS_CCM_C) || \
755 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200757 mode == MBEDTLS_MODE_CCM ||
758 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000759 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200761 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000762 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000763
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100764 /* Check that there's space for the authentication tag. */
765 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000766 {
767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
768 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
769 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000770
Paul Bakker68884e32013-01-07 18:20:04 +0100771 /*
772 * Generate IV
773 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200774 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
775 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200776 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000778 memcpy( iv + transform->fixed_ivlen, rec->ctr,
779 explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200780 }
781 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
782 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200783 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200784 unsigned char i;
785
786 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
787
788 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000789 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790 }
791 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100792 {
793 /* Reminder if we ever add an AEAD mode with a different size */
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 );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100796 }
797
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100798 /*
799 * Build additional data for AEAD encryption.
800 * This depends on the TLS version.
801 */
802 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
803 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100804
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200805 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
806 iv, transform->ivlen );
807 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000808 data - explicit_iv_len, explicit_iv_len );
809 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100810 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200812 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000813 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000814
Paul Bakker68884e32013-01-07 18:20:04 +0100815 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200816 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200817 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000818
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200819 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000820 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100821 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000822 data, rec->data_len, /* source */
823 data, &rec->data_len, /* destination */
824 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200827 return( ret );
828 }
829
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100830 /*
831 * Prefix record content with explicit IV.
832 */
833 if( rec->data_offset < explicit_iv_len )
834 {
835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
836 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
837 }
838 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
839
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000840 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
841 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200842
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100843 /* Account for tag and explicit IV. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000844 rec->data_len += transform->taglen + explicit_iv_len;
845 rec->data_offset -= explicit_iv_len;
846 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100847 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000848 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
851#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000852 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000856 size_t padlen, i;
857 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000858
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000859 /* Currently we're always using minimal padding
860 * (up to 255 bytes would be allowed). */
861 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
862 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 padlen = 0;
864
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000865 /* Check there's enough space in the buffer for the padding. */
866 if( post_avail < padlen + 1 )
867 {
868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
869 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
870 }
871
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000873 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000875 rec->data_len += padlen + 1;
876 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000879 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
881 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000882 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000883 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000884 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000885 if( f_rng == NULL )
886 {
887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
889 }
890
891 if( rec->data_offset < transform->ivlen )
892 {
893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
894 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
895 }
896
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000897 /*
898 * Generate IV
899 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000900 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000901 if( ret != 0 )
902 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000903
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000904 memcpy( data - transform->ivlen, transform->iv_enc,
905 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000906
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000911 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000912 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200913 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000915 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
916 transform->iv_enc,
917 transform->ivlen,
918 data, rec->data_len,
919 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200922 return( ret );
923 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200924
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000925 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200929 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000932 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200933 {
934 /*
935 * Save IV in SSL3 and TLS1
936 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000937 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
938 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000940 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200941#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000942 {
943 data -= transform->ivlen;
944 rec->data_offset -= transform->ivlen;
945 rec->data_len += transform->ivlen;
946 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100949 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100950 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000951 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
952
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100953 /*
954 * MAC(MAC_write_key, seq_num +
955 * TLSCipherText.type +
956 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100957 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100958 * IV + // except for TLS 1.0
959 * ENC(content + padding + padding_length));
960 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000961
962 if( post_avail < transform->maclen)
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
965 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
966 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100967
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100968 ssl_extract_add_data_from_record( add_data, &add_data_len,
969 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000972 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100973 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100974
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000975 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100976 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000977 mbedtls_md_hmac_update( &transform->md_ctx_enc,
978 data, rec->data_len );
979 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
980 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100981
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000982 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100983
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000984 rec->data_len += transform->maclen;
985 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100986 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000992 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200996 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100998 /* Make extra sure authentication was performed, exactly once */
999 if( auth_done != 1 )
1000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001003 }
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
1007 return( 0 );
1008}
1009
Hanno Becker605949f2019-07-12 08:23:59 +01001010int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001011 mbedtls_ssl_transform *transform,
1012 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001014 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001016 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001017#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001018 size_t padlen = 0, correct = 1;
1019#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001020 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001021 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001022 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001023
Hanno Beckera18d1322018-01-03 14:27:32 +00001024#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001025 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001026 ((void) ssl);
1027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001030 if( rec == NULL ||
1031 rec->buf == NULL ||
1032 rec->buf_len < rec->data_offset ||
1033 rec->buf_len - rec->data_offset < rec->data_len )
1034 {
1035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001037 }
1038
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001039 data = rec->buf + rec->data_offset;
1040 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Hanno Beckera0e20d02019-05-15 14:03:01 +01001042#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001043 /*
1044 * Match record's CID with incoming CID.
1045 */
Hanno Becker938489a2019-05-08 13:02:22 +01001046 if( rec->cid_len != transform->in_cid_len ||
1047 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1048 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001049 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001050 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001051#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1054 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001055 {
1056 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001057 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1058 transform->iv_dec,
1059 transform->ivlen,
1060 data, rec->data_len,
1061 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001064 return( ret );
1065 }
1066
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001067 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
Paul Bakker68884e32013-01-07 18:20:04 +01001073 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001075#if defined(MBEDTLS_GCM_C) || \
1076 defined(MBEDTLS_CCM_C) || \
1077 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001079 mode == MBEDTLS_MODE_CCM ||
1080 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001081 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001082 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001083 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001084
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001085 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001086 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001087 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001088
1089 /* Check that there's enough space for the explicit IV
1090 * (at the beginning of the record) and the MAC (at the
1091 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001092 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001095 "+ taglen (%d)", rec->data_len,
1096 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001098 }
Paul Bakker68884e32013-01-07 18:20:04 +01001099
Hanno Beckerd96a6522019-07-10 13:55:25 +01001100#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001101 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1102 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001103 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01001104
Hanno Beckerd96a6522019-07-10 13:55:25 +01001105 /* Fixed */
1106 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1107 /* Explicit */
1108 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001109 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01001110 else
1111#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1112#if defined(MBEDTLS_CHACHAPOLY_C)
1113 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001114 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001115 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001116 unsigned char i;
1117
1118 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1119
1120 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001121 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001122 }
1123 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01001124#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001125 {
1126 /* Reminder if we ever add an AEAD mode with a different size */
1127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1129 }
1130
Hanno Beckerd96a6522019-07-10 13:55:25 +01001131 /* Group changes to data, data_len, and add_data, because
1132 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001133 data += explicit_iv_len;
1134 rec->data_offset += explicit_iv_len;
1135 rec->data_len -= explicit_iv_len + transform->taglen;
1136
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001137 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1138 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001139 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001140 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001141
Hanno Beckerd96a6522019-07-10 13:55:25 +01001142 /* Because of the check above, we know that there are
1143 * explicit_iv_len Bytes preceeding data, and taglen
1144 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001145 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001146 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001147
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001148 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001149 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001150 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001151
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001152 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001153 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001154 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001155 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1156 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001157 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001158 data, rec->data_len,
1159 data, &olen,
1160 data + rec->data_len,
1161 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1166 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001167
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001168 return( ret );
1169 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001170 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001171
Hanno Beckerd96a6522019-07-10 13:55:25 +01001172 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001173 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001177 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1181#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001182 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001185 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001186
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 /*
Paul Bakker45829992013-01-03 14:52:21 +01001188 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001191 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1192 {
1193 /* The ciphertext is prefixed with the CBC IV. */
1194 minlen += transform->ivlen;
1195 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001196#endif
Paul Bakker45829992013-01-03 14:52:21 +01001197
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001198 /* Size considerations:
1199 *
1200 * - The CBC cipher text must not be empty and hence
1201 * at least of size transform->ivlen.
1202 *
1203 * Together with the potential IV-prefix, this explains
1204 * the first of the two checks below.
1205 *
1206 * - The record must contain a MAC, either in plain or
1207 * encrypted, depending on whether Encrypt-then-MAC
1208 * is used or not.
1209 * - If it is, the message contains the IV-prefix,
1210 * the CBC ciphertext, and the MAC.
1211 * - If it is not, the padded plaintext, and hence
1212 * the CBC ciphertext, has at least length maclen + 1
1213 * because there is at least the padding length byte.
1214 *
1215 * As the CBC ciphertext is not empty, both cases give the
1216 * lower bound minlen + maclen + 1 on the record size, which
1217 * we test for in the second check below.
1218 */
1219 if( rec->data_len < minlen + transform->ivlen ||
1220 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001223 "+ 1 ) ( + expl IV )", rec->data_len,
1224 transform->ivlen,
1225 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001227 }
1228
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001229 /*
1230 * Authenticate before decrypt if enabled
1231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001233 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001234 {
Hanno Becker992b6872017-11-09 18:57:39 +00001235 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001238
Hanno Beckerd96a6522019-07-10 13:55:25 +01001239 /* Update data_len in tandem with add_data.
1240 *
1241 * The subtraction is safe because of the previous check
1242 * data_len >= minlen + maclen + 1.
1243 *
1244 * Afterwards, we know that data + data_len is followed by at
1245 * least maclen Bytes, which justifies the call to
1246 * mbedtls_ssl_safer_memcmp() below.
1247 *
1248 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001249 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001250 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1251 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001252
Hanno Beckerd96a6522019-07-10 13:55:25 +01001253 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001254 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1255 add_data_len );
1256 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1257 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001258 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1259 data, rec->data_len );
1260 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1261 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001262
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001263 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1264 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001265 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001266 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001267
Hanno Beckerd96a6522019-07-10 13:55:25 +01001268 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001269 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1270 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001274 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001275 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001276 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001278
1279 /*
1280 * Check length sanity
1281 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001282
1283 /* We know from above that data_len > minlen >= 0,
1284 * so the following check in particular implies that
1285 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001286 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001289 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001291 }
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001295 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001297 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001299 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001300 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001301
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001302 data += transform->ivlen;
1303 rec->data_offset += transform->ivlen;
1304 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307
Hanno Beckerd96a6522019-07-10 13:55:25 +01001308 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1309
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001310 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1311 transform->iv_dec, transform->ivlen,
1312 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001315 return( ret );
1316 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001317
Hanno Beckerd96a6522019-07-10 13:55:25 +01001318 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001319 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1322 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001323 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001326 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001327 {
1328 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001329 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1330 * records is equivalent to CBC decryption of the concatenation
1331 * of the records; in other words, IVs are maintained across
1332 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001333 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001334 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1335 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001339 /* Safe since data_len >= minlen + maclen + 1, so after having
1340 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001341 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1342 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001343 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001344
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001345 if( auth_done == 1 )
1346 {
1347 correct *= ( rec->data_len >= padlen + 1 );
1348 padlen *= ( rec->data_len >= padlen + 1 );
1349 }
1350 else
Paul Bakker45829992013-01-03 14:52:21 +01001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001353 if( rec->data_len < transform->maclen + padlen + 1 )
1354 {
1355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1356 rec->data_len,
1357 transform->maclen,
1358 padlen + 1 ) );
1359 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001360#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001361
1362 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
1363 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001366 padlen++;
1367
1368 /* Regardless of the validity of the padding,
1369 * we have data_len >= padlen here. */
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001372 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001374 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_SSL_DEBUG_ALL)
1377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001378 "should be no more than %d",
1379 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001380#endif
Paul Bakker45829992013-01-03 14:52:21 +01001381 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383 }
1384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1387 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001388 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001390 /* The padding check involves a series of up to 256
1391 * consecutive memory reads at the end of the record
1392 * plaintext buffer. In order to hide the length and
1393 * validity of the padding, always perform exactly
1394 * `min(256,plaintext_len)` reads (but take into account
1395 * only the last `padlen` bytes for the padding check). */
1396 size_t pad_count = 0;
1397 size_t real_count = 0;
1398 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001399
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001400 /* Index of first padding byte; it has been ensured above
1401 * that the subtraction is safe. */
1402 size_t const padding_idx = rec->data_len - padlen;
1403 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1404 size_t const start_idx = rec->data_len - num_checks;
1405 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001406
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001407 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001408 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001409 real_count |= ( idx >= padding_idx );
1410 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001411 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001412 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001415 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001417#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001418 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001420 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1422 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001426 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001427
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001428 /* If the padding was found to be invalid, padlen == 0
1429 * and the subtraction is safe. If the padding was found valid,
1430 * padlen hasn't been changed and the previous assertion
1431 * data_len >= padlen still holds. */
1432 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001436 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001444 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
1447 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001448 * Authenticate if not done yet.
1449 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 */
Hanno Becker52344c22018-01-03 15:24:20 +00001451#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001452 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001453 {
Hanno Becker992b6872017-11-09 18:57:39 +00001454 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001455
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001456 /* If the initial value of padlen was such that
1457 * data_len < maclen + padlen + 1, then padlen
1458 * got reset to 1, and the initial check
1459 * data_len >= minlen + maclen + 1
1460 * guarantees that at this point we still
1461 * have at least data_len >= maclen.
1462 *
1463 * If the initial value of padlen was such that
1464 * data_len >= maclen + padlen + 1, then we have
1465 * subtracted either padlen + 1 (if the padding was correct)
1466 * or 0 (if the padding was incorrect) since then,
1467 * hence data_len >= maclen in any case.
1468 */
1469 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001470 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1471 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001474 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001476 ssl_mac( &transform->md_ctx_dec,
1477 transform->mac_dec,
1478 data, rec->data_len,
1479 rec->ctr, rec->type,
1480 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001481 }
1482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1484#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1485 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001486 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001487 {
1488 /*
1489 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02001490 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001491 *
1492 * Known timing attacks:
1493 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1494 *
Gilles Peskine20b44082018-05-29 14:06:49 +02001495 * To compensate for different timings for the MAC calculation
1496 * depending on how much padding was removed (which is determined
1497 * by padlen), process extra_run more blocks through the hash
1498 * function.
1499 *
1500 * The formula in the paper is
1501 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1502 * where L1 is the size of the header plus the decrypted message
1503 * plus CBC padding and L2 is the size of the header plus the
1504 * decrypted message. This is for an underlying hash function
1505 * with 64-byte blocks.
1506 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1507 * correctly. We round down instead of up, so -56 is the correct
1508 * value for our calculations instead of -55.
1509 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02001510 * Repeat the formula rather than defining a block_size variable.
1511 * This avoids requiring division by a variable at runtime
1512 * (which would be marginally less efficient and would require
1513 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001514 */
1515 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001516 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001517
1518 /*
1519 * The next two sizes are the minimum and maximum values of
1520 * in_msglen over all padlen values.
1521 *
1522 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001523 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001524 *
1525 * Note that max_len + maclen is never more than the buffer
1526 * length, as we previously did in_msglen -= maclen too.
1527 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001528 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001529 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1530
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001531 memset( tmp, 0, sizeof( tmp ) );
1532
1533 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001534 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001535#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1536 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001537 case MBEDTLS_MD_MD5:
1538 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001539 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001540 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001541 extra_run =
1542 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1543 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001544 break;
1545#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001546#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001547 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001548 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001549 extra_run =
1550 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1551 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001552 break;
1553#endif
1554 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1557 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001558
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001559 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001560
Hanno Beckercab87e62019-04-29 13:52:53 +01001561 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1562 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001563 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1564 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001565 /* Make sure we access everything even when padlen > 0. This
1566 * makes the synchronisation requirements for just-in-time
1567 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001568 ssl_read_memory( data + rec->data_len, padlen );
1569 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001570
1571 /* Call mbedtls_md_process at least once due to cache attacks
1572 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001573 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001574 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001575
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001576 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001577
1578 /* Make sure we access all the memory that could contain the MAC,
1579 * before we check it in the next code block. This makes the
1580 * synchronisation requirements for just-in-time Prime+Probe
1581 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001582 ssl_read_memory( data + min_len,
1583 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1587 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001593#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001594 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1595 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001596#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001598 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1599 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_SSL_DEBUG_ALL)
1602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001603#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001604 correct = 0;
1605 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001606 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001607 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001608
1609 /*
1610 * Finally check the correct flag
1611 */
1612 if( correct == 0 )
1613 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001614#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001615
1616 /* Make extra sure authentication was performed, exactly once */
1617 if( auth_done != 1 )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Hanno Beckerccc13d02020-05-04 12:30:04 +01001623#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1624 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1625 {
1626 /* Remove inner padding and infer true content type. */
1627 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1628 &rec->type );
1629
1630 if( ret != 0 )
1631 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1632 }
1633#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1634
Hanno Beckera0e20d02019-05-15 14:03:01 +01001635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001636 if( rec->cid_len != 0 )
1637 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001638 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1639 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001640 if( ret != 0 )
1641 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1642 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001643#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
1647 return( 0 );
1648}
1649
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001650#undef MAC_NONE
1651#undef MAC_PLAINTEXT
1652#undef MAC_CIPHERTEXT
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001655/*
1656 * Compression/decompression functions
1657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001659{
Janos Follath865b3eb2019-12-16 11:46:15 +00001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001661 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001662 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001663 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001664 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001665#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1666 size_t out_buf_len = ssl->out_buf_len;
1667#else
1668 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1669#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001672
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001673 if( len_pre == 0 )
1674 return( 0 );
1675
Paul Bakker2770fbd2012-07-03 13:30:23 +00001676 memcpy( msg_pre, ssl->out_msg, len_pre );
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001679 ssl->out_msglen ) );
1680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001682 ssl->out_msg, ssl->out_msglen );
1683
Paul Bakker48916f92012-09-16 19:57:18 +00001684 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1685 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1686 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001687 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001688
Paul Bakker48916f92012-09-16 19:57:18 +00001689 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001690 if( ret != Z_OK )
1691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1693 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001694 }
1695
Darryl Greenb33cc762019-11-28 14:29:44 +00001696 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001697 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700 ssl->out_msglen ) );
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001703 ssl->out_msg, ssl->out_msglen );
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001706
1707 return( 0 );
1708}
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001711{
Janos Follath865b3eb2019-12-16 11:46:15 +00001712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001713 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001714 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001715 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001716 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001717#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1718 size_t in_buf_len = ssl->in_buf_len;
1719#else
1720 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1721#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001724
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001725 if( len_pre == 0 )
1726 return( 0 );
1727
Paul Bakker2770fbd2012-07-03 13:30:23 +00001728 memcpy( msg_pre, ssl->in_msg, len_pre );
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731 ssl->in_msglen ) );
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001734 ssl->in_msg, ssl->in_msglen );
1735
Paul Bakker48916f92012-09-16 19:57:18 +00001736 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1737 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1738 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001739 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740
Paul Bakker48916f92012-09-16 19:57:18 +00001741 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001742 if( ret != Z_OK )
1743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1745 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001746 }
1747
Darryl Greenb33cc762019-11-28 14:29:44 +00001748 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001749 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752 ssl->in_msglen ) );
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 ssl->in_msg, ssl->in_msglen );
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758
1759 return( 0 );
1760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762
Paul Bakker5121ce52009-01-03 21:22:43 +00001763/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001764 * Fill the input message buffer by appending data to it.
1765 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001766 *
1767 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1768 * available (from this read and/or a previous one). Otherwise, an error code
1769 * is returned (possibly EOF or WANT_READ).
1770 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001771 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1772 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1773 * since we always read a whole datagram at once.
1774 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001775 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001776 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779{
Janos Follath865b3eb2019-12-16 11:46:15 +00001780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001781 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001782#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1783 size_t in_buf_len = ssl->in_buf_len;
1784#else
1785 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1786#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001790 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001793 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001795 }
1796
Darryl Greenb33cc762019-11-28 14:29:44 +00001797 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001804 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001806 uint32_t timeout;
1807
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001808 /* Just to be sure */
1809 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1810 {
1811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1812 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1814 }
1815
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001816 /*
1817 * The point is, we need to always read a full datagram at once, so we
1818 * sometimes read more then requested, and handle the additional data.
1819 * It could be the rest of the current record (while fetching the
1820 * header) and/or some other records in the same datagram.
1821 */
1822
1823 /*
1824 * Move to the next record in the already read datagram if applicable
1825 */
1826 if( ssl->next_record_offset != 0 )
1827 {
1828 if( ssl->in_left < ssl->next_record_offset )
1829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001832 }
1833
1834 ssl->in_left -= ssl->next_record_offset;
1835
1836 if( ssl->in_left != 0 )
1837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001839 ssl->next_record_offset ) );
1840 memmove( ssl->in_hdr,
1841 ssl->in_hdr + ssl->next_record_offset,
1842 ssl->in_left );
1843 }
1844
1845 ssl->next_record_offset = 0;
1846 }
1847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001850
1851 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001852 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001853 */
1854 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001857 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001858 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001859
1860 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001861 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001862 * are not at the beginning of a new record, the caller did something
1863 * wrong.
1864 */
1865 if( ssl->in_left != 0 )
1866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001869 }
1870
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001871 /*
1872 * Don't even try to read if time's out already.
1873 * This avoids by-passing the timer when repeatedly receiving messages
1874 * that will end up being dropped.
1875 */
Hanno Becker7876d122020-02-05 10:39:31 +00001876 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001877 {
1878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001879 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001880 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001881 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001882 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001883 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001886 timeout = ssl->handshake->retransmit_timeout;
1887 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001888 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001891
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001892 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001893 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1894 timeout );
1895 else
1896 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001899
1900 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001902 }
1903
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001904 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001907 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001910 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001911 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001914 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001915 }
1916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001920 return( ret );
1921 }
1922
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001923 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001924 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001926 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001928 {
Hanno Becker786300f2020-02-05 10:46:40 +00001929 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001930 {
Hanno Becker786300f2020-02-05 10:46:40 +00001931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1932 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001933 return( ret );
1934 }
1935
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001936 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001939 }
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 if( ret < 0 )
1942 return( ret );
1943
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001944 ssl->in_left = ret;
1945 }
1946 else
1947#endif
1948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001950 ssl->in_left, nb_want ) );
1951
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001952 while( ssl->in_left < nb_want )
1953 {
1954 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001955
Hanno Becker7876d122020-02-05 10:39:31 +00001956 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001957 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1958 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001959 {
1960 if( ssl->f_recv_timeout != NULL )
1961 {
1962 ret = ssl->f_recv_timeout( ssl->p_bio,
1963 ssl->in_hdr + ssl->in_left, len,
1964 ssl->conf->read_timeout );
1965 }
1966 else
1967 {
1968 ret = ssl->f_recv( ssl->p_bio,
1969 ssl->in_hdr + ssl->in_left, len );
1970 }
1971 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001974 ssl->in_left, nb_want ) );
1975 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001976
1977 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001979
1980 if( ret < 0 )
1981 return( ret );
1982
mohammad160352aecb92018-03-28 23:41:40 -07001983 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001984 {
Darryl Green11999bb2018-03-13 15:22:58 +00001985 MBEDTLS_SSL_DEBUG_MSG( 1,
1986 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07001987 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1989 }
1990
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001991 ssl->in_left += ret;
1992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 }
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
1997 return( 0 );
1998}
1999
2000/*
2001 * Flush any data not yet written
2002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004{
Janos Follath865b3eb2019-12-16 11:46:15 +00002005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002006 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002010 if( ssl->f_send == NULL )
2011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002013 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002015 }
2016
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002017 /* Avoid incrementing counter if data is flushed */
2018 if( ssl->out_left == 0 )
2019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002021 return( 0 );
2022 }
2023
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 while( ssl->out_left > 0 )
2025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002027 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Hanno Becker2b1e3542018-08-06 11:19:13 +01002029 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002030 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 if( ret <= 0 )
2035 return( ret );
2036
mohammad160352aecb92018-03-28 23:41:40 -07002037 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002038 {
Darryl Green11999bb2018-03-13 15:22:58 +00002039 MBEDTLS_SSL_DEBUG_MSG( 1,
2040 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002041 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2043 }
2044
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 ssl->out_left -= ret;
2046 }
2047
Hanno Becker2b1e3542018-08-06 11:19:13 +01002048#if defined(MBEDTLS_SSL_PROTO_DTLS)
2049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002050 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002051 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002052 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002053 else
2054#endif
2055 {
2056 ssl->out_hdr = ssl->out_buf + 8;
2057 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002058 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
2062 return( 0 );
2063}
2064
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002065/*
2066 * Functions to handle the DTLS retransmission state machine
2067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002069/*
2070 * Append current handshake message to current outgoing flight
2071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2076 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2077 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002078
2079 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002080 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002081 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002085 }
2086
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002087 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002088 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002091 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002092 }
2093
2094 /* Copy current handshake message with headers */
2095 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2096 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002097 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002098 msg->next = NULL;
2099
2100 /* Append to the current flight */
2101 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002102 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002103 else
2104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002106 while( cur->next != NULL )
2107 cur = cur->next;
2108 cur->next = msg;
2109 }
2110
Hanno Becker3b235902018-08-06 09:54:53 +01002111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002112 return( 0 );
2113}
2114
2115/*
2116 * Free the current flight of handshake messages
2117 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002118void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_ssl_flight_item *cur = flight;
2121 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002122
2123 while( cur != NULL )
2124 {
2125 next = cur->next;
2126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 mbedtls_free( cur->p );
2128 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002129
2130 cur = next;
2131 }
2132}
2133
2134/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002135 * Swap transform_out and out_ctr with the alternative ones
2136 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002137static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002140 unsigned char tmp_out_ctr[8];
2141
2142 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002145 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002146 }
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002149
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002150 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002151 tmp_transform = ssl->transform_out;
2152 ssl->transform_out = ssl->handshake->alt_transform_out;
2153 ssl->handshake->alt_transform_out = tmp_transform;
2154
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002155 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002156 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2157 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002158 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002159
2160 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002161 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2164 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002165 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002166 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2167 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2170 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002171 }
2172 }
2173#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002174
2175 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002176}
2177
2178/*
2179 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002180 */
2181int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2182{
2183 int ret = 0;
2184
2185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2186
2187 ret = mbedtls_ssl_flight_transmit( ssl );
2188
2189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2190
2191 return( ret );
2192}
2193
2194/*
2195 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002196 *
2197 * Need to remember the current message in case flush_output returns
2198 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002200 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002201int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002202{
Janos Follath865b3eb2019-12-16 11:46:15 +00002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002209
2210 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002211 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002212 ret = ssl_swap_epochs( ssl );
2213 if( ret != 0 )
2214 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002217 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002218
2219 while( ssl->handshake->cur_msg != NULL )
2220 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002221 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002222 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002223
Hanno Beckere1dcb032018-08-17 16:47:58 +01002224 int const is_finished =
2225 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2226 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2227
Hanno Becker04da1892018-08-14 13:22:10 +01002228 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2229 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2230
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002231 /* Swap epochs before sending Finished: we can't do it after
2232 * sending ChangeCipherSpec, in case write returns WANT_READ.
2233 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002234 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002235 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002237 ret = ssl_swap_epochs( ssl );
2238 if( ret != 0 )
2239 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002240 }
2241
Hanno Becker67bc7c32018-08-06 11:33:50 +01002242 ret = ssl_get_remaining_payload_in_datagram( ssl );
2243 if( ret < 0 )
2244 return( ret );
2245 max_frag_len = (size_t) ret;
2246
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002247 /* CCS is copied as is, while HS messages may need fragmentation */
2248 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2249 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002250 if( max_frag_len == 0 )
2251 {
2252 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2253 return( ret );
2254
2255 continue;
2256 }
2257
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002258 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002259 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002260 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002261
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002262 /* Update position inside current message */
2263 ssl->handshake->cur_msg_p += cur->len;
2264 }
2265 else
2266 {
2267 const unsigned char * const p = ssl->handshake->cur_msg_p;
2268 const size_t hs_len = cur->len - 12;
2269 const size_t frag_off = p - ( cur->p + 12 );
2270 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002271 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002272
Hanno Beckere1dcb032018-08-17 16:47:58 +01002273 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002274 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002275 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002276 {
2277 ret = ssl_swap_epochs( ssl );
2278 if( ret != 0 )
2279 return( ret );
2280 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002281
Hanno Becker67bc7c32018-08-06 11:33:50 +01002282 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2283 return( ret );
2284
2285 continue;
2286 }
2287 max_hs_frag_len = max_frag_len - 12;
2288
2289 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2290 max_hs_frag_len : rem_len;
2291
2292 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002293 {
2294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002295 (unsigned) cur_hs_frag_len,
2296 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002297 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002298
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002299 /* Messages are stored with handshake headers as if not fragmented,
2300 * copy beginning of headers then fill fragmentation fields.
2301 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2302 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002303
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002304 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2305 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2306 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2307
Hanno Becker67bc7c32018-08-06 11:33:50 +01002308 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2309 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2310 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002311
2312 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2313
Hanno Becker3f7b9732018-08-28 09:53:25 +01002314 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002315 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2316 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002317 ssl->out_msgtype = cur->type;
2318
2319 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002320 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002321 }
2322
2323 /* If done with the current message move to the next one if any */
2324 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2325 {
2326 if( cur->next != NULL )
2327 {
2328 ssl->handshake->cur_msg = cur->next;
2329 ssl->handshake->cur_msg_p = cur->next->p + 12;
2330 }
2331 else
2332 {
2333 ssl->handshake->cur_msg = NULL;
2334 ssl->handshake->cur_msg_p = NULL;
2335 }
2336 }
2337
2338 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002339 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002342 return( ret );
2343 }
2344 }
2345
Hanno Becker67bc7c32018-08-06 11:33:50 +01002346 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2347 return( ret );
2348
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002349 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2351 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002352 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002355 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002356 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002357
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002359
2360 return( 0 );
2361}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002362
2363/*
2364 * To be called when the last message of an incoming flight is received.
2365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002367{
2368 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002369 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002370 ssl->handshake->flight = NULL;
2371 ssl->handshake->cur_msg = NULL;
2372
2373 /* The next incoming flight will start with this msg_seq */
2374 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2375
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002376 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002377 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002378
Hanno Becker0271f962018-08-16 13:23:47 +01002379 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002380 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002381
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002382 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002383 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2386 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002389 }
2390 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002392}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002393
2394/*
2395 * To be called when the last message of an outgoing flight is send.
2396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002398{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002399 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002400 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2403 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002406 }
2407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002411
Paul Bakker5121ce52009-01-03 21:22:43 +00002412/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002413 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002415
2416/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002417 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002418 *
2419 * - fill in handshake headers
2420 * - update handshake checksum
2421 * - DTLS: save message for resending
2422 * - then pass to the record layer
2423 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002424 * DTLS: except for HelloRequest, messages are only queued, and will only be
2425 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002426 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002427 * Inputs:
2428 * - ssl->out_msglen: 4 + actual handshake message len
2429 * (4 is the size of handshake headers for TLS)
2430 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2431 * - ssl->out_msg + 4: the handshake message body
2432 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002433 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002434 * - ssl->out_msglen: the length of the record contents
2435 * (including handshake headers but excluding record headers)
2436 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002437 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002438int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002439{
Janos Follath865b3eb2019-12-16 11:46:15 +00002440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002441 const size_t hs_len = ssl->out_msglen - 4;
2442 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2445
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002446 /*
2447 * Sanity checks
2448 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002449 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002450 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2451 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002452 /* In SSLv3, the client might send a NoCertificate alert. */
2453#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2454 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2455 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2456 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2457#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2458 {
2459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2460 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2461 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002462 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002464 /* Whenever we send anything different from a
2465 * HelloRequest we should be in a handshake - double check. */
2466 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2467 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002468 ssl->handshake == NULL )
2469 {
2470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002475 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002476 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002478 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002481 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002482#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002483
Hanno Beckerb50a2532018-08-06 11:52:54 +01002484 /* Double-check that we did not exceed the bounds
2485 * of the outgoing record buffer.
2486 * This should never fail as the various message
2487 * writing functions must obey the bounds of the
2488 * outgoing record buffer, but better be safe.
2489 *
2490 * Note: We deliberately do not check for the MTU or MFL here.
2491 */
2492 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2493 {
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2495 "size %u, maximum %u",
2496 (unsigned) ssl->out_msglen,
2497 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2499 }
2500
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002501 /*
2502 * Fill handshake headers
2503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002506 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2507 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2508 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002510 /*
2511 * DTLS has additional fields in the Handshake layer,
2512 * between the length field and the actual payload:
2513 * uint16 message_seq;
2514 * uint24 fragment_offset;
2515 * uint24 fragment_length;
2516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002519 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002520 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002521 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002522 {
2523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2524 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002525 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002526 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 }
2529
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002530 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002531 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002532
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002533 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002534 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002535 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002536 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2537 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2538 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002539 }
2540 else
2541 {
2542 ssl->out_msg[4] = 0;
2543 ssl->out_msg[5] = 0;
2544 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002545
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002546 /* Handshake hashes are computed without fragmentation,
2547 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002548 memset( ssl->out_msg + 6, 0x00, 3 );
2549 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002552
Hanno Becker0207e532018-08-28 10:28:28 +01002553 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002554 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2555 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 }
2557
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002558 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002561 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2562 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002563 {
2564 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002567 return( ret );
2568 }
2569 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002570 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002571#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002572 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002573 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002574 {
2575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2576 return( ret );
2577 }
2578 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002579
2580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2581
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002582 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002583}
2584
2585/*
2586 * Record layer functions
2587 */
2588
2589/*
2590 * Write current record.
2591 *
2592 * Uses:
2593 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2594 * - ssl->out_msglen: length of the record content (excl headers)
2595 * - ssl->out_msg: record content
2596 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002597int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002598{
2599 int ret, done = 0;
2600 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002601 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002602
2603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002606 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002608 {
2609 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002612 return( ret );
2613 }
2614
2615 len = ssl->out_msglen;
2616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2620 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 ret = mbedtls_ssl_hw_record_write( ssl );
2625 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2628 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002629 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002630
2631 if( ret == 0 )
2632 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002635 if( !done )
2636 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002637 unsigned i;
2638 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002639#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2640 size_t out_buf_len = ssl->out_buf_len;
2641#else
2642 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2643#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002644 /* Skip writing the record content type to after the encryption,
2645 * as it may change when using the CID extension. */
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002648 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002649
Hanno Becker19859472018-08-06 09:40:20 +01002650 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002651 ssl->out_len[0] = (unsigned char)( len >> 8 );
2652 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002653
Paul Bakker48916f92012-09-16 19:57:18 +00002654 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002655 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656 mbedtls_record rec;
2657
2658 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002659 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 rec.data_len = ssl->out_msglen;
2661 rec.data_offset = ssl->out_msg - rec.buf;
2662
2663 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2664 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2665 ssl->conf->transport, rec.ver );
2666 rec.type = ssl->out_msgtype;
2667
Hanno Beckera0e20d02019-05-15 14:03:01 +01002668#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002669 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002670 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002671#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002672
Hanno Beckera18d1322018-01-03 14:27:32 +00002673 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002674 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002677 return( ret );
2678 }
2679
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002680 if( rec.data_offset != 0 )
2681 {
2682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2684 }
2685
Hanno Becker6430faf2019-05-08 11:57:13 +01002686 /* Update the record content type and CID. */
2687 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002689 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002690#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002691 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002692 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2693 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002694 }
2695
Hanno Becker5903de42019-05-03 14:46:38 +01002696 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002697
2698#if defined(MBEDTLS_SSL_PROTO_DTLS)
2699 /* In case of DTLS, double-check that we don't exceed
2700 * the remaining space in the datagram. */
2701 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2702 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002703 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002704 if( ret < 0 )
2705 return( ret );
2706
2707 if( protected_record_size > (size_t) ret )
2708 {
2709 /* Should never happen */
2710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2711 }
2712 }
2713#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002714
Hanno Becker6430faf2019-05-08 11:57:13 +01002715 /* Now write the potentially updated record content type. */
2716 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002719 "version = [%d:%d], msglen = %d",
2720 ssl->out_hdr[0], ssl->out_hdr[1],
2721 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002724 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002725
2726 ssl->out_left += protected_record_size;
2727 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002728 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002729
Hanno Beckerdd772292020-02-05 10:38:31 +00002730 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002731 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2732 break;
2733
2734 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002735 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002736 {
2737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2738 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
Hanno Becker67bc7c32018-08-06 11:33:50 +01002742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2744 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002745 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002746 size_t remaining;
2747 ret = ssl_get_remaining_payload_in_datagram( ssl );
2748 if( ret < 0 )
2749 {
2750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2751 ret );
2752 return( ret );
2753 }
2754
2755 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002756 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002757 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002758 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002759 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002760 else
2761 {
Hanno Becker513815a2018-08-20 11:56:09 +01002762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002763 }
2764 }
2765#endif /* MBEDTLS_SSL_PROTO_DTLS */
2766
2767 if( ( flush == SSL_FORCE_FLUSH ) &&
2768 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 return( ret );
2772 }
2773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
2776 return( 0 );
2777}
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002780
2781static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2782{
2783 if( ssl->in_msglen < ssl->in_hslen ||
2784 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2785 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2786 {
2787 return( 1 );
2788 }
2789 return( 0 );
2790}
Hanno Becker44650b72018-08-16 12:51:11 +01002791
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002792static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002793{
2794 return( ( ssl->in_msg[9] << 16 ) |
2795 ( ssl->in_msg[10] << 8 ) |
2796 ssl->in_msg[11] );
2797}
2798
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002799static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002800{
2801 return( ( ssl->in_msg[6] << 16 ) |
2802 ( ssl->in_msg[7] << 8 ) |
2803 ssl->in_msg[8] );
2804}
2805
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002806static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002807{
2808 uint32_t msg_len, frag_off, frag_len;
2809
2810 msg_len = ssl_get_hs_total_len( ssl );
2811 frag_off = ssl_get_hs_frag_off( ssl );
2812 frag_len = ssl_get_hs_frag_len( ssl );
2813
2814 if( frag_off > msg_len )
2815 return( -1 );
2816
2817 if( frag_len > msg_len - frag_off )
2818 return( -1 );
2819
2820 if( frag_len + 12 > ssl->in_msglen )
2821 return( -1 );
2822
2823 return( 0 );
2824}
2825
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002826/*
2827 * Mark bits in bitmask (used for DTLS HS reassembly)
2828 */
2829static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2830{
2831 unsigned int start_bits, end_bits;
2832
2833 start_bits = 8 - ( offset % 8 );
2834 if( start_bits != 8 )
2835 {
2836 size_t first_byte_idx = offset / 8;
2837
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002838 /* Special case */
2839 if( len <= start_bits )
2840 {
2841 for( ; len != 0; len-- )
2842 mask[first_byte_idx] |= 1 << ( start_bits - len );
2843
2844 /* Avoid potential issues with offset or len becoming invalid */
2845 return;
2846 }
2847
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002848 offset += start_bits; /* Now offset % 8 == 0 */
2849 len -= start_bits;
2850
2851 for( ; start_bits != 0; start_bits-- )
2852 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2853 }
2854
2855 end_bits = len % 8;
2856 if( end_bits != 0 )
2857 {
2858 size_t last_byte_idx = ( offset + len ) / 8;
2859
2860 len -= end_bits; /* Now len % 8 == 0 */
2861
2862 for( ; end_bits != 0; end_bits-- )
2863 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2864 }
2865
2866 memset( mask + offset / 8, 0xFF, len / 8 );
2867}
2868
2869/*
2870 * Check that bitmask is full
2871 */
2872static int ssl_bitmask_check( unsigned char *mask, size_t len )
2873{
2874 size_t i;
2875
2876 for( i = 0; i < len / 8; i++ )
2877 if( mask[i] != 0xFF )
2878 return( -1 );
2879
2880 for( i = 0; i < len % 8; i++ )
2881 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2882 return( -1 );
2883
2884 return( 0 );
2885}
2886
Hanno Becker56e205e2018-08-16 09:06:12 +01002887/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002888static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002889 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002890{
Hanno Becker56e205e2018-08-16 09:06:12 +01002891 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002892
Hanno Becker56e205e2018-08-16 09:06:12 +01002893 alloc_len = 12; /* Handshake header */
2894 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002895
Hanno Beckerd07df862018-08-16 09:14:58 +01002896 if( add_bitmap )
2897 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002898
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002899 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002900}
Hanno Becker56e205e2018-08-16 09:06:12 +01002901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002903
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002904static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002905{
2906 return( ( ssl->in_msg[1] << 16 ) |
2907 ( ssl->in_msg[2] << 8 ) |
2908 ssl->in_msg[3] );
2909}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002910
Simon Butcher99000142016-10-13 17:21:01 +01002911int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002916 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002918 }
2919
Hanno Becker12555c62018-08-16 12:47:53 +01002920 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002923 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002924 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002927 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002928 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002930 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002931
Hanno Becker44650b72018-08-16 12:51:11 +01002932 if( ssl_check_hs_header( ssl ) != 0 )
2933 {
2934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2935 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2936 }
2937
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002938 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002939 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2940 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2941 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2942 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002943 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002944 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2945 {
2946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2947 recv_msg_seq,
2948 ssl->handshake->in_msg_seq ) );
2949 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2950 }
2951
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002952 /* Retransmit only on last message from previous flight, to avoid
2953 * too many retransmissions.
2954 * Besides, No sane server ever retransmits HelloVerifyRequest */
2955 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002959 "message_seq = %d, start_of_flight = %d",
2960 recv_msg_seq,
2961 ssl->handshake->in_flight_start_seq ) );
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002966 return( ret );
2967 }
2968 }
2969 else
2970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002972 "message_seq = %d, expected = %d",
2973 recv_msg_seq,
2974 ssl->handshake->in_msg_seq ) );
2975 }
2976
Hanno Becker90333da2017-10-10 11:27:13 +01002977 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002978 }
2979 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002980
Hanno Becker6d97ef52018-08-16 13:09:04 +01002981 /* Message reassembly is handled alongside buffering of future
2982 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002983 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002984 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002985 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002988 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002989 }
2990 }
2991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002993 /* With TLS we don't handle fragmentation (for now) */
2994 if( ssl->in_msglen < ssl->in_hslen )
2995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2997 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002998 }
2999
Simon Butcher99000142016-10-13 17:21:01 +01003000 return( 0 );
3001}
3002
3003void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3004{
Hanno Becker0271f962018-08-16 13:23:47 +01003005 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003006
Hanno Becker0271f962018-08-16 13:23:47 +01003007 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003008 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003009 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003010 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003011
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003012 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003015 ssl->handshake != NULL )
3016 {
Hanno Becker0271f962018-08-16 13:23:47 +01003017 unsigned offset;
3018 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003019
Hanno Becker0271f962018-08-16 13:23:47 +01003020 /* Increment handshake sequence number */
3021 hs->in_msg_seq++;
3022
3023 /*
3024 * Clear up handshake buffering and reassembly structure.
3025 */
3026
3027 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003028 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003029
3030 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003031 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3032 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003033 offset++, hs_buf++ )
3034 {
3035 *hs_buf = *(hs_buf + 1);
3036 }
3037
3038 /* Create a fresh last entry */
3039 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003040 }
3041#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003042}
3043
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003044/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003045 * DTLS anti-replay: RFC 6347 4.1.2.6
3046 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003047 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3048 * Bit n is set iff record number in_window_top - n has been seen.
3049 *
3050 * Usually, in_window_top is the last record number seen and the lsb of
3051 * in_window is set. The only exception is the initial state (record number 0
3052 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003055void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003056{
3057 ssl->in_window_top = 0;
3058 ssl->in_window = 0;
3059}
3060
3061static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3062{
3063 return( ( (uint64_t) buf[0] << 40 ) |
3064 ( (uint64_t) buf[1] << 32 ) |
3065 ( (uint64_t) buf[2] << 24 ) |
3066 ( (uint64_t) buf[3] << 16 ) |
3067 ( (uint64_t) buf[4] << 8 ) |
3068 ( (uint64_t) buf[5] ) );
3069}
3070
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003071static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3072{
Janos Follath865b3eb2019-12-16 11:46:15 +00003073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003074 unsigned char *original_in_ctr;
3075
3076 // save original in_ctr
3077 original_in_ctr = ssl->in_ctr;
3078
3079 // use counter from record
3080 ssl->in_ctr = record_in_ctr;
3081
3082 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3083
3084 // restore the counter
3085 ssl->in_ctr = original_in_ctr;
3086
3087 return ret;
3088}
3089
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003090/*
3091 * Return 0 if sequence number is acceptable, -1 otherwise
3092 */
Hanno Becker0183d692019-07-12 08:50:37 +01003093int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003094{
3095 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3096 uint64_t bit;
3097
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003098 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003099 return( 0 );
3100
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003101 if( rec_seqnum > ssl->in_window_top )
3102 return( 0 );
3103
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003104 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003105
3106 if( bit >= 64 )
3107 return( -1 );
3108
3109 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3110 return( -1 );
3111
3112 return( 0 );
3113}
3114
3115/*
3116 * Update replay window on new validated record
3117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003119{
3120 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3121
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003122 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003123 return;
3124
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003125 if( rec_seqnum > ssl->in_window_top )
3126 {
3127 /* Update window_top and the contents of the window */
3128 uint64_t shift = rec_seqnum - ssl->in_window_top;
3129
3130 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003131 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003132 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003133 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003134 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003135 ssl->in_window |= 1;
3136 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137
3138 ssl->in_window_top = rec_seqnum;
3139 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003140 else
3141 {
3142 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003143 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003144
3145 if( bit < 64 ) /* Always true, but be extra sure */
3146 ssl->in_window |= (uint64_t) 1 << bit;
3147 }
3148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003150
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003151#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003152/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003153 * Without any SSL context, check if a datagram looks like a ClientHello with
3154 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003155 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003156 *
3157 * - if cookie is valid, return 0
3158 * - if ClientHello looks superficially valid but cookie is not,
3159 * fill obuf and set olen, then
3160 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3161 * - otherwise return a specific error code
3162 */
3163static int ssl_check_dtls_clihlo_cookie(
3164 mbedtls_ssl_cookie_write_t *f_cookie_write,
3165 mbedtls_ssl_cookie_check_t *f_cookie_check,
3166 void *p_cookie,
3167 const unsigned char *cli_id, size_t cli_id_len,
3168 const unsigned char *in, size_t in_len,
3169 unsigned char *obuf, size_t buf_len, size_t *olen )
3170{
3171 size_t sid_len, cookie_len;
3172 unsigned char *p;
3173
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003174 /*
3175 * Structure of ClientHello with record and handshake headers,
3176 * and expected values. We don't need to check a lot, more checks will be
3177 * done when actually parsing the ClientHello - skipping those checks
3178 * avoids code duplication and does not make cookie forging any easier.
3179 *
3180 * 0-0 ContentType type; copied, must be handshake
3181 * 1-2 ProtocolVersion version; copied
3182 * 3-4 uint16 epoch; copied, must be 0
3183 * 5-10 uint48 sequence_number; copied
3184 * 11-12 uint16 length; (ignored)
3185 *
3186 * 13-13 HandshakeType msg_type; (ignored)
3187 * 14-16 uint24 length; (ignored)
3188 * 17-18 uint16 message_seq; copied
3189 * 19-21 uint24 fragment_offset; copied, must be 0
3190 * 22-24 uint24 fragment_length; (ignored)
3191 *
3192 * 25-26 ProtocolVersion client_version; (ignored)
3193 * 27-58 Random random; (ignored)
3194 * 59-xx SessionID session_id; 1 byte len + sid_len content
3195 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3196 * ...
3197 *
3198 * Minimum length is 61 bytes.
3199 */
3200 if( in_len < 61 ||
3201 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3202 in[3] != 0 || in[4] != 0 ||
3203 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3204 {
3205 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3206 }
3207
3208 sid_len = in[59];
3209 if( sid_len > in_len - 61 )
3210 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3211
3212 cookie_len = in[60 + sid_len];
3213 if( cookie_len > in_len - 60 )
3214 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3215
3216 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3217 cli_id, cli_id_len ) == 0 )
3218 {
3219 /* Valid cookie */
3220 return( 0 );
3221 }
3222
3223 /*
3224 * If we get here, we've got an invalid cookie, let's prepare HVR.
3225 *
3226 * 0-0 ContentType type; copied
3227 * 1-2 ProtocolVersion version; copied
3228 * 3-4 uint16 epoch; copied
3229 * 5-10 uint48 sequence_number; copied
3230 * 11-12 uint16 length; olen - 13
3231 *
3232 * 13-13 HandshakeType msg_type; hello_verify_request
3233 * 14-16 uint24 length; olen - 25
3234 * 17-18 uint16 message_seq; copied
3235 * 19-21 uint24 fragment_offset; copied
3236 * 22-24 uint24 fragment_length; olen - 25
3237 *
3238 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3239 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3240 *
3241 * Minimum length is 28.
3242 */
3243 if( buf_len < 28 )
3244 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3245
3246 /* Copy most fields and adapt others */
3247 memcpy( obuf, in, 25 );
3248 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3249 obuf[25] = 0xfe;
3250 obuf[26] = 0xff;
3251
3252 /* Generate and write actual cookie */
3253 p = obuf + 28;
3254 if( f_cookie_write( p_cookie,
3255 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3256 {
3257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3258 }
3259
3260 *olen = p - obuf;
3261
3262 /* Go back and fill length fields */
3263 obuf[27] = (unsigned char)( *olen - 28 );
3264
3265 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3266 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3267 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3268
3269 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3270 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3271
3272 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3273}
3274
3275/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003276 * Handle possible client reconnect with the same UDP quadruplet
3277 * (RFC 6347 Section 4.2.8).
3278 *
3279 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3280 * that looks like a ClientHello.
3281 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003282 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003283 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003284 * - if the input looks like a ClientHello with a valid cookie,
3285 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003286 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003287 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003288 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003289 * This function is called (through ssl_check_client_reconnect()) when an
3290 * unexpected record is found in ssl_get_next_record(), which will discard the
3291 * record if we return 0, and bubble up the return value otherwise (this
3292 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3293 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003294 */
3295static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3296{
Janos Follath865b3eb2019-12-16 11:46:15 +00003297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003298 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003299
Hanno Becker2fddd372019-07-10 14:37:41 +01003300 if( ssl->conf->f_cookie_write == NULL ||
3301 ssl->conf->f_cookie_check == NULL )
3302 {
3303 /* If we can't use cookies to verify reachability of the peer,
3304 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3306 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003307 return( 0 );
3308 }
3309
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003310 ret = ssl_check_dtls_clihlo_cookie(
3311 ssl->conf->f_cookie_write,
3312 ssl->conf->f_cookie_check,
3313 ssl->conf->p_cookie,
3314 ssl->cli_id, ssl->cli_id_len,
3315 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003316 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003317
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003318 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3319
3320 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003321 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003322 int send_ret;
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3324 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3325 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003326 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003327 * If the error is permanent we'll catch it later,
3328 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003329 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3330 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3331 (void) send_ret;
3332
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003333 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003334 }
3335
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003336 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003337 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003339 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003340 {
3341 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3342 return( ret );
3343 }
3344
3345 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003346 }
3347
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003348 return( ret );
3349}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003350#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003351
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003352static int ssl_check_record_type( uint8_t record_type )
3353{
3354 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3355 record_type != MBEDTLS_SSL_MSG_ALERT &&
3356 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3357 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3358 {
3359 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3360 }
3361
3362 return( 0 );
3363}
3364
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003365/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003366 * ContentType type;
3367 * ProtocolVersion version;
3368 * uint16 epoch; // DTLS only
3369 * uint48 sequence_number; // DTLS only
3370 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003371 *
3372 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003373 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003374 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3375 *
3376 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003377 * 1. proceed with the record if this function returns 0
3378 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3379 * 3. return CLIENT_RECONNECT if this function return that value
3380 * 4. drop the whole datagram if this function returns anything else.
3381 * Point 2 is needed when the peer is resending, and we have already received
3382 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003383 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003384static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003385 unsigned char *buf,
3386 size_t len,
3387 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003388{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003389 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
Hanno Beckere5e7e782019-07-11 12:29:35 +01003391 size_t const rec_hdr_type_offset = 0;
3392 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003393
Hanno Beckere5e7e782019-07-11 12:29:35 +01003394 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3395 rec_hdr_type_len;
3396 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Hanno Beckere5e7e782019-07-11 12:29:35 +01003398 size_t const rec_hdr_ctr_len = 8;
3399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003400 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003401 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3402 rec_hdr_version_len;
3403
Hanno Beckera0e20d02019-05-15 14:03:01 +01003404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003405 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3406 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003407 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003408#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3409#endif /* MBEDTLS_SSL_PROTO_DTLS */
3410
3411 size_t rec_hdr_len_offset; /* To be determined */
3412 size_t const rec_hdr_len_len = 2;
3413
3414 /*
3415 * Check minimum lengths for record header.
3416 */
3417
3418#if defined(MBEDTLS_SSL_PROTO_DTLS)
3419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3420 {
3421 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3422 }
3423 else
3424#endif /* MBEDTLS_SSL_PROTO_DTLS */
3425 {
3426 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3427 }
3428
3429 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3430 {
3431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3432 (unsigned) len,
3433 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3435 }
3436
3437 /*
3438 * Parse and validate record content type
3439 */
3440
3441 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003442
3443 /* Check record content type */
3444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3445 rec->cid_len = 0;
3446
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003447 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003448 ssl->conf->cid_len != 0 &&
3449 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003450 {
3451 /* Shift pointers to account for record header including CID
3452 * struct {
3453 * ContentType special_type = tls12_cid;
3454 * ProtocolVersion version;
3455 * uint16 epoch;
3456 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003457 * opaque cid[cid_length]; // Additional field compared to
3458 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003459 * uint16 length;
3460 * opaque enc_content[DTLSCiphertext.length];
3461 * } DTLSCiphertext;
3462 */
3463
3464 /* So far, we only support static CID lengths
3465 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003466 rec_hdr_cid_len = ssl->conf->cid_len;
3467 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003468
Hanno Beckere5e7e782019-07-11 12:29:35 +01003469 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003470 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3472 (unsigned) len,
3473 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003474 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003475 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003476
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003477 /* configured CID len is guaranteed at most 255, see
3478 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3479 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003480 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003481 }
3482 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003483#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003484 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003485 if( ssl_check_record_type( rec->type ) )
3486 {
Hanno Becker54229812019-07-12 14:40:00 +01003487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3488 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003489 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3490 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003491 }
3492
Hanno Beckere5e7e782019-07-11 12:29:35 +01003493 /*
3494 * Parse and validate record version
3495 */
3496
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003497 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3498 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003499 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3500 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003501 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003502
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003503 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 }
3508
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003509 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3512 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 }
3514
Hanno Beckere5e7e782019-07-11 12:29:35 +01003515 /*
3516 * Parse/Copy record sequence number.
3517 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003518
Hanno Beckere5e7e782019-07-11 12:29:35 +01003519#if defined(MBEDTLS_SSL_PROTO_DTLS)
3520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003521 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003522 /* Copy explicit record sequence number from input buffer. */
3523 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3524 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003525 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003526 else
3527#endif /* MBEDTLS_SSL_PROTO_DTLS */
3528 {
3529 /* Copy implicit record sequence number from SSL context structure. */
3530 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3531 }
Paul Bakker40e46942009-01-03 21:51:57 +00003532
Hanno Beckere5e7e782019-07-11 12:29:35 +01003533 /*
3534 * Parse record length.
3535 */
3536
Hanno Beckere5e7e782019-07-11 12:29:35 +01003537 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003538 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3539 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003540 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003543 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003544 rec->type,
3545 major_ver, minor_ver, rec->data_len ) );
3546
3547 rec->buf = buf;
3548 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003549
Hanno Beckerd417cc92019-07-26 08:20:27 +01003550 if( rec->data_len == 0 )
3551 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003553 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003554 * DTLS-related tests.
3555 * Check epoch before checking length constraint because
3556 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3557 * message gets duplicated before the corresponding Finished message,
3558 * the second ChangeCipherSpec should be discarded because it belongs
3559 * to an old epoch, but not because its length is shorter than
3560 * the minimum record length for packets using the new record transform.
3561 * Note that these two kinds of failures are handled differently,
3562 * as an unexpected record is silently skipped but an invalid
3563 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003564 */
3565#if defined(MBEDTLS_SSL_PROTO_DTLS)
3566 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3567 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003568 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003569
Hanno Becker955a5c92019-07-10 17:12:07 +01003570 /* Check that the datagram is large enough to contain a record
3571 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003572 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003573 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3575 (unsigned) len,
3576 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003577 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3578 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003579
Hanno Becker37cfe732019-07-10 17:20:01 +01003580 /* Records from other, non-matching epochs are silently discarded.
3581 * (The case of same-port Client reconnects must be considered in
3582 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003583 if( rec_epoch != ssl->in_epoch )
3584 {
3585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3586 "expected %d, received %d",
3587 ssl->in_epoch, rec_epoch ) );
3588
Hanno Becker552f7472019-07-19 10:59:12 +01003589 /* Records from the next epoch are considered for buffering
3590 * (concretely: early Finished messages). */
3591 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003592 {
Hanno Becker552f7472019-07-19 10:59:12 +01003593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3594 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003595 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003596
Hanno Becker2fddd372019-07-10 14:37:41 +01003597 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003598 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003599#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003600 /* For records from the correct epoch, check whether their
3601 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003602 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3603 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003604 {
3605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3606 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3607 }
3608#endif
3609 }
3610#endif /* MBEDTLS_SSL_PROTO_DTLS */
3611
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003612 return( 0 );
3613}
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Hanno Becker2fddd372019-07-10 14:37:41 +01003616#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3617static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3618{
3619 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3620
3621 /*
3622 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3623 * access the first byte of record content (handshake type), as we
3624 * have an active transform (possibly iv_len != 0), so use the
3625 * fact that the record header len is 13 instead.
3626 */
3627 if( rec_epoch == 0 &&
3628 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3629 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3630 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3631 ssl->in_left > 13 &&
3632 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3633 {
3634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3635 "from the same port" ) );
3636 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 }
3638
3639 return( 0 );
3640}
Hanno Becker2fddd372019-07-10 14:37:41 +01003641#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003643/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003644 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003645 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003646static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3647 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003648{
3649 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003652 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3655 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 ret = mbedtls_ssl_hw_record_read( ssl );
3660 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3663 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003664 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003665
3666 if( ret == 0 )
3667 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003670 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003672 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003673
Hanno Beckera18d1322018-01-03 14:27:32 +00003674 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003675 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003678
Hanno Beckera0e20d02019-05-15 14:03:01 +01003679#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003680 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3681 ssl->conf->ignore_unexpected_cid
3682 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3683 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003685 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003686 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003687#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003688
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 return( ret );
3690 }
3691
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003692 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003693 {
3694 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003695 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003696 }
3697
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003698 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003699 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003700
Hanno Beckera0e20d02019-05-15 14:03:01 +01003701#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003702 /* We have already checked the record content type
3703 * in ssl_parse_record_header(), failing or silently
3704 * dropping the record in the case of an unknown type.
3705 *
3706 * Since with the use of CIDs, the record content type
3707 * might change during decryption, re-check the record
3708 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003709 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003710 {
3711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3712 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3713 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003714#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003715
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003716 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003717 {
3718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3719 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003720 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003721 {
3722 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3724 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3725 }
3726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3727
3728 ssl->nb_zero++;
3729
3730 /*
3731 * Three or more empty messages may be a DoS attack
3732 * (excessive CPU consumption).
3733 */
3734 if( ssl->nb_zero > 3 )
3735 {
3736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003737 "messages, possible DoS attack" ) );
3738 /* Treat the records as if they were not properly authenticated,
3739 * thereby failing the connection if we see more than allowed
3740 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003741 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3742 }
3743 }
3744 else
3745 ssl->nb_zero = 0;
3746
3747#if defined(MBEDTLS_SSL_PROTO_DTLS)
3748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3749 {
3750 ; /* in_ctr read from peer, not maintained internally */
3751 }
3752 else
3753#endif
3754 {
3755 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003756 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003757 if( ++ssl->in_ctr[i - 1] != 0 )
3758 break;
3759
3760 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003761 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3764 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3765 }
3766 }
3767
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 }
3769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003774 }
3775#endif
3776
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003777 /* Check actual (decrypted) record content length against
3778 * configured maximum. */
3779 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3780 {
3781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3783 }
3784
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003785 return( 0 );
3786}
3787
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003788/*
3789 * Read a record.
3790 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003791 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3792 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3793 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003794 */
Hanno Becker1097b342018-08-15 14:09:41 +01003795
3796/* Helper functions for mbedtls_ssl_read_record(). */
3797static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003798static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3799static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003800
Hanno Becker327c93b2018-08-15 13:56:18 +01003801int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003802 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003803{
Janos Follath865b3eb2019-12-16 11:46:15 +00003804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003807
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003808 if( ssl->keep_current_message == 0 )
3809 {
3810 do {
Simon Butcher99000142016-10-13 17:21:01 +01003811
Hanno Becker26994592018-08-15 14:14:59 +01003812 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003813 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003814 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003815
Hanno Beckere74d5562018-08-15 14:26:08 +01003816 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003817 {
Hanno Becker40f50842018-08-15 14:48:01 +01003818#if defined(MBEDTLS_SSL_PROTO_DTLS)
3819 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003820
Hanno Becker40f50842018-08-15 14:48:01 +01003821 /* We only check for buffered messages if the
3822 * current datagram is fully consumed. */
3823 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003824 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003825 {
Hanno Becker40f50842018-08-15 14:48:01 +01003826 if( ssl_load_buffered_message( ssl ) == 0 )
3827 have_buffered = 1;
3828 }
3829
3830 if( have_buffered == 0 )
3831#endif /* MBEDTLS_SSL_PROTO_DTLS */
3832 {
3833 ret = ssl_get_next_record( ssl );
3834 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3835 continue;
3836
3837 if( ret != 0 )
3838 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003839 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003840 return( ret );
3841 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003842 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003843 }
3844
3845 ret = mbedtls_ssl_handle_message_type( ssl );
3846
Hanno Becker40f50842018-08-15 14:48:01 +01003847#if defined(MBEDTLS_SSL_PROTO_DTLS)
3848 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3849 {
3850 /* Buffer future message */
3851 ret = ssl_buffer_message( ssl );
3852 if( ret != 0 )
3853 return( ret );
3854
3855 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3856 }
3857#endif /* MBEDTLS_SSL_PROTO_DTLS */
3858
Hanno Becker90333da2017-10-10 11:27:13 +01003859 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3860 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003861
3862 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003863 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003864 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003865 return( ret );
3866 }
3867
Hanno Becker327c93b2018-08-15 13:56:18 +01003868 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003869 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003870 {
3871 mbedtls_ssl_update_handshake_status( ssl );
3872 }
Simon Butcher99000142016-10-13 17:21:01 +01003873 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003874 else
Simon Butcher99000142016-10-13 17:21:01 +01003875 {
Hanno Becker02f59072018-08-15 14:00:24 +01003876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003877 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003878 }
3879
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3881
3882 return( 0 );
3883}
3884
Hanno Becker40f50842018-08-15 14:48:01 +01003885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003886static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003887{
Hanno Becker40f50842018-08-15 14:48:01 +01003888 if( ssl->in_left > ssl->next_record_offset )
3889 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003890
Hanno Becker40f50842018-08-15 14:48:01 +01003891 return( 0 );
3892}
3893
3894static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3895{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003896 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003897 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003898 int ret = 0;
3899
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003900 if( hs == NULL )
3901 return( -1 );
3902
Hanno Beckere00ae372018-08-20 09:39:42 +01003903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3904
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003905 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3906 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3907 {
3908 /* Check if we have seen a ChangeCipherSpec before.
3909 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003910 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003911 {
3912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3913 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003914 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003915 }
3916
Hanno Becker39b8bc92018-08-28 17:17:13 +01003917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003918 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3919 ssl->in_msglen = 1;
3920 ssl->in_msg[0] = 1;
3921
3922 /* As long as they are equal, the exact value doesn't matter. */
3923 ssl->in_left = 0;
3924 ssl->next_record_offset = 0;
3925
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003926 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003927 goto exit;
3928 }
Hanno Becker37f95322018-08-16 13:55:32 +01003929
Hanno Beckerb8f50142018-08-28 10:01:34 +01003930#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003931 /* Debug only */
3932 {
3933 unsigned offset;
3934 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3935 {
3936 hs_buf = &hs->buffering.hs[offset];
3937 if( hs_buf->is_valid == 1 )
3938 {
3939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3940 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003941 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003942 }
3943 }
3944 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003945#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003946
3947 /* Check if we have buffered and/or fully reassembled the
3948 * next handshake message. */
3949 hs_buf = &hs->buffering.hs[0];
3950 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3951 {
3952 /* Synthesize a record containing the buffered HS message. */
3953 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3954 ( hs_buf->data[2] << 8 ) |
3955 hs_buf->data[3];
3956
3957 /* Double-check that we haven't accidentally buffered
3958 * a message that doesn't fit into the input buffer. */
3959 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3960 {
3961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3963 }
3964
3965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3966 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3967 hs_buf->data, msg_len + 12 );
3968
3969 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3970 ssl->in_hslen = msg_len + 12;
3971 ssl->in_msglen = msg_len + 12;
3972 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3973
3974 ret = 0;
3975 goto exit;
3976 }
3977 else
3978 {
3979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3980 hs->in_msg_seq ) );
3981 }
3982
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003983 ret = -1;
3984
3985exit:
3986
3987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3988 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003989}
3990
Hanno Beckera02b0b42018-08-21 17:20:27 +01003991static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3992 size_t desired )
3993{
3994 int offset;
3995 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3997 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003998
Hanno Becker01315ea2018-08-21 17:22:17 +01003999 /* Get rid of future records epoch first, if such exist. */
4000 ssl_free_buffered_record( ssl );
4001
4002 /* Check if we have enough space available now. */
4003 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4004 hs->buffering.total_bytes_buffered ) )
4005 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004007 return( 0 );
4008 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004009
Hanno Becker4f432ad2018-08-28 10:02:32 +01004010 /* We don't have enough space to buffer the next expected handshake
4011 * message. Remove buffers used for future messages to gain space,
4012 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004013 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4014 offset >= 0; offset-- )
4015 {
4016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4017 offset ) );
4018
Hanno Beckerb309b922018-08-23 13:18:05 +01004019 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004020
4021 /* Check if we have enough space available now. */
4022 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4023 hs->buffering.total_bytes_buffered ) )
4024 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004026 return( 0 );
4027 }
4028 }
4029
4030 return( -1 );
4031}
4032
Hanno Becker40f50842018-08-15 14:48:01 +01004033static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4034{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004035 int ret = 0;
4036 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4037
4038 if( hs == NULL )
4039 return( 0 );
4040
4041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4042
4043 switch( ssl->in_msgtype )
4044 {
4045 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004047
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004048 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004049 break;
4050
4051 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004052 {
4053 unsigned recv_msg_seq_offset;
4054 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4055 mbedtls_ssl_hs_buffer *hs_buf;
4056 size_t msg_len = ssl->in_hslen - 12;
4057
4058 /* We should never receive an old handshake
4059 * message - double-check nonetheless. */
4060 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4061 {
4062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4064 }
4065
4066 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4067 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4068 {
4069 /* Silently ignore -- message too far in the future */
4070 MBEDTLS_SSL_DEBUG_MSG( 2,
4071 ( "Ignore future HS message with sequence number %u, "
4072 "buffering window %u - %u",
4073 recv_msg_seq, ssl->handshake->in_msg_seq,
4074 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4075
4076 goto exit;
4077 }
4078
4079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4080 recv_msg_seq, recv_msg_seq_offset ) );
4081
4082 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4083
4084 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004085 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004086 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004087 size_t reassembly_buf_sz;
4088
Hanno Becker37f95322018-08-16 13:55:32 +01004089 hs_buf->is_fragmented =
4090 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4091
4092 /* We copy the message back into the input buffer
4093 * after reassembly, so check that it's not too large.
4094 * This is an implementation-specific limitation
4095 * and not one from the standard, hence it is not
4096 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004097 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004098 {
4099 /* Ignore message */
4100 goto exit;
4101 }
4102
Hanno Beckere0b150f2018-08-21 15:51:03 +01004103 /* Check if we have enough space to buffer the message. */
4104 if( hs->buffering.total_bytes_buffered >
4105 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4106 {
4107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4108 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4109 }
4110
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004111 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4112 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004113
4114 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4115 hs->buffering.total_bytes_buffered ) )
4116 {
4117 if( recv_msg_seq_offset > 0 )
4118 {
4119 /* If we can't buffer a future message because
4120 * of space limitations -- ignore. */
4121 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",
4122 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4123 (unsigned) hs->buffering.total_bytes_buffered ) );
4124 goto exit;
4125 }
Hanno Beckere1801392018-08-21 16:51:05 +01004126 else
4127 {
4128 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",
4129 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4130 (unsigned) hs->buffering.total_bytes_buffered ) );
4131 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004132
Hanno Beckera02b0b42018-08-21 17:20:27 +01004133 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004134 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004135 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",
4136 (unsigned) msg_len,
4137 (unsigned) reassembly_buf_sz,
4138 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004139 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004140 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4141 goto exit;
4142 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004143 }
4144
4145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4146 msg_len ) );
4147
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004148 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4149 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004150 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004151 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004152 goto exit;
4153 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004154 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004155
4156 /* Prepare final header: copy msg_type, length and message_seq,
4157 * then add standardised fragment_offset and fragment_length */
4158 memcpy( hs_buf->data, ssl->in_msg, 6 );
4159 memset( hs_buf->data + 6, 0, 3 );
4160 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4161
4162 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004163
4164 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004165 }
4166 else
4167 {
4168 /* Make sure msg_type and length are consistent */
4169 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4172 /* Ignore */
4173 goto exit;
4174 }
4175 }
4176
Hanno Becker4422bbb2018-08-20 09:40:19 +01004177 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004178 {
4179 size_t frag_len, frag_off;
4180 unsigned char * const msg = hs_buf->data + 12;
4181
4182 /*
4183 * Check and copy current fragment
4184 */
4185
4186 /* Validation of header fields already done in
4187 * mbedtls_ssl_prepare_handshake_record(). */
4188 frag_off = ssl_get_hs_frag_off( ssl );
4189 frag_len = ssl_get_hs_frag_len( ssl );
4190
4191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4192 frag_off, frag_len ) );
4193 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4194
4195 if( hs_buf->is_fragmented )
4196 {
4197 unsigned char * const bitmask = msg + msg_len;
4198 ssl_bitmask_set( bitmask, frag_off, frag_len );
4199 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4200 msg_len ) == 0 );
4201 }
4202 else
4203 {
4204 hs_buf->is_complete = 1;
4205 }
4206
4207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4208 hs_buf->is_complete ? "" : "not yet " ) );
4209 }
4210
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004211 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004212 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004213
4214 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004215 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004216 break;
4217 }
4218
4219exit:
4220
4221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4222 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004223}
4224#endif /* MBEDTLS_SSL_PROTO_DTLS */
4225
Hanno Becker1097b342018-08-15 14:09:41 +01004226static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004228 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004229 * Consume last content-layer message and potentially
4230 * update in_msglen which keeps track of the contents'
4231 * consumption state.
4232 *
4233 * (1) Handshake messages:
4234 * Remove last handshake message, move content
4235 * and adapt in_msglen.
4236 *
4237 * (2) Alert messages:
4238 * Consume whole record content, in_msglen = 0.
4239 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004240 * (3) Change cipher spec:
4241 * Consume whole record content, in_msglen = 0.
4242 *
4243 * (4) Application data:
4244 * Don't do anything - the record layer provides
4245 * the application data as a stream transport
4246 * and consumes through mbedtls_ssl_read only.
4247 *
4248 */
4249
4250 /* Case (1): Handshake messages */
4251 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004252 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004253 /* Hard assertion to be sure that no application data
4254 * is in flight, as corrupting ssl->in_msglen during
4255 * ssl->in_offt != NULL is fatal. */
4256 if( ssl->in_offt != NULL )
4257 {
4258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4260 }
4261
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004262 /*
4263 * Get next Handshake message in the current record
4264 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004265
Hanno Becker4a810fb2017-05-24 16:27:30 +01004266 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004267 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004268 * current handshake content: If DTLS handshake
4269 * fragmentation is used, that's the fragment
4270 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004271 * size here is faulty and should be changed at
4272 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004273 * (2) While it doesn't seem to cause problems, one
4274 * has to be very careful not to assume that in_hslen
4275 * is always <= in_msglen in a sensible communication.
4276 * Again, it's wrong for DTLS handshake fragmentation.
4277 * The following check is therefore mandatory, and
4278 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004279 * Additionally, ssl->in_hslen might be arbitrarily out of
4280 * bounds after handling a DTLS message with an unexpected
4281 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004282 */
4283 if( ssl->in_hslen < ssl->in_msglen )
4284 {
4285 ssl->in_msglen -= ssl->in_hslen;
4286 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4287 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004288
Hanno Becker4a810fb2017-05-24 16:27:30 +01004289 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4290 ssl->in_msg, ssl->in_msglen );
4291 }
4292 else
4293 {
4294 ssl->in_msglen = 0;
4295 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004296
Hanno Becker4a810fb2017-05-24 16:27:30 +01004297 ssl->in_hslen = 0;
4298 }
4299 /* Case (4): Application data */
4300 else if( ssl->in_offt != NULL )
4301 {
4302 return( 0 );
4303 }
4304 /* Everything else (CCS & Alerts) */
4305 else
4306 {
4307 ssl->in_msglen = 0;
4308 }
4309
Hanno Becker1097b342018-08-15 14:09:41 +01004310 return( 0 );
4311}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004312
Hanno Beckere74d5562018-08-15 14:26:08 +01004313static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4314{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004315 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004316 return( 1 );
4317
4318 return( 0 );
4319}
4320
Hanno Becker5f066e72018-08-16 14:56:31 +01004321#if defined(MBEDTLS_SSL_PROTO_DTLS)
4322
4323static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4324{
4325 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4326 if( hs == NULL )
4327 return;
4328
Hanno Becker01315ea2018-08-21 17:22:17 +01004329 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004330 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004331 hs->buffering.total_bytes_buffered -=
4332 hs->buffering.future_record.len;
4333
4334 mbedtls_free( hs->buffering.future_record.data );
4335 hs->buffering.future_record.data = NULL;
4336 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004337}
4338
4339static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4340{
4341 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4342 unsigned char * rec;
4343 size_t rec_len;
4344 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004345#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4346 size_t in_buf_len = ssl->in_buf_len;
4347#else
4348 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4349#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004350 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4351 return( 0 );
4352
4353 if( hs == NULL )
4354 return( 0 );
4355
Hanno Becker5f066e72018-08-16 14:56:31 +01004356 rec = hs->buffering.future_record.data;
4357 rec_len = hs->buffering.future_record.len;
4358 rec_epoch = hs->buffering.future_record.epoch;
4359
4360 if( rec == NULL )
4361 return( 0 );
4362
Hanno Becker4cb782d2018-08-20 11:19:05 +01004363 /* Only consider loading future records if the
4364 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004365 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004366 return( 0 );
4367
Hanno Becker5f066e72018-08-16 14:56:31 +01004368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4369
4370 if( rec_epoch != ssl->in_epoch )
4371 {
4372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4373 goto exit;
4374 }
4375
4376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4377
4378 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004379 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004380 {
4381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4383 }
4384
4385 memcpy( ssl->in_hdr, rec, rec_len );
4386 ssl->in_left = rec_len;
4387 ssl->next_record_offset = 0;
4388
4389 ssl_free_buffered_record( ssl );
4390
4391exit:
4392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4393 return( 0 );
4394}
4395
Hanno Becker519f15d2019-07-11 12:43:20 +01004396static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4397 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004398{
4399 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004400
4401 /* Don't buffer future records outside handshakes. */
4402 if( hs == NULL )
4403 return( 0 );
4404
4405 /* Only buffer handshake records (we are only interested
4406 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004407 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004408 return( 0 );
4409
4410 /* Don't buffer more than one future epoch record. */
4411 if( hs->buffering.future_record.data != NULL )
4412 return( 0 );
4413
Hanno Becker01315ea2018-08-21 17:22:17 +01004414 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004415 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004416 hs->buffering.total_bytes_buffered ) )
4417 {
4418 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 +01004419 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004420 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421 return( 0 );
4422 }
4423
Hanno Becker5f066e72018-08-16 14:56:31 +01004424 /* Buffer record */
4425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4426 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004427 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004428
4429 /* ssl_parse_record_header() only considers records
4430 * of the next epoch as candidates for buffering. */
4431 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004432 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004433
4434 hs->buffering.future_record.data =
4435 mbedtls_calloc( 1, hs->buffering.future_record.len );
4436 if( hs->buffering.future_record.data == NULL )
4437 {
4438 /* If we run out of RAM trying to buffer a
4439 * record from the next epoch, just ignore. */
4440 return( 0 );
4441 }
4442
Hanno Becker519f15d2019-07-11 12:43:20 +01004443 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004444
Hanno Becker519f15d2019-07-11 12:43:20 +01004445 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004446 return( 0 );
4447}
4448
4449#endif /* MBEDTLS_SSL_PROTO_DTLS */
4450
Hanno Beckere74d5562018-08-15 14:26:08 +01004451static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004452{
Janos Follath865b3eb2019-12-16 11:46:15 +00004453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004454 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004455
Hanno Becker5f066e72018-08-16 14:56:31 +01004456#if defined(MBEDTLS_SSL_PROTO_DTLS)
4457 /* We might have buffered a future record; if so,
4458 * and if the epoch matches now, load it.
4459 * On success, this call will set ssl->in_left to
4460 * the length of the buffered record, so that
4461 * the calls to ssl_fetch_input() below will
4462 * essentially be no-ops. */
4463 ret = ssl_load_buffered_record( ssl );
4464 if( ret != 0 )
4465 return( ret );
4466#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004467
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004468 /* Ensure that we have enough space available for the default form
4469 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4470 * with no space for CIDs counted in). */
4471 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4472 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004475 return( ret );
4476 }
4477
Hanno Beckere5e7e782019-07-11 12:29:35 +01004478 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4479 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004483 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004484 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4485 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004486 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004487 if( ret != 0 )
4488 return( ret );
4489
4490 /* Fall through to handling of unexpected records */
4491 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4492 }
4493
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004494 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4495 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004496#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004497 /* Reset in pointers to default state for TLS/DTLS records,
4498 * assuming no CID and no offset between record content and
4499 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004500 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004501
Hanno Becker7ae20e02019-07-12 08:33:49 +01004502 /* Setup internal message pointers from record structure. */
4503 ssl->in_msgtype = rec.type;
4504#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4505 ssl->in_len = ssl->in_cid + rec.cid_len;
4506#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4507 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4508 ssl->in_msglen = rec.data_len;
4509
Hanno Becker2fddd372019-07-10 14:37:41 +01004510 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004511 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004512 if( ret != 0 )
4513 return( ret );
4514#endif
4515
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004516 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004517 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004518
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4520 "(header)" ) );
4521 }
4522 else
4523 {
4524 /* Skip invalid record and the rest of the datagram */
4525 ssl->next_record_offset = 0;
4526 ssl->in_left = 0;
4527
4528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4529 "(header)" ) );
4530 }
4531
4532 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004533 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004534 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004535 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004536#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004537 {
4538 return( ret );
4539 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004540 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004544 {
Hanno Beckera8814792019-07-10 15:01:45 +01004545 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004546 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004547 if( ssl->next_record_offset < ssl->in_left )
4548 {
4549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4550 }
4551 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004552 else
4553#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004554 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004555 /*
4556 * Fetch record contents from underlying transport.
4557 */
Hanno Beckera3175662019-07-11 12:50:29 +01004558 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004559 if( ret != 0 )
4560 {
4561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4562 return( ret );
4563 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004564
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004565 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004566 }
4567
4568 /*
4569 * Decrypt record contents.
4570 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004571
Hanno Beckerfdf66042019-07-11 13:07:45 +01004572 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004576 {
4577 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004578 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004579 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004580 /* Except when waiting for Finished as a bad mac here
4581 * probably means something went wrong in the handshake
4582 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4583 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4584 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4585 {
4586#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4587 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4588 {
4589 mbedtls_ssl_send_alert_message( ssl,
4590 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4591 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4592 }
4593#endif
4594 return( ret );
4595 }
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004598 if( ssl->conf->badmac_limit != 0 &&
4599 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4602 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004603 }
4604#endif
4605
Hanno Becker4a810fb2017-05-24 16:27:30 +01004606 /* As above, invalid records cause
4607 * dismissal of the whole datagram. */
4608
4609 ssl->next_record_offset = 0;
4610 ssl->in_left = 0;
4611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004613 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004614 }
4615
4616 return( ret );
4617 }
4618 else
4619#endif
4620 {
4621 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4623 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 mbedtls_ssl_send_alert_message( ssl,
4626 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4627 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004628 }
4629#endif
4630 return( ret );
4631 }
4632 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633
Hanno Becker44d89b22019-07-12 09:40:44 +01004634
4635 /* Reset in pointers to default state for TLS/DTLS records,
4636 * assuming no CID and no offset between record content and
4637 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004638 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004639#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4640 ssl->in_len = ssl->in_cid + rec.cid_len;
4641#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004642 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004643
Hanno Becker8685c822019-07-12 09:37:30 +01004644 /* The record content type may change during decryption,
4645 * so re-read it. */
4646 ssl->in_msgtype = rec.type;
4647 /* Also update the input buffer, because unfortunately
4648 * the server-side ssl_parse_client_hello() reparses the
4649 * record header when receiving a ClientHello initiating
4650 * a renegotiation. */
4651 ssl->in_hdr[0] = rec.type;
4652 ssl->in_msg = rec.buf + rec.data_offset;
4653 ssl->in_msglen = rec.data_len;
4654 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4655 ssl->in_len[1] = (unsigned char)( rec.data_len );
4656
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004657#if defined(MBEDTLS_ZLIB_SUPPORT)
4658 if( ssl->transform_in != NULL &&
4659 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4660 {
4661 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4662 {
4663 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4664 return( ret );
4665 }
4666
4667 /* Check actual (decompress) record content length against
4668 * configured maximum. */
4669 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4670 {
4671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4672 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4673 }
4674 }
4675#endif /* MBEDTLS_ZLIB_SUPPORT */
4676
Simon Butcher99000142016-10-13 17:21:01 +01004677 return( 0 );
4678}
4679
4680int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4681{
Janos Follath865b3eb2019-12-16 11:46:15 +00004682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004683
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004684 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004685 * Handle particular types of records
4686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 {
Simon Butcher99000142016-10-13 17:21:01 +01004689 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4690 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004691 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 }
4694
Hanno Beckere678eaa2018-08-21 14:57:46 +01004695 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004696 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004697 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004698 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4700 ssl->in_msglen ) );
4701 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004702 }
4703
Hanno Beckere678eaa2018-08-21 14:57:46 +01004704 if( ssl->in_msg[0] != 1 )
4705 {
4706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4707 ssl->in_msg[0] ) );
4708 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4709 }
4710
4711#if defined(MBEDTLS_SSL_PROTO_DTLS)
4712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4713 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4714 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4715 {
4716 if( ssl->handshake == NULL )
4717 {
4718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4719 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4720 }
4721
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4723 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4724 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004725#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004726 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004730 if( ssl->in_msglen != 2 )
4731 {
4732 /* Note: Standard allows for more than one 2 byte alert
4733 to be packed in a single message, but Mbed TLS doesn't
4734 currently support this. */
4735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4736 ssl->in_msglen ) );
4737 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4738 }
4739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004741 ssl->in_msg[0], ssl->in_msg[1] ) );
4742
4743 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004744 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004749 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 }
4752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4754 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4757 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004759
4760#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4761 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4762 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4763 {
Hanno Becker90333da2017-10-10 11:27:13 +01004764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004765 /* Will be handled when trying to parse ServerHello */
4766 return( 0 );
4767 }
4768#endif
4769
4770#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4771 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4772 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4773 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4774 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4777 /* Will be handled in mbedtls_ssl_parse_certificate() */
4778 return( 0 );
4779 }
4780#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4781
4782 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004783 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 }
4785
Hanno Beckerc76c6192017-06-06 10:03:17 +01004786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004788 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004789 /* Drop unexpected ApplicationData records,
4790 * except at the beginning of renegotiations */
4791 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4792 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4793#if defined(MBEDTLS_SSL_RENEGOTIATION)
4794 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4795 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004796#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004797 )
4798 {
4799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4800 return( MBEDTLS_ERR_SSL_NON_FATAL );
4801 }
4802
4803 if( ssl->handshake != NULL &&
4804 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4805 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004806 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004807 }
4808 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004809#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004810
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 return( 0 );
4812}
4813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004815{
irwir6c0da642019-09-26 21:07:41 +03004816 return( mbedtls_ssl_send_alert_message( ssl,
4817 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4818 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004819}
4820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004822 unsigned char level,
4823 unsigned char message )
4824{
Janos Follath865b3eb2019-12-16 11:46:15 +00004825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004826
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004827 if( ssl == NULL || ssl->conf == NULL )
4828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004834 ssl->out_msglen = 2;
4835 ssl->out_msg[0] = level;
4836 ssl->out_msg[1] = message;
4837
Hanno Becker67bc7c32018-08-06 11:33:50 +01004838 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004841 return( ret );
4842 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004844
4845 return( 0 );
4846}
4847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849{
Janos Follath865b3eb2019-12-16 11:46:15 +00004850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 ssl->out_msglen = 1;
4856 ssl->out_msg[0] = 1;
4857
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 ssl->state++;
4859
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004860 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 return( ret );
4864 }
4865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004867
4868 return( 0 );
4869}
4870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872{
Janos Follath865b3eb2019-12-16 11:46:15 +00004873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004876
Hanno Becker327c93b2018-08-15 13:56:18 +01004877 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 return( ret );
4881 }
4882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004886 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4887 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 }
4890
Hanno Beckere678eaa2018-08-21 14:57:46 +01004891 /* CCS records are only accepted if they have length 1 and content '1',
4892 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004893
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004894 /*
4895 * Switch to our negotiated transform and session parameters for inbound
4896 * data.
4897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004899 ssl->transform_in = ssl->transform_negotiate;
4900 ssl->session_in = ssl->session_negotiate;
4901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004903 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004906 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004907#endif
4908
4909 /* Increment epoch */
4910 if( ++ssl->in_epoch == 0 )
4911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004913 /* This is highly unlikely to happen for legitimate reasons, so
4914 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004916 }
4917 }
4918 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004920 memset( ssl->in_ctr, 0, 8 );
4921
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004922 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4925 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4931 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004933 }
4934 }
4935#endif
4936
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 ssl->state++;
4938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004940
4941 return( 0 );
4942}
4943
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004944/* Once ssl->out_hdr as the address of the beginning of the
4945 * next outgoing record is set, deduce the other pointers.
4946 *
4947 * Note: For TLS, we save the implicit record sequence number
4948 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4949 * and the caller has to make sure there's space for this.
4950 */
4951
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004952void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4953 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004954{
4955#if defined(MBEDTLS_SSL_PROTO_DTLS)
4956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4957 {
4958 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004959#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004960 ssl->out_cid = ssl->out_ctr + 8;
4961 ssl->out_len = ssl->out_cid;
4962 if( transform != NULL )
4963 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004964#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004965 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004966#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004967 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004968 }
4969 else
4970#endif
4971 {
4972 ssl->out_ctr = ssl->out_hdr - 8;
4973 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004974#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004975 ssl->out_cid = ssl->out_len;
4976#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004977 ssl->out_iv = ssl->out_hdr + 5;
4978 }
4979
4980 /* Adjust out_msg to make space for explicit IV, if used. */
4981 if( transform != NULL &&
4982 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
4983 {
4984 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
4985 }
4986 else
4987 ssl->out_msg = ssl->out_iv;
4988}
4989
4990/* Once ssl->in_hdr as the address of the beginning of the
4991 * next incoming record is set, deduce the other pointers.
4992 *
4993 * Note: For TLS, we save the implicit record sequence number
4994 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4995 * and the caller has to make sure there's space for this.
4996 */
4997
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004998void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004999{
Hanno Becker79594fd2019-05-08 09:38:41 +01005000 /* This function sets the pointers to match the case
5001 * of unprotected TLS/DTLS records, with both ssl->in_iv
5002 * and ssl->in_msg pointing to the beginning of the record
5003 * content.
5004 *
5005 * When decrypting a protected record, ssl->in_msg
5006 * will be shifted to point to the beginning of the
5007 * record plaintext.
5008 */
5009
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005010#if defined(MBEDTLS_SSL_PROTO_DTLS)
5011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5012 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005013 /* This sets the header pointers to match records
5014 * without CID. When we receive a record containing
5015 * a CID, the fields are shifted accordingly in
5016 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005017 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005018#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005019 ssl->in_cid = ssl->in_ctr + 8;
5020 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005021#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005022 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005023#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005024 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005025 }
5026 else
5027#endif
5028 {
5029 ssl->in_ctr = ssl->in_hdr - 8;
5030 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005031#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005032 ssl->in_cid = ssl->in_len;
5033#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005034 ssl->in_iv = ssl->in_hdr + 5;
5035 }
5036
Hanno Becker79594fd2019-05-08 09:38:41 +01005037 /* This will be adjusted at record decryption time. */
5038 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005039}
5040
Paul Bakker5121ce52009-01-03 21:22:43 +00005041/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005042 * Setup an SSL context
5043 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005044
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005045void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005046{
5047 /* Set the incoming and outgoing record pointers. */
5048#if defined(MBEDTLS_SSL_PROTO_DTLS)
5049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5050 {
5051 ssl->out_hdr = ssl->out_buf;
5052 ssl->in_hdr = ssl->in_buf;
5053 }
5054 else
5055#endif /* MBEDTLS_SSL_PROTO_DTLS */
5056 {
5057 ssl->out_hdr = ssl->out_buf + 8;
5058 ssl->in_hdr = ssl->in_buf + 8;
5059 }
5060
5061 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005062 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5063 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005064}
5065
Paul Bakker5121ce52009-01-03 21:22:43 +00005066/*
5067 * SSL get accessors
5068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005070{
5071 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5072}
5073
Hanno Becker8b170a02017-10-10 11:51:19 +01005074int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5075{
5076 /*
5077 * Case A: We're currently holding back
5078 * a message for further processing.
5079 */
5080
5081 if( ssl->keep_current_message == 1 )
5082 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005084 return( 1 );
5085 }
5086
5087 /*
5088 * Case B: Further records are pending in the current datagram.
5089 */
5090
5091#if defined(MBEDTLS_SSL_PROTO_DTLS)
5092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5093 ssl->in_left > ssl->next_record_offset )
5094 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005095 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005096 return( 1 );
5097 }
5098#endif /* MBEDTLS_SSL_PROTO_DTLS */
5099
5100 /*
5101 * Case C: A handshake message is being processed.
5102 */
5103
Hanno Becker8b170a02017-10-10 11:51:19 +01005104 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5105 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005107 return( 1 );
5108 }
5109
5110 /*
5111 * Case D: An application data message is being processed
5112 */
5113 if( ssl->in_offt != NULL )
5114 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005116 return( 1 );
5117 }
5118
5119 /*
5120 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005121 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005122 * we implement support for multiple alerts in single records.
5123 */
5124
5125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5126 return( 0 );
5127}
5128
Paul Bakker43ca69c2011-01-15 17:35:19 +00005129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005131{
Hanno Becker3136ede2018-08-17 15:28:19 +01005132 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005134 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005135
Hanno Becker5903de42019-05-03 14:46:38 +01005136 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5137
Hanno Becker78640902018-08-13 16:35:15 +01005138 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005139 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141#if defined(MBEDTLS_ZLIB_SUPPORT)
5142 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5143 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005144#endif
5145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 case MBEDTLS_MODE_GCM:
5149 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005150 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005152 transform_expansion = transform->minlen;
5153 break;
5154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005156
5157 block_size = mbedtls_cipher_get_block_size(
5158 &transform->cipher_ctx_enc );
5159
Hanno Becker3136ede2018-08-17 15:28:19 +01005160 /* Expansion due to the addition of the MAC. */
5161 transform_expansion += transform->maclen;
5162
5163 /* Expansion due to the addition of CBC padding;
5164 * Theoretically up to 256 bytes, but we never use
5165 * more than the block size of the underlying cipher. */
5166 transform_expansion += block_size;
5167
5168 /* For TLS 1.1 or higher, an explicit IV is added
5169 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005170#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5171 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005172 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005173#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005174
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005175 break;
5176
5177 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005180 }
5181
Hanno Beckera0e20d02019-05-15 14:03:01 +01005182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005183 if( transform->out_cid_len != 0 )
5184 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005185#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005186
Hanno Becker5903de42019-05-03 14:46:38 +01005187 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005188}
5189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005191/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005192 * Check record counters and renegotiate if they're above the limit.
5193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005195{
Hanno Beckerdd772292020-02-05 10:38:31 +00005196 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005197 int in_ctr_cmp;
5198 int out_ctr_cmp;
5199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5201 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005202 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005203 {
5204 return( 0 );
5205 }
5206
Andres AG2196c7f2016-12-15 17:01:16 +00005207 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5208 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005209 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005210 ssl->conf->renego_period + ep_len, 8 - ep_len );
5211
5212 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005213 {
5214 return( 0 );
5215 }
5216
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005221
5222/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 * Receive application data decrypted from the SSL layer
5224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005226{
Janos Follath865b3eb2019-12-16 11:46:15 +00005227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005228 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005230 if( ssl == NULL || ssl->conf == NULL )
5231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005239 return( ret );
5240
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005241 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005243 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005244 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005245 return( ret );
5246 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005247 }
5248#endif
5249
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250 /*
5251 * Check if renegotiation is necessary and/or handshake is
5252 * in process. If yes, perform/continue, and fall through
5253 * if an unexpected packet is received while the client
5254 * is waiting for the ServerHello.
5255 *
5256 * (There is no equivalent to the last condition on
5257 * the server-side as it is not treated as within
5258 * a handshake while waiting for the ClientHello
5259 * after a renegotiation request.)
5260 */
5261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005263 ret = ssl_check_ctr_renegotiate( ssl );
5264 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5265 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005268 return( ret );
5269 }
5270#endif
5271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005275 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5276 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 return( ret );
5280 }
5281 }
5282
Hanno Beckere41158b2017-10-23 13:30:32 +01005283 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005284 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005286 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005287 if( ssl->f_get_timer != NULL &&
5288 ssl->f_get_timer( ssl->p_timer ) == -1 )
5289 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005290 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005291 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005292
Hanno Becker327c93b2018-08-15 13:56:18 +01005293 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005294 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005295 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5296 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005297
Hanno Becker4a810fb2017-05-24 16:27:30 +01005298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5299 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 }
5301
5302 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 {
5305 /*
5306 * OpenSSL sends empty messages to randomize the IV
5307 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005308 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005311 return( 0 );
5312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005314 return( ret );
5315 }
5316 }
5317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005321
Hanno Becker4a810fb2017-05-24 16:27:30 +01005322 /*
5323 * - For client-side, expect SERVER_HELLO_REQUEST.
5324 * - For server-side, expect CLIENT_HELLO.
5325 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5326 */
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005329 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005331 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005334
5335 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005338 {
5339 continue;
5340 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005343 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005344#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005345
Hanno Becker4a810fb2017-05-24 16:27:30 +01005346#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005347 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005351
5352 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005355 {
5356 continue;
5357 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005360 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005361#endif /* MBEDTLS_SSL_SRV_C */
5362
Hanno Becker21df7f92017-10-17 11:03:26 +01005363#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005364 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005365 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5366 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5367 ssl->conf->allow_legacy_renegotiation ==
5368 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5369 {
5370 /*
5371 * Accept renegotiation request
5372 */
Paul Bakker48916f92012-09-16 19:57:18 +00005373
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005374 /* DTLS clients need to know renego is server-initiated */
5375#if defined(MBEDTLS_SSL_PROTO_DTLS)
5376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5377 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5378 {
5379 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5380 }
5381#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005382 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005383 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5384 ret != 0 )
5385 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5387 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005388 return( ret );
5389 }
5390 }
5391 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005392#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005393 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005394 /*
5395 * Refuse renegotiation
5396 */
5397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400#if defined(MBEDTLS_SSL_PROTO_SSL3)
5401 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005402 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005403 /* SSLv3 does not have a "no_renegotiation" warning, so
5404 we send a fatal alert and abort the connection. */
5405 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5406 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5407 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005408 }
5409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5411#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5412 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5413 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5416 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5417 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005418 {
5419 return( ret );
5420 }
Paul Bakker48916f92012-09-16 19:57:18 +00005421 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5424 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005428 }
Paul Bakker48916f92012-09-16 19:57:18 +00005429 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005430
Hanno Becker90333da2017-10-10 11:27:13 +01005431 /* At this point, we don't know whether the renegotiation has been
5432 * completed or not. The cases to consider are the following:
5433 * 1) The renegotiation is complete. In this case, no new record
5434 * has been read yet.
5435 * 2) The renegotiation is incomplete because the client received
5436 * an application data record while awaiting the ServerHello.
5437 * 3) The renegotiation is incomplete because the client received
5438 * a non-handshake, non-application data message while awaiting
5439 * the ServerHello.
5440 * In each of these case, looping will be the proper action:
5441 * - For 1), the next iteration will read a new record and check
5442 * if it's application data.
5443 * - For 2), the loop condition isn't satisfied as application data
5444 * is present, hence continue is the same as break
5445 * - For 3), the loop condition is satisfied and read_record
5446 * will re-deliver the message that was held back by the client
5447 * when expecting the ServerHello.
5448 */
5449 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005450 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005453 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005454 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005455 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005456 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005459 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005461 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005462 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5467 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005470 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005471 }
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5476 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 }
5478
5479 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005480
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005481 /* We're going to return something now, cancel timer,
5482 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005484 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005485
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005487 /* If we requested renego but received AppData, resend HelloRequest.
5488 * Do it now, after setting in_offt, to avoid taking this branch
5489 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005491 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005493 {
Hanno Becker786300f2020-02-05 10:46:40 +00005494 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005495 {
Hanno Becker786300f2020-02-05 10:46:40 +00005496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5497 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005498 return( ret );
5499 }
5500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 }
5504
5505 n = ( len < ssl->in_msglen )
5506 ? len : ssl->in_msglen;
5507
5508 memcpy( buf, ssl->in_offt, n );
5509 ssl->in_msglen -= n;
5510
5511 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005512 {
5513 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005515 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005518 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 /* more data available */
5520 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005524
Paul Bakker23986e52011-04-24 08:57:21 +00005525 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005526}
5527
5528/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005529 * Send application data to be encrypted by the SSL layer, taking care of max
5530 * fragment length and buffer size.
5531 *
5532 * According to RFC 5246 Section 6.2.1:
5533 *
5534 * Zero-length fragments of Application data MAY be sent as they are
5535 * potentially useful as a traffic analysis countermeasure.
5536 *
5537 * Therefore, it is possible that the input message length is 0 and the
5538 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005540static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005541 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005542{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005543 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5544 const size_t max_len = (size_t) ret;
5545
5546 if( ret < 0 )
5547 {
5548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5549 return( ret );
5550 }
5551
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005552 if( len > max_len )
5553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005558 "maximum fragment length: %d > %d",
5559 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005561 }
5562 else
5563#endif
5564 len = max_len;
5565 }
Paul Bakker887bd502011-06-08 13:10:54 +00005566
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 if( ssl->out_left != 0 )
5568 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005569 /*
5570 * The user has previously tried to send the data and
5571 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5572 * written. In this case, we expect the high-level write function
5573 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005578 return( ret );
5579 }
5580 }
Paul Bakker887bd502011-06-08 13:10:54 +00005581 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005582 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005583 /*
5584 * The user is trying to send a message the first time, so we need to
5585 * copy the data into the internal buffers and setup the data structure
5586 * to keep track of partial writes
5587 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005588 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005590 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005591
Hanno Becker67bc7c32018-08-06 11:33:50 +01005592 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005595 return( ret );
5596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005597 }
5598
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005599 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005600}
5601
5602/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005603 * Write application data, doing 1/n-1 splitting if necessary.
5604 *
5605 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005606 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005607 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005610static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005611 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005612{
Janos Follath865b3eb2019-12-16 11:46:15 +00005613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005614
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005615 if( ssl->conf->cbc_record_splitting ==
5616 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005617 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5619 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5620 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005621 {
5622 return( ssl_write_real( ssl, buf, len ) );
5623 }
5624
5625 if( ssl->split_done == 0 )
5626 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005627 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005628 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005629 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005630 }
5631
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005632 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5633 return( ret );
5634 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005635
5636 return( ret + 1 );
5637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005639
5640/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005641 * Write application data (public-facing wrapper)
5642 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005643int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005644{
Janos Follath865b3eb2019-12-16 11:46:15 +00005645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005646
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005649 if( ssl == NULL || ssl->conf == NULL )
5650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5651
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005652#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005653 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5654 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005655 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005656 return( ret );
5657 }
5658#endif
5659
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005660 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005661 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005662 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005663 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005665 return( ret );
5666 }
5667 }
5668
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005669#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005670 ret = ssl_write_split( ssl, buf, len );
5671#else
5672 ret = ssl_write_real( ssl, buf, len );
5673#endif
5674
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005676
5677 return( ret );
5678}
5679
5680/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 * Notify the peer that the connection is being closed
5682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005684{
Janos Follath865b3eb2019-12-16 11:46:15 +00005685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005686
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005687 if( ssl == NULL || ssl->conf == NULL )
5688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005691
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005692 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5698 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5699 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 return( ret );
5703 }
5704 }
5705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005708 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709}
5710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005712{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005713 if( transform == NULL )
5714 return;
5715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005717 deflateEnd( &transform->ctx_deflate );
5718 inflateEnd( &transform->ctx_inflate );
5719#endif
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5722 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005723
Hanno Beckerd56ed242018-01-03 15:32:51 +00005724#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 mbedtls_md_free( &transform->md_ctx_enc );
5726 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005727#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005728
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005729 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005730}
5731
Hanno Becker0271f962018-08-16 13:23:47 +01005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
5733
Hanno Becker533ab5f2020-02-05 10:49:13 +00005734void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005735{
5736 unsigned offset;
5737 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5738
5739 if( hs == NULL )
5740 return;
5741
Hanno Becker283f5ef2018-08-24 09:34:47 +01005742 ssl_free_buffered_record( ssl );
5743
Hanno Becker0271f962018-08-16 13:23:47 +01005744 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005745 ssl_buffering_free_slot( ssl, offset );
5746}
5747
5748static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5749 uint8_t slot )
5750{
5751 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5752 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005753
5754 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5755 return;
5756
Hanno Beckere605b192018-08-21 15:59:07 +01005757 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005758 {
Hanno Beckere605b192018-08-21 15:59:07 +01005759 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005760 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005761 mbedtls_free( hs_buf->data );
5762 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005763 }
5764}
5765
5766#endif /* MBEDTLS_SSL_PROTO_DTLS */
5767
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005768/*
5769 * Convert version numbers to/from wire format
5770 * and, for DTLS, to/from TLS equivalent.
5771 *
5772 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005773 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005774 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5775 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005778 unsigned char ver[2] )
5779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_SSL_PROTO_DTLS)
5781 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005784 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5785
5786 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5787 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5788 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005789 else
5790#else
5791 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005792#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005793 {
5794 ver[0] = (unsigned char) major;
5795 ver[1] = (unsigned char) minor;
5796 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005797}
5798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005800 const unsigned char ver[2] )
5801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802#if defined(MBEDTLS_SSL_PROTO_DTLS)
5803 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005804 {
5805 *major = 255 - ver[0] + 2;
5806 *minor = 255 - ver[1] + 1;
5807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005809 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5810 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005811 else
5812#else
5813 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005814#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005815 {
5816 *major = ver[0];
5817 *minor = ver[1];
5818 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005819}
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#endif /* MBEDTLS_SSL_TLS_C */