blob: bed3e52d1f7ccc3dfef6ee3ca0075212a6093516 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020047#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000048#include "mbedtls/debug.h"
49#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010051#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020052
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050055#if defined(MBEDTLS_USE_PSA_CRYPTO)
56#include "mbedtls/psa_util.h"
57#include "psa/crypto.h"
58#endif
59
Janos Follath23bdca02016-10-07 14:47:14 +010060#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020062#endif
63
Hanno Beckercd9dcda2018-08-28 17:18:56 +010064static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010065
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020066/*
67 * Start a timer.
68 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020069 */
Hanno Becker0f57a652020-02-05 10:37:26 +000070void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020072 if( ssl->f_set_timer == NULL )
73 return;
74
75 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
76 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077}
78
79/*
80 * Return -1 is timer is expired, 0 if it isn't.
81 */
Hanno Becker7876d122020-02-05 10:39:31 +000082int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020085 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020086
87 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088 {
89 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020090 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020091 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092
93 return( 0 );
94}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095
Hanno Beckercfe45792019-07-03 16:13:00 +010096#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010097static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
98 unsigned char *buf,
99 size_t len,
100 mbedtls_record *rec );
101
Hanno Beckercfe45792019-07-03 16:13:00 +0100102int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
103 unsigned char *buf,
104 size_t buflen )
105{
Hanno Becker54229812019-07-12 14:40:00 +0100106 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
108 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
109
110 /* We don't support record checking in TLS because
111 * (a) there doesn't seem to be a usecase for it, and
112 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
113 * and we'd need to backup the transform here.
114 */
115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
116 {
117 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
118 goto exit;
119 }
120#if defined(MBEDTLS_SSL_PROTO_DTLS)
121 else
122 {
irwir734f0cf2019-09-26 21:03:24 +0300123 mbedtls_record rec;
124
Hanno Becker54229812019-07-12 14:40:00 +0100125 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
126 if( ret != 0 )
127 {
128 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
129 goto exit;
130 }
131
132 if( ssl->transform_in != NULL )
133 {
134 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
135 if( ret != 0 )
136 {
137 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
138 goto exit;
139 }
140 }
141 }
142#endif /* MBEDTLS_SSL_PROTO_DTLS */
143
144exit:
145 /* On success, we have decrypted the buffer in-place, so make
146 * sure we don't leak any plaintext data. */
147 mbedtls_platform_zeroize( buf, buflen );
148
149 /* For the purpose of this API, treat messages with unexpected CID
150 * as well as such from future epochs as unexpected. */
151 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
152 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
153 {
154 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
155 }
156
157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
158 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100159}
160#endif /* MBEDTLS_SSL_RECORD_CHECKING */
161
Hanno Becker67bc7c32018-08-06 11:33:50 +0100162#define SSL_DONT_FORCE_FLUSH 0
163#define SSL_FORCE_FLUSH 1
164
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100166
Hanno Beckerd5847772018-08-28 10:09:23 +0100167/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100168static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
169 uint8_t slot );
170static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
171static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
172static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
173static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100174static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
175 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100176static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100177
Hanno Becker11682cc2018-08-22 14:41:02 +0100178static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100179{
Hanno Becker89490712020-02-05 10:50:12 +0000180 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100181
182 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100183 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184
185 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
186}
187
Hanno Becker67bc7c32018-08-06 11:33:50 +0100188static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
189{
Hanno Becker11682cc2018-08-22 14:41:02 +0100190 size_t const bytes_written = ssl->out_left;
191 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100192
193 /* Double-check that the write-index hasn't gone
194 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100195 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100196 {
197 /* Should never happen... */
198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
199 }
200
201 return( (int) ( mtu - bytes_written ) );
202}
203
204static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
205{
Janos Follath865b3eb2019-12-16 11:46:15 +0000206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100207 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400208 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209
210#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
211 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
212
213 if( max_len > mfl )
214 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100215
216 /* By the standard (RFC 6066 Sect. 4), the MFL extension
217 * only limits the maximum record payload size, so in theory
218 * we would be allowed to pack multiple records of payload size
219 * MFL into a single datagram. However, this would mean that there's
220 * no way to explicitly communicate MTU restrictions to the peer.
221 *
222 * The following reduction of max_len makes sure that we never
223 * write datagrams larger than MFL + Record Expansion Overhead.
224 */
225 if( max_len <= ssl->out_left )
226 return( 0 );
227
228 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100229#endif
230
231 ret = ssl_get_remaining_space_in_datagram( ssl );
232 if( ret < 0 )
233 return( ret );
234 remaining = (size_t) ret;
235
236 ret = mbedtls_ssl_get_record_expansion( ssl );
237 if( ret < 0 )
238 return( ret );
239 expansion = (size_t) ret;
240
241 if( remaining <= expansion )
242 return( 0 );
243
244 remaining -= expansion;
245 if( remaining >= max_len )
246 remaining = max_len;
247
248 return( (int) remaining );
249}
250
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200251/*
252 * Double the retransmit timeout value, within the allowed range,
253 * returning -1 if the maximum value has already been reached.
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256{
257 uint32_t new_timeout;
258
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200259 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200260 return( -1 );
261
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200262 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
263 * in the following way: after the initial transmission and a first
264 * retransmission, back off to a temporary estimated MTU of 508 bytes.
265 * This value is guaranteed to be deliverable (if not guaranteed to be
266 * delivered) of any compliant IPv4 (and IPv6) network, and should work
267 * on most non-IP stacks too. */
268 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400269 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200270 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
272 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200273
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200274 new_timeout = 2 * ssl->handshake->retransmit_timeout;
275
276 /* Avoid arithmetic overflow and range overflow */
277 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200278 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200280 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281 }
282
283 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200285 ssl->handshake->retransmit_timeout ) );
286
287 return( 0 );
288}
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200292 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200294 ssl->handshake->retransmit_timeout ) );
295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
299int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200300 const unsigned char *key_enc, const unsigned char *key_dec,
301 size_t keylen,
302 const unsigned char *iv_enc, const unsigned char *iv_dec,
303 size_t ivlen,
304 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200305 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
307int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
308int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
309int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
311#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000312
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200313/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +0100314 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +0000315#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200316 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
317 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
318 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
319/* This function makes sure every byte in the memory region is accessed
320 * (in ascending addresses order) */
321static void ssl_read_memory( unsigned char *p, size_t len )
322{
323 unsigned char acc = 0;
324 volatile unsigned char force;
325
326 for( ; len != 0; p++, len-- )
327 acc ^= *p;
328
329 force = acc;
330 (void) force;
331}
332#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
333
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100334/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200336 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000337
Hanno Beckera0e20d02019-05-15 14:03:01 +0100338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +0100339/* This functions transforms a DTLS plaintext fragment and a record content
340 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100341 *
342 * struct {
343 * opaque content[DTLSPlaintext.length];
344 * ContentType real_type;
345 * uint8 zeros[length_of_padding];
346 * } DTLSInnerPlaintext;
347 *
348 * Input:
349 * - `content`: The beginning of the buffer holding the
350 * plaintext to be wrapped.
351 * - `*content_size`: The length of the plaintext in Bytes.
352 * - `max_len`: The number of Bytes available starting from
353 * `content`. This must be `>= *content_size`.
354 * - `rec_type`: The desired record content type.
355 *
356 * Output:
357 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
358 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
359 *
360 * Returns:
361 * - `0` on success.
362 * - A negative error code if `max_len` didn't offer enough space
363 * for the expansion.
364 */
365static int ssl_cid_build_inner_plaintext( unsigned char *content,
366 size_t *content_size,
367 size_t remaining,
368 uint8_t rec_type )
369{
370 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +0100371 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
372 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
373 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100374
375 /* Write real content type */
376 if( remaining == 0 )
377 return( -1 );
378 content[ len ] = rec_type;
379 len++;
380 remaining--;
381
382 if( remaining < pad )
383 return( -1 );
384 memset( content + len, 0, pad );
385 len += pad;
386 remaining -= pad;
387
388 *content_size = len;
389 return( 0 );
390}
391
Hanno Becker07dc97d2019-05-20 15:08:01 +0100392/* This function parses a DTLSInnerPlaintext structure.
393 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100394static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
395 size_t *content_size,
396 uint8_t *rec_type )
397{
398 size_t remaining = *content_size;
399
400 /* Determine length of padding by skipping zeroes from the back. */
401 do
402 {
403 if( remaining == 0 )
404 return( -1 );
405 remaining--;
406 } while( content[ remaining ] == 0 );
407
408 *content_size = remaining;
409 *rec_type = content[ remaining ];
410
411 return( 0 );
412}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100413#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100414
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100415/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100416 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000417static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100418 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000419 mbedtls_record *rec )
420{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100421 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100422 *
423 * additional_data = seq_num + TLSCompressed.type +
424 * TLSCompressed.version + TLSCompressed.length;
425 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100426 * For the CID extension, this is extended as follows
427 * (quoting draft-ietf-tls-dtls-connection-id-05,
428 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100429 *
430 * additional_data = seq_num + DTLSPlaintext.type +
431 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100432 * cid +
433 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100434 * length_of_DTLSInnerPlaintext;
435 */
436
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000437 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
438 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +0100439 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +0100440
Hanno Beckera0e20d02019-05-15 14:03:01 +0100441#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100442 if( rec->cid_len != 0 )
443 {
444 memcpy( add_data + 11, rec->cid, rec->cid_len );
445 add_data[11 + rec->cid_len + 0] = rec->cid_len;
446 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
447 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
448 *add_data_len = 13 + 1 + rec->cid_len;
449 }
450 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100452 {
453 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
454 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
455 *add_data_len = 13;
456 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000457}
458
Hanno Beckera18d1322018-01-03 14:27:32 +0000459int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
460 mbedtls_ssl_transform *transform,
461 mbedtls_record *rec,
462 int (*f_rng)(void *, unsigned char *, size_t),
463 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100466 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000467 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100468 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100469 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000470 size_t post_avail;
471
472 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000473#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200474 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000475 ((void) ssl);
476#endif
477
478 /* The PRNG is used for dynamic IV generation that's used
479 * for CBC transformations in TLS 1.1 and TLS 1.2. */
480#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
481 ( defined(MBEDTLS_AES_C) || \
482 defined(MBEDTLS_ARIA_C) || \
483 defined(MBEDTLS_CAMELLIA_C) ) && \
484 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
485 ((void) f_rng);
486 ((void) p_rng);
487#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000491 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100492 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
495 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100496 if( rec == NULL
497 || rec->buf == NULL
498 || rec->buf_len < rec->data_offset
499 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100501 || rec->cid_len != 0
502#endif
503 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000504 {
505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100507 }
508
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000509 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100510 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000512 data, rec->data_len );
513
514 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
515
516 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
517 {
518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
519 (unsigned) rec->data_len,
520 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
522 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100523
Hanno Beckera0e20d02019-05-15 14:03:01 +0100524#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100525 /*
526 * Add CID information
527 */
528 rec->cid_len = transform->out_cid_len;
529 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
530 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100531
532 if( rec->cid_len != 0 )
533 {
534 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100535 * Wrap plaintext into DTLSInnerPlaintext structure.
536 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100537 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100538 * Note that this changes `rec->data_len`, and hence
539 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100540 */
541 if( ssl_cid_build_inner_plaintext( data,
542 &rec->data_len,
543 post_avail,
544 rec->type ) != 0 )
545 {
546 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
547 }
548
549 rec->type = MBEDTLS_SSL_MSG_CID;
550 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100551#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100552
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100553 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
554
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100556 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Hanno Becker52344c22018-01-03 15:24:20 +0000558#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( mode == MBEDTLS_MODE_STREAM ||
560 ( mode == MBEDTLS_MODE_CBC
561#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000562 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100563#endif
564 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000566 if( post_avail < transform->maclen )
567 {
568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
569 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
570 }
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000573 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200574 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +0100575 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000576 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
577 data, rec->data_len, rec->ctr, rec->type, mac );
578 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200579 }
580 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
583 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000584 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200585 {
Hanno Becker992b6872017-11-09 18:57:39 +0000586 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
587
Hanno Beckercab87e62019-04-29 13:52:53 +0100588 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +0000589
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000590 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100591 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000592 mbedtls_md_hmac_update( &transform->md_ctx_enc,
593 data, rec->data_len );
594 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
595 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
596
597 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200598 }
599 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200600#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200604 }
605
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000606 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
607 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200608
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000609 rec->data_len += transform->maclen;
610 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100611 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200612 }
Hanno Becker52344c22018-01-03 15:24:20 +0000613#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200615 /*
616 * Encrypt
617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
619 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000622 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000624 "including %d bytes of padding",
625 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000627 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
628 transform->iv_enc, transform->ivlen,
629 data, rec->data_len,
630 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200633 return( ret );
634 }
635
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000636 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
Paul Bakker68884e32013-01-07 18:20:04 +0100642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000644
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200645#if defined(MBEDTLS_GCM_C) || \
646 defined(MBEDTLS_CCM_C) || \
647 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200649 mode == MBEDTLS_MODE_CCM ||
650 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200653 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000654 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000656 /* Check that there's space for both the authentication tag
657 * and the explicit IV before and after the record content. */
658 if( post_avail < transform->taglen ||
659 rec->data_offset < explicit_iv_len )
660 {
661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
662 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
663 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664
Paul Bakker68884e32013-01-07 18:20:04 +0100665 /*
666 * Generate IV
667 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200668 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
669 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200670 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200671 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000672 memcpy( iv + transform->fixed_ivlen, rec->ctr,
673 explicit_iv_len );
674 /* Prefix record content with explicit IV. */
675 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200676 }
677 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
678 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200679 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200680 unsigned char i;
681
682 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
683
684 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000685 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200686 }
687 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100688 {
689 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100692 }
693
Hanno Beckercab87e62019-04-29 13:52:53 +0100694 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +0100695
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200696 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
697 iv, transform->ivlen );
698 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000699 data - explicit_iv_len, explicit_iv_len );
700 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100701 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200703 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000705
Paul Bakker68884e32013-01-07 18:20:04 +0100706 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200707 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200708 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000709
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200710 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000711 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100712 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000713 data, rec->data_len, /* source */
714 data, &rec->data_len, /* destination */
715 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200718 return( ret );
719 }
720
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000721 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
722 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200723
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000724 rec->data_len += transform->taglen + explicit_iv_len;
725 rec->data_offset -= explicit_iv_len;
726 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100727 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000728 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
731#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000732 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000736 size_t padlen, i;
737 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000738
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000739 /* Currently we're always using minimal padding
740 * (up to 255 bytes would be allowed). */
741 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
742 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 padlen = 0;
744
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000745 /* Check there's enough space in the buffer for the padding. */
746 if( post_avail < padlen + 1 )
747 {
748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
749 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
750 }
751
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000753 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000755 rec->data_len += padlen + 1;
756 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000759 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
761 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000762 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000763 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000764 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000765 if( f_rng == NULL )
766 {
767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
769 }
770
771 if( rec->data_offset < transform->ivlen )
772 {
773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
774 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
775 }
776
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000777 /*
778 * Generate IV
779 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000780 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000781 if( ret != 0 )
782 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000783
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000784 memcpy( data - transform->ivlen, transform->iv_enc,
785 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000786
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000791 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000792 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200793 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000795 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
796 transform->iv_enc,
797 transform->ivlen,
798 data, rec->data_len,
799 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200802 return( ret );
803 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200804
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000805 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200809 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000812 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200813 {
814 /*
815 * Save IV in SSL3 and TLS1
816 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000817 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
818 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000820 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200821#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000822 {
823 data -= transform->ivlen;
824 rec->data_offset -= transform->ivlen;
825 rec->data_len += transform->ivlen;
826 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100829 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100830 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000831 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
832
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100833 /*
834 * MAC(MAC_write_key, seq_num +
835 * TLSCipherText.type +
836 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100837 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100838 * IV + // except for TLS 1.0
839 * ENC(content + padding + padding_length));
840 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000841
842 if( post_avail < transform->maclen)
843 {
844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
845 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
846 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100847
Hanno Beckercab87e62019-04-29 13:52:53 +0100848 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +0100849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000851 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100852 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100853
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000854 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100855 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000856 mbedtls_md_hmac_update( &transform->md_ctx_enc,
857 data, rec->data_len );
858 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
859 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100860
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000861 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100862
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000863 rec->data_len += transform->maclen;
864 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100865 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200869 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000871 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
874 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200875 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100877 /* Make extra sure authentication was performed, exactly once */
878 if( auth_done != 1 )
879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100882 }
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
886 return( 0 );
887}
888
Hanno Becker605949f2019-07-12 08:23:59 +0100889int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +0000890 mbedtls_ssl_transform *transform,
891 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000892{
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000893 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000895 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +0000896#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +0100897 size_t padlen = 0, correct = 1;
898#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000899 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100900 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100901 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000902
Hanno Beckera18d1322018-01-03 14:27:32 +0000903#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200904 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000905 ((void) ssl);
906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000909 if( rec == NULL ||
910 rec->buf == NULL ||
911 rec->buf_len < rec->data_offset ||
912 rec->buf_len - rec->data_offset < rec->data_len )
913 {
914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100916 }
917
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000918 data = rec->buf + rec->data_offset;
919 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920
Hanno Beckera0e20d02019-05-15 14:03:01 +0100921#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100922 /*
923 * Match record's CID with incoming CID.
924 */
Hanno Becker938489a2019-05-08 13:02:22 +0100925 if( rec->cid_len != transform->in_cid_len ||
926 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
927 {
Hanno Becker8367ccc2019-05-14 11:30:10 +0100928 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +0100929 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100930#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
933 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +0100934 {
935 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000936 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
937 transform->iv_dec,
938 transform->ivlen,
939 data, rec->data_len,
940 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200943 return( ret );
944 }
945
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000946 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
Paul Bakker68884e32013-01-07 18:20:04 +0100952 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200954#if defined(MBEDTLS_GCM_C) || \
955 defined(MBEDTLS_CCM_C) || \
956 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200958 mode == MBEDTLS_MODE_CCM ||
959 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000960 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200961 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200962 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000963
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200964 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +0100965 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200966 */
Hanno Beckerd96a6522019-07-10 13:55:25 +0100967
968 /* Check that there's enough space for the explicit IV
969 * (at the beginning of the record) and the MAC (at the
970 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000971 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +0200972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000974 "+ taglen (%d)", rec->data_len,
975 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978
Hanno Beckerd96a6522019-07-10 13:55:25 +0100979#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200980 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
981 {
Hanno Beckerd96a6522019-07-10 13:55:25 +0100982 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +0100983
Hanno Beckerd96a6522019-07-10 13:55:25 +0100984 /* Fixed */
985 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
986 /* Explicit */
987 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200988 }
Hanno Beckerd96a6522019-07-10 13:55:25 +0100989 else
990#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
991#if defined(MBEDTLS_CHACHAPOLY_C)
992 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200993 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +0200994 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200995 unsigned char i;
996
997 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
998
999 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001000 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001 }
1002 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01001003#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001004 {
1005 /* Reminder if we ever add an AEAD mode with a different size */
1006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1008 }
1009
Hanno Beckerd96a6522019-07-10 13:55:25 +01001010 /* Group changes to data, data_len, and add_data, because
1011 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001012 data += explicit_iv_len;
1013 rec->data_offset += explicit_iv_len;
1014 rec->data_len -= explicit_iv_len + transform->taglen;
1015
Hanno Beckercab87e62019-04-29 13:52:53 +01001016 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001017 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001018 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001019
Hanno Beckerd96a6522019-07-10 13:55:25 +01001020 /* Because of the check above, we know that there are
1021 * explicit_iv_len Bytes preceeding data, and taglen
1022 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001023 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001024 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001025
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001026 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001027 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001028 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001029
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001030 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001031 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001032 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001033 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1034 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001035 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001036 data, rec->data_len,
1037 data, &olen,
1038 data + rec->data_len,
1039 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1044 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001045
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001046 return( ret );
1047 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001048 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001049
Hanno Beckerd96a6522019-07-10 13:55:25 +01001050 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001051 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1054 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001055 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1059#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001060 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001063 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001064
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 /*
Paul Bakker45829992013-01-03 14:52:21 +01001066 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001069 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1070 {
1071 /* The ciphertext is prefixed with the CBC IV. */
1072 minlen += transform->ivlen;
1073 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001074#endif
Paul Bakker45829992013-01-03 14:52:21 +01001075
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001076 /* Size considerations:
1077 *
1078 * - The CBC cipher text must not be empty and hence
1079 * at least of size transform->ivlen.
1080 *
1081 * Together with the potential IV-prefix, this explains
1082 * the first of the two checks below.
1083 *
1084 * - The record must contain a MAC, either in plain or
1085 * encrypted, depending on whether Encrypt-then-MAC
1086 * is used or not.
1087 * - If it is, the message contains the IV-prefix,
1088 * the CBC ciphertext, and the MAC.
1089 * - If it is not, the padded plaintext, and hence
1090 * the CBC ciphertext, has at least length maclen + 1
1091 * because there is at least the padding length byte.
1092 *
1093 * As the CBC ciphertext is not empty, both cases give the
1094 * lower bound minlen + maclen + 1 on the record size, which
1095 * we test for in the second check below.
1096 */
1097 if( rec->data_len < minlen + transform->ivlen ||
1098 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001101 "+ 1 ) ( + expl IV )", rec->data_len,
1102 transform->ivlen,
1103 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001105 }
1106
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001107 /*
1108 * Authenticate before decrypt if enabled
1109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001111 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001112 {
Hanno Becker992b6872017-11-09 18:57:39 +00001113 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001116
Hanno Beckerd96a6522019-07-10 13:55:25 +01001117 /* Update data_len in tandem with add_data.
1118 *
1119 * The subtraction is safe because of the previous check
1120 * data_len >= minlen + maclen + 1.
1121 *
1122 * Afterwards, we know that data + data_len is followed by at
1123 * least maclen Bytes, which justifies the call to
1124 * mbedtls_ssl_safer_memcmp() below.
1125 *
1126 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001127 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01001128 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001129
Hanno Beckerd96a6522019-07-10 13:55:25 +01001130 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001131 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1132 add_data_len );
1133 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1134 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001135 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1136 data, rec->data_len );
1137 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1138 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001139
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001140 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1141 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001142 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001143 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001144
Hanno Beckerd96a6522019-07-10 13:55:25 +01001145 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001146 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1147 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001151 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001152 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001155
1156 /*
1157 * Check length sanity
1158 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001159
1160 /* We know from above that data_len > minlen >= 0,
1161 * so the following check in particular implies that
1162 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001163 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001166 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001168 }
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001171 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001172 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001173 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001174 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001175 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001176 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001177 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001178
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001179 data += transform->ivlen;
1180 rec->data_offset += transform->ivlen;
1181 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001182 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001184
Hanno Beckerd96a6522019-07-10 13:55:25 +01001185 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1186
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001187 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1188 transform->iv_dec, transform->ivlen,
1189 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001192 return( ret );
1193 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001194
Hanno Beckerd96a6522019-07-10 13:55:25 +01001195 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001196 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001200 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001203 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001204 {
1205 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001206 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1207 * records is equivalent to CBC decryption of the concatenation
1208 * of the records; in other words, IVs are maintained across
1209 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001210 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001211 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1212 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001214#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001216 /* Safe since data_len >= minlen + maclen + 1, so after having
1217 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001218 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1219 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001220 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001221
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001222 if( auth_done == 1 )
1223 {
1224 correct *= ( rec->data_len >= padlen + 1 );
1225 padlen *= ( rec->data_len >= padlen + 1 );
1226 }
1227 else
Paul Bakker45829992013-01-03 14:52:21 +01001228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001230 if( rec->data_len < transform->maclen + padlen + 1 )
1231 {
1232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1233 rec->data_len,
1234 transform->maclen,
1235 padlen + 1 ) );
1236 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001237#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001238
1239 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
1240 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01001241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001243 padlen++;
1244
1245 /* Regardless of the validity of the padding,
1246 * we have data_len >= padlen here. */
1247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001249 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001251 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_SSL_DEBUG_ALL)
1254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001255 "should be no more than %d",
1256 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001257#endif
Paul Bakker45829992013-01-03 14:52:21 +01001258 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260 }
1261 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1263#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1264 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001265 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001267 /* The padding check involves a series of up to 256
1268 * consecutive memory reads at the end of the record
1269 * plaintext buffer. In order to hide the length and
1270 * validity of the padding, always perform exactly
1271 * `min(256,plaintext_len)` reads (but take into account
1272 * only the last `padlen` bytes for the padding check). */
1273 size_t pad_count = 0;
1274 size_t real_count = 0;
1275 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001276
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001277 /* Index of first padding byte; it has been ensured above
1278 * that the subtraction is safe. */
1279 size_t const padding_idx = rec->data_len - padlen;
1280 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1281 size_t const start_idx = rec->data_len - num_checks;
1282 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001283
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001284 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001285 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001286 real_count |= ( idx >= padding_idx );
1287 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001288 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001289 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001292 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001294#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001295 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1299 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001303 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001304
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001305 /* If the padding was found to be invalid, padlen == 0
1306 * and the subtraction is safe. If the padding was found valid,
1307 * padlen hasn't been changed and the previous assertion
1308 * data_len >= padlen still holds. */
1309 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001313 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001317 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001319#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001321 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
1324 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001325 * Authenticate if not done yet.
1326 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 */
Hanno Becker52344c22018-01-03 15:24:20 +00001328#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001329 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001330 {
Hanno Becker992b6872017-11-09 18:57:39 +00001331 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001332
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001333 /* If the initial value of padlen was such that
1334 * data_len < maclen + padlen + 1, then padlen
1335 * got reset to 1, and the initial check
1336 * data_len >= minlen + maclen + 1
1337 * guarantees that at this point we still
1338 * have at least data_len >= maclen.
1339 *
1340 * If the initial value of padlen was such that
1341 * data_len >= maclen + padlen + 1, then we have
1342 * subtracted either padlen + 1 (if the padding was correct)
1343 * or 0 (if the padding was incorrect) since then,
1344 * hence data_len >= maclen in any case.
1345 */
1346 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01001347 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001350 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001351 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001352 ssl_mac( &transform->md_ctx_dec,
1353 transform->mac_dec,
1354 data, rec->data_len,
1355 rec->ctr, rec->type,
1356 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001357 }
1358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1360#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1361 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001362 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001363 {
1364 /*
1365 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02001366 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001367 *
1368 * Known timing attacks:
1369 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1370 *
Gilles Peskine20b44082018-05-29 14:06:49 +02001371 * To compensate for different timings for the MAC calculation
1372 * depending on how much padding was removed (which is determined
1373 * by padlen), process extra_run more blocks through the hash
1374 * function.
1375 *
1376 * The formula in the paper is
1377 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1378 * where L1 is the size of the header plus the decrypted message
1379 * plus CBC padding and L2 is the size of the header plus the
1380 * decrypted message. This is for an underlying hash function
1381 * with 64-byte blocks.
1382 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1383 * correctly. We round down instead of up, so -56 is the correct
1384 * value for our calculations instead of -55.
1385 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02001386 * Repeat the formula rather than defining a block_size variable.
1387 * This avoids requiring division by a variable at runtime
1388 * (which would be marginally less efficient and would require
1389 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001390 */
1391 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001392 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001393
1394 /*
1395 * The next two sizes are the minimum and maximum values of
1396 * in_msglen over all padlen values.
1397 *
1398 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001399 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001400 *
1401 * Note that max_len + maclen is never more than the buffer
1402 * length, as we previously did in_msglen -= maclen too.
1403 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001404 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001405 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1406
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001407 memset( tmp, 0, sizeof( tmp ) );
1408
1409 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001410 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001411#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1412 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001413 case MBEDTLS_MD_MD5:
1414 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001415 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001416 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001417 extra_run =
1418 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1419 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001420 break;
1421#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001422#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001423 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001424 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001425 extra_run =
1426 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1427 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001428 break;
1429#endif
1430 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1433 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001434
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001435 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436
Hanno Beckercab87e62019-04-29 13:52:53 +01001437 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1438 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001439 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1440 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001441 /* Make sure we access everything even when padlen > 0. This
1442 * makes the synchronisation requirements for just-in-time
1443 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001444 ssl_read_memory( data + rec->data_len, padlen );
1445 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001446
1447 /* Call mbedtls_md_process at least once due to cache attacks
1448 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001449 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001450 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001451
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001452 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001453
1454 /* Make sure we access all the memory that could contain the MAC,
1455 * before we check it in the next code block. This makes the
1456 * synchronisation requirements for just-in-time Prime+Probe
1457 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001458 ssl_read_memory( data + min_len,
1459 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001460 }
1461 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1463 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001469#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001470 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1471 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001474 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1475 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_DEBUG_ALL)
1478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001479#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001480 correct = 0;
1481 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001482 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001483 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001484
1485 /*
1486 * Finally check the correct flag
1487 */
1488 if( correct == 0 )
1489 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001490#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001491
1492 /* Make extra sure authentication was performed, exactly once */
1493 if( auth_done != 1 )
1494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
Hanno Beckera0e20d02019-05-15 14:03:01 +01001499#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001500 if( rec->cid_len != 0 )
1501 {
1502 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
1503 &rec->type );
1504 if( ret != 0 )
1505 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1506 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001507#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
1511 return( 0 );
1512}
1513
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001514#undef MAC_NONE
1515#undef MAC_PLAINTEXT
1516#undef MAC_CIPHERTEXT
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001519/*
1520 * Compression/decompression functions
1521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001523{
Janos Follath865b3eb2019-12-16 11:46:15 +00001524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001525 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001526 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001527 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001528 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001531
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001532 if( len_pre == 0 )
1533 return( 0 );
1534
Paul Bakker2770fbd2012-07-03 13:30:23 +00001535 memcpy( msg_pre, ssl->out_msg, len_pre );
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001538 ssl->out_msglen ) );
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001541 ssl->out_msg, ssl->out_msglen );
1542
Paul Bakker48916f92012-09-16 19:57:18 +00001543 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1544 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1545 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10001546 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001547
Paul Bakker48916f92012-09-16 19:57:18 +00001548 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001549 if( ret != Z_OK )
1550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1552 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001553 }
1554
Angus Grattond8213d02016-05-25 20:56:48 +10001555 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001556 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001559 ssl->out_msglen ) );
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001562 ssl->out_msg, ssl->out_msglen );
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001565
1566 return( 0 );
1567}
1568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001570{
Janos Follath865b3eb2019-12-16 11:46:15 +00001571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001572 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001573 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001574 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001575 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001578
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001579 if( len_pre == 0 )
1580 return( 0 );
1581
Paul Bakker2770fbd2012-07-03 13:30:23 +00001582 memcpy( msg_pre, ssl->in_msg, len_pre );
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001585 ssl->in_msglen ) );
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588 ssl->in_msg, ssl->in_msglen );
1589
Paul Bakker48916f92012-09-16 19:57:18 +00001590 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1591 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1592 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10001593 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001594 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001595
Paul Bakker48916f92012-09-16 19:57:18 +00001596 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 if( ret != Z_OK )
1598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1600 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601 }
1602
Angus Grattond8213d02016-05-25 20:56:48 +10001603 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001604 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001607 ssl->in_msglen ) );
1608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001610 ssl->in_msg, ssl->in_msglen );
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613
1614 return( 0 );
1615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617
Paul Bakker5121ce52009-01-03 21:22:43 +00001618/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001619 * Fill the input message buffer by appending data to it.
1620 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001621 *
1622 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1623 * available (from this read and/or a previous one). Otherwise, an error code
1624 * is returned (possibly EOF or WANT_READ).
1625 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001626 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1627 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1628 * since we always read a whole datagram at once.
1629 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001630 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001631 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634{
Janos Follath865b3eb2019-12-16 11:46:15 +00001635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001636 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001640 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001643 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001645 }
1646
Angus Grattond8213d02016-05-25 20:56:48 +10001647 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001651 }
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001654 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001656 uint32_t timeout;
1657
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001658 /* Just to be sure */
1659 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1660 {
1661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1662 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1664 }
1665
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001666 /*
1667 * The point is, we need to always read a full datagram at once, so we
1668 * sometimes read more then requested, and handle the additional data.
1669 * It could be the rest of the current record (while fetching the
1670 * header) and/or some other records in the same datagram.
1671 */
1672
1673 /*
1674 * Move to the next record in the already read datagram if applicable
1675 */
1676 if( ssl->next_record_offset != 0 )
1677 {
1678 if( ssl->in_left < ssl->next_record_offset )
1679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001682 }
1683
1684 ssl->in_left -= ssl->next_record_offset;
1685
1686 if( ssl->in_left != 0 )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001689 ssl->next_record_offset ) );
1690 memmove( ssl->in_hdr,
1691 ssl->in_hdr + ssl->next_record_offset,
1692 ssl->in_left );
1693 }
1694
1695 ssl->next_record_offset = 0;
1696 }
1697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001700
1701 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001702 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001703 */
1704 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001707 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001708 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001709
1710 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001711 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001712 * are not at the beginning of a new record, the caller did something
1713 * wrong.
1714 */
1715 if( ssl->in_left != 0 )
1716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001719 }
1720
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001721 /*
1722 * Don't even try to read if time's out already.
1723 * This avoids by-passing the timer when repeatedly receiving messages
1724 * that will end up being dropped.
1725 */
Hanno Becker7876d122020-02-05 10:39:31 +00001726 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001727 {
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001729 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001730 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001731 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001732 {
Angus Grattond8213d02016-05-25 20:56:48 +10001733 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001736 timeout = ssl->handshake->retransmit_timeout;
1737 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001738 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001741
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001742 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001743 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1744 timeout );
1745 else
1746 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001749
1750 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001752 }
1753
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001754 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001757 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001760 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001761 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001764 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001770 return( ret );
1771 }
1772
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001773 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001776 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001778 {
Hanno Becker786300f2020-02-05 10:46:40 +00001779 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001780 {
Hanno Becker786300f2020-02-05 10:46:40 +00001781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1782 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001783 return( ret );
1784 }
1785
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001786 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001789 }
1790
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 if( ret < 0 )
1792 return( ret );
1793
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001794 ssl->in_left = ret;
1795 }
1796 else
1797#endif
1798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001800 ssl->in_left, nb_want ) );
1801
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001802 while( ssl->in_left < nb_want )
1803 {
1804 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001805
Hanno Becker7876d122020-02-05 10:39:31 +00001806 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001807 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1808 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001809 {
1810 if( ssl->f_recv_timeout != NULL )
1811 {
1812 ret = ssl->f_recv_timeout( ssl->p_bio,
1813 ssl->in_hdr + ssl->in_left, len,
1814 ssl->conf->read_timeout );
1815 }
1816 else
1817 {
1818 ret = ssl->f_recv( ssl->p_bio,
1819 ssl->in_hdr + ssl->in_left, len );
1820 }
1821 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001824 ssl->in_left, nb_want ) );
1825 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001826
1827 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001829
1830 if( ret < 0 )
1831 return( ret );
1832
mohammad160352aecb92018-03-28 23:41:40 -07001833 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001834 {
Darryl Green11999bb2018-03-13 15:22:58 +00001835 MBEDTLS_SSL_DEBUG_MSG( 1,
1836 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07001837 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1839 }
1840
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001841 ssl->in_left += ret;
1842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 }
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 return( 0 );
1848}
1849
1850/*
1851 * Flush any data not yet written
1852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001854{
Janos Follath865b3eb2019-12-16 11:46:15 +00001855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001856 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001860 if( ssl->f_send == NULL )
1861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001863 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001865 }
1866
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001867 /* Avoid incrementing counter if data is flushed */
1868 if( ssl->out_left == 0 )
1869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001871 return( 0 );
1872 }
1873
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 while( ssl->out_left > 0 )
1875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01001877 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Hanno Becker2b1e3542018-08-06 11:19:13 +01001879 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001880 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 if( ret <= 0 )
1885 return( ret );
1886
mohammad160352aecb92018-03-28 23:41:40 -07001887 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001888 {
Darryl Green11999bb2018-03-13 15:22:58 +00001889 MBEDTLS_SSL_DEBUG_MSG( 1,
1890 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07001891 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001892 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1893 }
1894
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 ssl->out_left -= ret;
1896 }
1897
Hanno Becker2b1e3542018-08-06 11:19:13 +01001898#if defined(MBEDTLS_SSL_PROTO_DTLS)
1899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001900 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001901 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001902 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001903 else
1904#endif
1905 {
1906 ssl->out_hdr = ssl->out_buf + 8;
1907 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001908 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
1912 return( 0 );
1913}
1914
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001915/*
1916 * Functions to handle the DTLS retransmission state machine
1917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001919/*
1920 * Append current handshake message to current outgoing flight
1921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1926 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1927 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001928
1929 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001930 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001931 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001934 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001935 }
1936
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001937 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001938 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001941 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001942 }
1943
1944 /* Copy current handshake message with headers */
1945 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1946 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001947 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001948 msg->next = NULL;
1949
1950 /* Append to the current flight */
1951 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001952 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001953 else
1954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001956 while( cur->next != NULL )
1957 cur = cur->next;
1958 cur->next = msg;
1959 }
1960
Hanno Becker3b235902018-08-06 09:54:53 +01001961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001962 return( 0 );
1963}
1964
1965/*
1966 * Free the current flight of handshake messages
1967 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00001968void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 mbedtls_ssl_flight_item *cur = flight;
1971 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001972
1973 while( cur != NULL )
1974 {
1975 next = cur->next;
1976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 mbedtls_free( cur->p );
1978 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001979
1980 cur = next;
1981 }
1982}
1983
1984/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001985 * Swap transform_out and out_ctr with the alternative ones
1986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001990 unsigned char tmp_out_ctr[8];
1991
1992 if( ssl->transform_out == ssl->handshake->alt_transform_out )
1993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001995 return;
1996 }
1997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001999
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002000 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002001 tmp_transform = ssl->transform_out;
2002 ssl->transform_out = ssl->handshake->alt_transform_out;
2003 ssl->handshake->alt_transform_out = tmp_transform;
2004
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002005 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002006 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2007 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002008 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002009
2010 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002011 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2014 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2019 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002020 }
2021 }
2022#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002023}
2024
2025/*
2026 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002027 */
2028int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2029{
2030 int ret = 0;
2031
2032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2033
2034 ret = mbedtls_ssl_flight_transmit( ssl );
2035
2036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2037
2038 return( ret );
2039}
2040
2041/*
2042 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002043 *
2044 * Need to remember the current message in case flush_output returns
2045 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002046 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002047 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002048int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002049{
Janos Follath865b3eb2019-12-16 11:46:15 +00002050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002054 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002056
2057 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002058 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002059 ssl_swap_epochs( ssl );
2060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002062 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002063
2064 while( ssl->handshake->cur_msg != NULL )
2065 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002066 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002067 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002068
Hanno Beckere1dcb032018-08-17 16:47:58 +01002069 int const is_finished =
2070 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2071 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2072
Hanno Becker04da1892018-08-14 13:22:10 +01002073 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2074 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2075
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002076 /* Swap epochs before sending Finished: we can't do it after
2077 * sending ChangeCipherSpec, in case write returns WANT_READ.
2078 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002079 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002080 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002082 ssl_swap_epochs( ssl );
2083 }
2084
Hanno Becker67bc7c32018-08-06 11:33:50 +01002085 ret = ssl_get_remaining_payload_in_datagram( ssl );
2086 if( ret < 0 )
2087 return( ret );
2088 max_frag_len = (size_t) ret;
2089
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002090 /* CCS is copied as is, while HS messages may need fragmentation */
2091 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2092 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002093 if( max_frag_len == 0 )
2094 {
2095 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2096 return( ret );
2097
2098 continue;
2099 }
2100
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002101 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002102 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002103 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002104
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002105 /* Update position inside current message */
2106 ssl->handshake->cur_msg_p += cur->len;
2107 }
2108 else
2109 {
2110 const unsigned char * const p = ssl->handshake->cur_msg_p;
2111 const size_t hs_len = cur->len - 12;
2112 const size_t frag_off = p - ( cur->p + 12 );
2113 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002114 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002115
Hanno Beckere1dcb032018-08-17 16:47:58 +01002116 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002117 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002118 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002119 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002120
Hanno Becker67bc7c32018-08-06 11:33:50 +01002121 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2122 return( ret );
2123
2124 continue;
2125 }
2126 max_hs_frag_len = max_frag_len - 12;
2127
2128 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2129 max_hs_frag_len : rem_len;
2130
2131 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002132 {
2133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002134 (unsigned) cur_hs_frag_len,
2135 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002136 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002137
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002138 /* Messages are stored with handshake headers as if not fragmented,
2139 * copy beginning of headers then fill fragmentation fields.
2140 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2141 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002143 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2144 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2145 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2146
Hanno Becker67bc7c32018-08-06 11:33:50 +01002147 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2148 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2149 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002150
2151 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2152
Hanno Becker3f7b9732018-08-28 09:53:25 +01002153 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002154 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2155 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002156 ssl->out_msgtype = cur->type;
2157
2158 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002159 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002160 }
2161
2162 /* If done with the current message move to the next one if any */
2163 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2164 {
2165 if( cur->next != NULL )
2166 {
2167 ssl->handshake->cur_msg = cur->next;
2168 ssl->handshake->cur_msg_p = cur->next->p + 12;
2169 }
2170 else
2171 {
2172 ssl->handshake->cur_msg = NULL;
2173 ssl->handshake->cur_msg_p = NULL;
2174 }
2175 }
2176
2177 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002178 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002181 return( ret );
2182 }
2183 }
2184
Hanno Becker67bc7c32018-08-06 11:33:50 +01002185 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2186 return( ret );
2187
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002188 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2190 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002191 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002194 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002195 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002196
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002198
2199 return( 0 );
2200}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002201
2202/*
2203 * To be called when the last message of an incoming flight is received.
2204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002206{
2207 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002208 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002209 ssl->handshake->flight = NULL;
2210 ssl->handshake->cur_msg = NULL;
2211
2212 /* The next incoming flight will start with this msg_seq */
2213 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2214
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002215 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002216 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002217
Hanno Becker0271f962018-08-16 13:23:47 +01002218 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002219 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002220
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002221 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002222 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2225 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002228 }
2229 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002231}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002232
2233/*
2234 * To be called when the last message of an outgoing flight is send.
2235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002237{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002238 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002239 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2242 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002245 }
2246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002250
Paul Bakker5121ce52009-01-03 21:22:43 +00002251/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002252 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254
2255/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002256 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002257 *
2258 * - fill in handshake headers
2259 * - update handshake checksum
2260 * - DTLS: save message for resending
2261 * - then pass to the record layer
2262 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002263 * DTLS: except for HelloRequest, messages are only queued, and will only be
2264 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002265 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002266 * Inputs:
2267 * - ssl->out_msglen: 4 + actual handshake message len
2268 * (4 is the size of handshake headers for TLS)
2269 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2270 * - ssl->out_msg + 4: the handshake message body
2271 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002272 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002273 * - ssl->out_msglen: the length of the record contents
2274 * (including handshake headers but excluding record headers)
2275 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002276 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002277int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278{
Janos Follath865b3eb2019-12-16 11:46:15 +00002279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002280 const size_t hs_len = ssl->out_msglen - 4;
2281 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2284
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002285 /*
2286 * Sanity checks
2287 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002288 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002289 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2290 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002291 /* In SSLv3, the client might send a NoCertificate alert. */
2292#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2293 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2294 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2295 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2296#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2297 {
2298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2300 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002303 /* Whenever we send anything different from a
2304 * HelloRequest we should be in a handshake - double check. */
2305 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2306 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002307 ssl->handshake == NULL )
2308 {
2309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002315 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002317 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002320 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002321#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002322
Hanno Beckerb50a2532018-08-06 11:52:54 +01002323 /* Double-check that we did not exceed the bounds
2324 * of the outgoing record buffer.
2325 * This should never fail as the various message
2326 * writing functions must obey the bounds of the
2327 * outgoing record buffer, but better be safe.
2328 *
2329 * Note: We deliberately do not check for the MTU or MFL here.
2330 */
2331 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2332 {
2333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2334 "size %u, maximum %u",
2335 (unsigned) ssl->out_msglen,
2336 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2338 }
2339
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002340 /*
2341 * Fill handshake headers
2342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002345 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2346 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2347 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002349 /*
2350 * DTLS has additional fields in the Handshake layer,
2351 * between the length field and the actual payload:
2352 * uint16 message_seq;
2353 * uint24 fragment_offset;
2354 * uint24 fragment_length;
2355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002358 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002359 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002360 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002361 {
2362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2363 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002364 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002365 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2367 }
2368
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002369 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002370 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002371
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002372 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002373 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002374 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002375 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2376 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2377 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002378 }
2379 else
2380 {
2381 ssl->out_msg[4] = 0;
2382 ssl->out_msg[5] = 0;
2383 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002384
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002385 /* Handshake hashes are computed without fragmentation,
2386 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002387 memset( ssl->out_msg + 6, 0x00, 3 );
2388 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002391
Hanno Becker0207e532018-08-28 10:28:28 +01002392 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002393 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2394 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
2396
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002397 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002400 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2401 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002402 {
2403 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002406 return( ret );
2407 }
2408 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002409 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002410#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002411 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002412 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002413 {
2414 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2415 return( ret );
2416 }
2417 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002418
2419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2420
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002421 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002422}
2423
2424/*
2425 * Record layer functions
2426 */
2427
2428/*
2429 * Write current record.
2430 *
2431 * Uses:
2432 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2433 * - ssl->out_msglen: length of the record content (excl headers)
2434 * - ssl->out_msg: record content
2435 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002436int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002437{
2438 int ret, done = 0;
2439 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002440 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002441
2442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002445 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002447 {
2448 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002451 return( ret );
2452 }
2453
2454 len = ssl->out_msglen;
2455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2459 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 ret = mbedtls_ssl_hw_record_write( ssl );
2464 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2467 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002468 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002469
2470 if( ret == 0 )
2471 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002474 if( !done )
2475 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002476 unsigned i;
2477 size_t protected_record_size;
2478
Hanno Becker6430faf2019-05-08 11:57:13 +01002479 /* Skip writing the record content type to after the encryption,
2480 * as it may change when using the CID extension. */
2481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002483 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002484
Hanno Becker19859472018-08-06 09:40:20 +01002485 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002486 ssl->out_len[0] = (unsigned char)( len >> 8 );
2487 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002488
Paul Bakker48916f92012-09-16 19:57:18 +00002489 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002490 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 mbedtls_record rec;
2492
2493 rec.buf = ssl->out_iv;
2494 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
2495 ( ssl->out_iv - ssl->out_buf );
2496 rec.data_len = ssl->out_msglen;
2497 rec.data_offset = ssl->out_msg - rec.buf;
2498
2499 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2500 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2501 ssl->conf->transport, rec.ver );
2502 rec.type = ssl->out_msgtype;
2503
Hanno Beckera0e20d02019-05-15 14:03:01 +01002504#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002505 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002506 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002507#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002508
Hanno Beckera18d1322018-01-03 14:27:32 +00002509 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002513 return( ret );
2514 }
2515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 if( rec.data_offset != 0 )
2517 {
2518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2520 }
2521
Hanno Becker6430faf2019-05-08 11:57:13 +01002522 /* Update the record content type and CID. */
2523 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002524#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002525 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002526#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002527 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2529 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002530 }
2531
Hanno Becker5903de42019-05-03 14:46:38 +01002532 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002533
2534#if defined(MBEDTLS_SSL_PROTO_DTLS)
2535 /* In case of DTLS, double-check that we don't exceed
2536 * the remaining space in the datagram. */
2537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2538 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002539 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002540 if( ret < 0 )
2541 return( ret );
2542
2543 if( protected_record_size > (size_t) ret )
2544 {
2545 /* Should never happen */
2546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2547 }
2548 }
2549#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002550
Hanno Becker6430faf2019-05-08 11:57:13 +01002551 /* Now write the potentially updated record content type. */
2552 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002555 "version = [%d:%d], msglen = %d",
2556 ssl->out_hdr[0], ssl->out_hdr[1],
2557 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002560 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002561
2562 ssl->out_left += protected_record_size;
2563 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002564 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002565
Hanno Beckerdd772292020-02-05 10:38:31 +00002566 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002567 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2568 break;
2569
2570 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002571 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002572 {
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2574 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
Hanno Becker67bc7c32018-08-06 11:33:50 +01002578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2580 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002581 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002582 size_t remaining;
2583 ret = ssl_get_remaining_payload_in_datagram( ssl );
2584 if( ret < 0 )
2585 {
2586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2587 ret );
2588 return( ret );
2589 }
2590
2591 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002592 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002593 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002594 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002595 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002596 else
2597 {
Hanno Becker513815a2018-08-20 11:56:09 +01002598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002599 }
2600 }
2601#endif /* MBEDTLS_SSL_PROTO_DTLS */
2602
2603 if( ( flush == SSL_FORCE_FLUSH ) &&
2604 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 return( ret );
2608 }
2609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
2612 return( 0 );
2613}
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002616
2617static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2618{
2619 if( ssl->in_msglen < ssl->in_hslen ||
2620 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2621 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2622 {
2623 return( 1 );
2624 }
2625 return( 0 );
2626}
Hanno Becker44650b72018-08-16 12:51:11 +01002627
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002628static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002629{
2630 return( ( ssl->in_msg[9] << 16 ) |
2631 ( ssl->in_msg[10] << 8 ) |
2632 ssl->in_msg[11] );
2633}
2634
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002635static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002636{
2637 return( ( ssl->in_msg[6] << 16 ) |
2638 ( ssl->in_msg[7] << 8 ) |
2639 ssl->in_msg[8] );
2640}
2641
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002642static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002643{
2644 uint32_t msg_len, frag_off, frag_len;
2645
2646 msg_len = ssl_get_hs_total_len( ssl );
2647 frag_off = ssl_get_hs_frag_off( ssl );
2648 frag_len = ssl_get_hs_frag_len( ssl );
2649
2650 if( frag_off > msg_len )
2651 return( -1 );
2652
2653 if( frag_len > msg_len - frag_off )
2654 return( -1 );
2655
2656 if( frag_len + 12 > ssl->in_msglen )
2657 return( -1 );
2658
2659 return( 0 );
2660}
2661
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002662/*
2663 * Mark bits in bitmask (used for DTLS HS reassembly)
2664 */
2665static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2666{
2667 unsigned int start_bits, end_bits;
2668
2669 start_bits = 8 - ( offset % 8 );
2670 if( start_bits != 8 )
2671 {
2672 size_t first_byte_idx = offset / 8;
2673
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002674 /* Special case */
2675 if( len <= start_bits )
2676 {
2677 for( ; len != 0; len-- )
2678 mask[first_byte_idx] |= 1 << ( start_bits - len );
2679
2680 /* Avoid potential issues with offset or len becoming invalid */
2681 return;
2682 }
2683
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002684 offset += start_bits; /* Now offset % 8 == 0 */
2685 len -= start_bits;
2686
2687 for( ; start_bits != 0; start_bits-- )
2688 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2689 }
2690
2691 end_bits = len % 8;
2692 if( end_bits != 0 )
2693 {
2694 size_t last_byte_idx = ( offset + len ) / 8;
2695
2696 len -= end_bits; /* Now len % 8 == 0 */
2697
2698 for( ; end_bits != 0; end_bits-- )
2699 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2700 }
2701
2702 memset( mask + offset / 8, 0xFF, len / 8 );
2703}
2704
2705/*
2706 * Check that bitmask is full
2707 */
2708static int ssl_bitmask_check( unsigned char *mask, size_t len )
2709{
2710 size_t i;
2711
2712 for( i = 0; i < len / 8; i++ )
2713 if( mask[i] != 0xFF )
2714 return( -1 );
2715
2716 for( i = 0; i < len % 8; i++ )
2717 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2718 return( -1 );
2719
2720 return( 0 );
2721}
2722
Hanno Becker56e205e2018-08-16 09:06:12 +01002723/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002724static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002725 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002726{
Hanno Becker56e205e2018-08-16 09:06:12 +01002727 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002728
Hanno Becker56e205e2018-08-16 09:06:12 +01002729 alloc_len = 12; /* Handshake header */
2730 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002731
Hanno Beckerd07df862018-08-16 09:14:58 +01002732 if( add_bitmap )
2733 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002734
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002735 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002736}
Hanno Becker56e205e2018-08-16 09:06:12 +01002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002739
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002740static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002741{
2742 return( ( ssl->in_msg[1] << 16 ) |
2743 ( ssl->in_msg[2] << 8 ) |
2744 ssl->in_msg[3] );
2745}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002746
Simon Butcher99000142016-10-13 17:21:01 +01002747int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002752 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002754 }
2755
Hanno Becker12555c62018-08-16 12:47:53 +01002756 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002759 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002760 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002764 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002766 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002767
Hanno Becker44650b72018-08-16 12:51:11 +01002768 if( ssl_check_hs_header( ssl ) != 0 )
2769 {
2770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2771 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2772 }
2773
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002774 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002775 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2776 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2777 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2778 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002779 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002780 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2781 {
2782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2783 recv_msg_seq,
2784 ssl->handshake->in_msg_seq ) );
2785 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2786 }
2787
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002788 /* Retransmit only on last message from previous flight, to avoid
2789 * too many retransmissions.
2790 * Besides, No sane server ever retransmits HelloVerifyRequest */
2791 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002795 "message_seq = %d, start_of_flight = %d",
2796 recv_msg_seq,
2797 ssl->handshake->in_flight_start_seq ) );
2798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002802 return( ret );
2803 }
2804 }
2805 else
2806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002808 "message_seq = %d, expected = %d",
2809 recv_msg_seq,
2810 ssl->handshake->in_msg_seq ) );
2811 }
2812
Hanno Becker90333da2017-10-10 11:27:13 +01002813 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002814 }
2815 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002816
Hanno Becker6d97ef52018-08-16 13:09:04 +01002817 /* Message reassembly is handled alongside buffering of future
2818 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002819 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002820 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002821 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002824 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002825 }
2826 }
2827 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002829 /* With TLS we don't handle fragmentation (for now) */
2830 if( ssl->in_msglen < ssl->in_hslen )
2831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2833 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002834 }
2835
Simon Butcher99000142016-10-13 17:21:01 +01002836 return( 0 );
2837}
2838
2839void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2840{
Hanno Becker0271f962018-08-16 13:23:47 +01002841 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002842
Hanno Becker0271f962018-08-16 13:23:47 +01002843 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002844 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002845 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002846 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002847
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002848 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002850 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002851 ssl->handshake != NULL )
2852 {
Hanno Becker0271f962018-08-16 13:23:47 +01002853 unsigned offset;
2854 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002855
Hanno Becker0271f962018-08-16 13:23:47 +01002856 /* Increment handshake sequence number */
2857 hs->in_msg_seq++;
2858
2859 /*
2860 * Clear up handshake buffering and reassembly structure.
2861 */
2862
2863 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002864 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002865
2866 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002867 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2868 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002869 offset++, hs_buf++ )
2870 {
2871 *hs_buf = *(hs_buf + 1);
2872 }
2873
2874 /* Create a fresh last entry */
2875 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002876 }
2877#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002878}
2879
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002880/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002881 * DTLS anti-replay: RFC 6347 4.1.2.6
2882 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002883 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2884 * Bit n is set iff record number in_window_top - n has been seen.
2885 *
2886 * Usually, in_window_top is the last record number seen and the lsb of
2887 * in_window is set. The only exception is the initial state (record number 0
2888 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002891void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002892{
2893 ssl->in_window_top = 0;
2894 ssl->in_window = 0;
2895}
2896
2897static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2898{
2899 return( ( (uint64_t) buf[0] << 40 ) |
2900 ( (uint64_t) buf[1] << 32 ) |
2901 ( (uint64_t) buf[2] << 24 ) |
2902 ( (uint64_t) buf[3] << 16 ) |
2903 ( (uint64_t) buf[4] << 8 ) |
2904 ( (uint64_t) buf[5] ) );
2905}
2906
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002907static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2908{
Janos Follath865b3eb2019-12-16 11:46:15 +00002909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002910 unsigned char *original_in_ctr;
2911
2912 // save original in_ctr
2913 original_in_ctr = ssl->in_ctr;
2914
2915 // use counter from record
2916 ssl->in_ctr = record_in_ctr;
2917
2918 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2919
2920 // restore the counter
2921 ssl->in_ctr = original_in_ctr;
2922
2923 return ret;
2924}
2925
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002926/*
2927 * Return 0 if sequence number is acceptable, -1 otherwise
2928 */
Hanno Becker0183d692019-07-12 08:50:37 +01002929int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002930{
2931 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2932 uint64_t bit;
2933
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002934 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002935 return( 0 );
2936
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002937 if( rec_seqnum > ssl->in_window_top )
2938 return( 0 );
2939
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002940 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002941
2942 if( bit >= 64 )
2943 return( -1 );
2944
2945 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2946 return( -1 );
2947
2948 return( 0 );
2949}
2950
2951/*
2952 * Update replay window on new validated record
2953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002955{
2956 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2957
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002958 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002959 return;
2960
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002961 if( rec_seqnum > ssl->in_window_top )
2962 {
2963 /* Update window_top and the contents of the window */
2964 uint64_t shift = rec_seqnum - ssl->in_window_top;
2965
2966 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002967 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002968 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002969 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002970 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002971 ssl->in_window |= 1;
2972 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002973
2974 ssl->in_window_top = rec_seqnum;
2975 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002976 else
2977 {
2978 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002979 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002980
2981 if( bit < 64 ) /* Always true, but be extra sure */
2982 ssl->in_window |= (uint64_t) 1 << bit;
2983 }
2984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002986
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002987#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002988/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002989 * Without any SSL context, check if a datagram looks like a ClientHello with
2990 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002991 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002992 *
2993 * - if cookie is valid, return 0
2994 * - if ClientHello looks superficially valid but cookie is not,
2995 * fill obuf and set olen, then
2996 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
2997 * - otherwise return a specific error code
2998 */
2999static int ssl_check_dtls_clihlo_cookie(
3000 mbedtls_ssl_cookie_write_t *f_cookie_write,
3001 mbedtls_ssl_cookie_check_t *f_cookie_check,
3002 void *p_cookie,
3003 const unsigned char *cli_id, size_t cli_id_len,
3004 const unsigned char *in, size_t in_len,
3005 unsigned char *obuf, size_t buf_len, size_t *olen )
3006{
3007 size_t sid_len, cookie_len;
3008 unsigned char *p;
3009
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003010 /*
3011 * Structure of ClientHello with record and handshake headers,
3012 * and expected values. We don't need to check a lot, more checks will be
3013 * done when actually parsing the ClientHello - skipping those checks
3014 * avoids code duplication and does not make cookie forging any easier.
3015 *
3016 * 0-0 ContentType type; copied, must be handshake
3017 * 1-2 ProtocolVersion version; copied
3018 * 3-4 uint16 epoch; copied, must be 0
3019 * 5-10 uint48 sequence_number; copied
3020 * 11-12 uint16 length; (ignored)
3021 *
3022 * 13-13 HandshakeType msg_type; (ignored)
3023 * 14-16 uint24 length; (ignored)
3024 * 17-18 uint16 message_seq; copied
3025 * 19-21 uint24 fragment_offset; copied, must be 0
3026 * 22-24 uint24 fragment_length; (ignored)
3027 *
3028 * 25-26 ProtocolVersion client_version; (ignored)
3029 * 27-58 Random random; (ignored)
3030 * 59-xx SessionID session_id; 1 byte len + sid_len content
3031 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3032 * ...
3033 *
3034 * Minimum length is 61 bytes.
3035 */
3036 if( in_len < 61 ||
3037 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3038 in[3] != 0 || in[4] != 0 ||
3039 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3040 {
3041 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3042 }
3043
3044 sid_len = in[59];
3045 if( sid_len > in_len - 61 )
3046 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3047
3048 cookie_len = in[60 + sid_len];
3049 if( cookie_len > in_len - 60 )
3050 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3051
3052 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3053 cli_id, cli_id_len ) == 0 )
3054 {
3055 /* Valid cookie */
3056 return( 0 );
3057 }
3058
3059 /*
3060 * If we get here, we've got an invalid cookie, let's prepare HVR.
3061 *
3062 * 0-0 ContentType type; copied
3063 * 1-2 ProtocolVersion version; copied
3064 * 3-4 uint16 epoch; copied
3065 * 5-10 uint48 sequence_number; copied
3066 * 11-12 uint16 length; olen - 13
3067 *
3068 * 13-13 HandshakeType msg_type; hello_verify_request
3069 * 14-16 uint24 length; olen - 25
3070 * 17-18 uint16 message_seq; copied
3071 * 19-21 uint24 fragment_offset; copied
3072 * 22-24 uint24 fragment_length; olen - 25
3073 *
3074 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3075 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3076 *
3077 * Minimum length is 28.
3078 */
3079 if( buf_len < 28 )
3080 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3081
3082 /* Copy most fields and adapt others */
3083 memcpy( obuf, in, 25 );
3084 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3085 obuf[25] = 0xfe;
3086 obuf[26] = 0xff;
3087
3088 /* Generate and write actual cookie */
3089 p = obuf + 28;
3090 if( f_cookie_write( p_cookie,
3091 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3092 {
3093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3094 }
3095
3096 *olen = p - obuf;
3097
3098 /* Go back and fill length fields */
3099 obuf[27] = (unsigned char)( *olen - 28 );
3100
3101 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3102 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3103 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3104
3105 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3106 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3107
3108 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3109}
3110
3111/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003112 * Handle possible client reconnect with the same UDP quadruplet
3113 * (RFC 6347 Section 4.2.8).
3114 *
3115 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3116 * that looks like a ClientHello.
3117 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003118 * - if the input looks like a ClientHello without cookies,
3119 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003120 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003121 * - if the input looks like a ClientHello with a valid cookie,
3122 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003123 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003124 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003125 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003126 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003127 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3128 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003129 */
3130static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3131{
Janos Follath865b3eb2019-12-16 11:46:15 +00003132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003133 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003134
Hanno Becker2fddd372019-07-10 14:37:41 +01003135 if( ssl->conf->f_cookie_write == NULL ||
3136 ssl->conf->f_cookie_check == NULL )
3137 {
3138 /* If we can't use cookies to verify reachability of the peer,
3139 * drop the record. */
3140 return( 0 );
3141 }
3142
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003143 ret = ssl_check_dtls_clihlo_cookie(
3144 ssl->conf->f_cookie_write,
3145 ssl->conf->f_cookie_check,
3146 ssl->conf->p_cookie,
3147 ssl->cli_id, ssl->cli_id_len,
3148 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003149 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003150
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003151 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3152
3153 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003154 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003155 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003156 * If the error is permanent we'll catch it later,
3157 * if it's not, then hopefully it'll work next time. */
3158 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01003159 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003160 }
3161
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003162 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003163 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003164 /* Got a valid cookie, partially reset context */
Hanno Becker43aefe22020-02-05 10:44:56 +00003165 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003166 {
3167 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3168 return( ret );
3169 }
3170
3171 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003172 }
3173
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003174 return( ret );
3175}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003176#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003177
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003178static int ssl_check_record_type( uint8_t record_type )
3179{
3180 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3181 record_type != MBEDTLS_SSL_MSG_ALERT &&
3182 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3183 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3184 {
3185 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3186 }
3187
3188 return( 0 );
3189}
3190
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003191/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003192 * ContentType type;
3193 * ProtocolVersion version;
3194 * uint16 epoch; // DTLS only
3195 * uint48 sequence_number; // DTLS only
3196 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003197 *
3198 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003199 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003200 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3201 *
3202 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003203 * 1. proceed with the record if this function returns 0
3204 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3205 * 3. return CLIENT_RECONNECT if this function return that value
3206 * 4. drop the whole datagram if this function returns anything else.
3207 * Point 2 is needed when the peer is resending, and we have already received
3208 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003209 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003210static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003211 unsigned char *buf,
3212 size_t len,
3213 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003214{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003215 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003216
Hanno Beckere5e7e782019-07-11 12:29:35 +01003217 size_t const rec_hdr_type_offset = 0;
3218 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003219
Hanno Beckere5e7e782019-07-11 12:29:35 +01003220 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3221 rec_hdr_type_len;
3222 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
Hanno Beckere5e7e782019-07-11 12:29:35 +01003224 size_t const rec_hdr_ctr_len = 8;
3225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003226 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003227 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3228 rec_hdr_version_len;
3229
Hanno Beckera0e20d02019-05-15 14:03:01 +01003230#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003231 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3232 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003233 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003234#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3235#endif /* MBEDTLS_SSL_PROTO_DTLS */
3236
3237 size_t rec_hdr_len_offset; /* To be determined */
3238 size_t const rec_hdr_len_len = 2;
3239
3240 /*
3241 * Check minimum lengths for record header.
3242 */
3243
3244#if defined(MBEDTLS_SSL_PROTO_DTLS)
3245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3246 {
3247 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3248 }
3249 else
3250#endif /* MBEDTLS_SSL_PROTO_DTLS */
3251 {
3252 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3253 }
3254
3255 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3256 {
3257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3258 (unsigned) len,
3259 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3260 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3261 }
3262
3263 /*
3264 * Parse and validate record content type
3265 */
3266
3267 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003268
3269 /* Check record content type */
3270#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3271 rec->cid_len = 0;
3272
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003274 ssl->conf->cid_len != 0 &&
3275 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003276 {
3277 /* Shift pointers to account for record header including CID
3278 * struct {
3279 * ContentType special_type = tls12_cid;
3280 * ProtocolVersion version;
3281 * uint16 epoch;
3282 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003283 * opaque cid[cid_length]; // Additional field compared to
3284 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003285 * uint16 length;
3286 * opaque enc_content[DTLSCiphertext.length];
3287 * } DTLSCiphertext;
3288 */
3289
3290 /* So far, we only support static CID lengths
3291 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003292 rec_hdr_cid_len = ssl->conf->cid_len;
3293 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003294
Hanno Beckere5e7e782019-07-11 12:29:35 +01003295 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003296 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3298 (unsigned) len,
3299 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003300 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003301 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003302
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003303 /* configured CID len is guaranteed at most 255, see
3304 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3305 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003306 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003307 }
3308 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003309#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003310 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003311 if( ssl_check_record_type( rec->type ) )
3312 {
Hanno Becker54229812019-07-12 14:40:00 +01003313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3314 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3316 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003317 }
3318
Hanno Beckere5e7e782019-07-11 12:29:35 +01003319 /*
3320 * Parse and validate record version
3321 */
3322
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003323 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3324 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003325 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3326 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003327 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003328
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003329 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3332 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003335 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3338 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 }
3340
Hanno Beckere5e7e782019-07-11 12:29:35 +01003341 /*
3342 * Parse/Copy record sequence number.
3343 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003344
Hanno Beckere5e7e782019-07-11 12:29:35 +01003345#if defined(MBEDTLS_SSL_PROTO_DTLS)
3346 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003347 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003348 /* Copy explicit record sequence number from input buffer. */
3349 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3350 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003351 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003352 else
3353#endif /* MBEDTLS_SSL_PROTO_DTLS */
3354 {
3355 /* Copy implicit record sequence number from SSL context structure. */
3356 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3357 }
Paul Bakker40e46942009-01-03 21:51:57 +00003358
Hanno Beckere5e7e782019-07-11 12:29:35 +01003359 /*
3360 * Parse record length.
3361 */
3362
Hanno Beckere5e7e782019-07-11 12:29:35 +01003363 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003364 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3365 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003366 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003369 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003370 rec->type,
3371 major_ver, minor_ver, rec->data_len ) );
3372
3373 rec->buf = buf;
3374 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003375
Hanno Beckerd417cc92019-07-26 08:20:27 +01003376 if( rec->data_len == 0 )
3377 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003379 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003380 * DTLS-related tests.
3381 * Check epoch before checking length constraint because
3382 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3383 * message gets duplicated before the corresponding Finished message,
3384 * the second ChangeCipherSpec should be discarded because it belongs
3385 * to an old epoch, but not because its length is shorter than
3386 * the minimum record length for packets using the new record transform.
3387 * Note that these two kinds of failures are handled differently,
3388 * as an unexpected record is silently skipped but an invalid
3389 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003390 */
3391#if defined(MBEDTLS_SSL_PROTO_DTLS)
3392 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3393 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003394 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003395
Hanno Becker955a5c92019-07-10 17:12:07 +01003396 /* Check that the datagram is large enough to contain a record
3397 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003398 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003399 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3401 (unsigned) len,
3402 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3404 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003405
Hanno Becker37cfe732019-07-10 17:20:01 +01003406 /* Records from other, non-matching epochs are silently discarded.
3407 * (The case of same-port Client reconnects must be considered in
3408 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003409 if( rec_epoch != ssl->in_epoch )
3410 {
3411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3412 "expected %d, received %d",
3413 ssl->in_epoch, rec_epoch ) );
3414
Hanno Becker552f7472019-07-19 10:59:12 +01003415 /* Records from the next epoch are considered for buffering
3416 * (concretely: early Finished messages). */
3417 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003418 {
Hanno Becker552f7472019-07-19 10:59:12 +01003419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3420 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003421 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003422
Hanno Becker2fddd372019-07-10 14:37:41 +01003423 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003424 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003425#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003426 /* For records from the correct epoch, check whether their
3427 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003428 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3429 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003430 {
3431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3432 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3433 }
3434#endif
3435 }
3436#endif /* MBEDTLS_SSL_PROTO_DTLS */
3437
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003438 return( 0 );
3439}
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Hanno Becker2fddd372019-07-10 14:37:41 +01003442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3443static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3444{
3445 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3446
3447 /*
3448 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3449 * access the first byte of record content (handshake type), as we
3450 * have an active transform (possibly iv_len != 0), so use the
3451 * fact that the record header len is 13 instead.
3452 */
3453 if( rec_epoch == 0 &&
3454 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3455 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3456 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3457 ssl->in_left > 13 &&
3458 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3459 {
3460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3461 "from the same port" ) );
3462 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 }
3464
3465 return( 0 );
3466}
Hanno Becker2fddd372019-07-10 14:37:41 +01003467#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003468
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003469/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003470 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003471 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003472static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3473 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003474{
3475 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003478 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3481 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ret = mbedtls_ssl_hw_record_read( ssl );
3486 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3489 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003490 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003491
3492 if( ret == 0 )
3493 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003496 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003498 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003499
Hanno Beckera18d1322018-01-03 14:27:32 +00003500 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003501 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003504
Hanno Beckera0e20d02019-05-15 14:03:01 +01003505#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003506 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3507 ssl->conf->ignore_unexpected_cid
3508 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3509 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003511 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003512 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003513#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003514
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 return( ret );
3516 }
3517
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003518 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003519 {
3520 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003521 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003522 }
3523
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003524 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003525 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003526
Hanno Beckera0e20d02019-05-15 14:03:01 +01003527#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003528 /* We have already checked the record content type
3529 * in ssl_parse_record_header(), failing or silently
3530 * dropping the record in the case of an unknown type.
3531 *
3532 * Since with the use of CIDs, the record content type
3533 * might change during decryption, re-check the record
3534 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003535 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003536 {
3537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3538 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3539 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003540#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003541
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003542 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003543 {
3544#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3545 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003546 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003547 {
3548 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3551 }
3552#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3553
3554 ssl->nb_zero++;
3555
3556 /*
3557 * Three or more empty messages may be a DoS attack
3558 * (excessive CPU consumption).
3559 */
3560 if( ssl->nb_zero > 3 )
3561 {
3562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003563 "messages, possible DoS attack" ) );
3564 /* Treat the records as if they were not properly authenticated,
3565 * thereby failing the connection if we see more than allowed
3566 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003567 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3568 }
3569 }
3570 else
3571 ssl->nb_zero = 0;
3572
3573#if defined(MBEDTLS_SSL_PROTO_DTLS)
3574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3575 {
3576 ; /* in_ctr read from peer, not maintained internally */
3577 }
3578 else
3579#endif
3580 {
3581 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003582 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003583 if( ++ssl->in_ctr[i - 1] != 0 )
3584 break;
3585
3586 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003587 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003588 {
3589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3590 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3591 }
3592 }
3593
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 }
3595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003600 }
3601#endif
3602
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003603 /* Check actual (decrypted) record content length against
3604 * configured maximum. */
3605 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3606 {
3607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3608 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3609 }
3610
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003611 return( 0 );
3612}
3613
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003614/*
3615 * Read a record.
3616 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003617 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3618 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3619 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003620 */
Hanno Becker1097b342018-08-15 14:09:41 +01003621
3622/* Helper functions for mbedtls_ssl_read_record(). */
3623static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003624static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3625static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003626
Hanno Becker327c93b2018-08-15 13:56:18 +01003627int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003628 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003629{
Janos Follath865b3eb2019-12-16 11:46:15 +00003630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003633
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003634 if( ssl->keep_current_message == 0 )
3635 {
3636 do {
Simon Butcher99000142016-10-13 17:21:01 +01003637
Hanno Becker26994592018-08-15 14:14:59 +01003638 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003639 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003640 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003641
Hanno Beckere74d5562018-08-15 14:26:08 +01003642 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003643 {
Hanno Becker40f50842018-08-15 14:48:01 +01003644#if defined(MBEDTLS_SSL_PROTO_DTLS)
3645 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003646
Hanno Becker40f50842018-08-15 14:48:01 +01003647 /* We only check for buffered messages if the
3648 * current datagram is fully consumed. */
3649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003650 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003651 {
Hanno Becker40f50842018-08-15 14:48:01 +01003652 if( ssl_load_buffered_message( ssl ) == 0 )
3653 have_buffered = 1;
3654 }
3655
3656 if( have_buffered == 0 )
3657#endif /* MBEDTLS_SSL_PROTO_DTLS */
3658 {
3659 ret = ssl_get_next_record( ssl );
3660 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3661 continue;
3662
3663 if( ret != 0 )
3664 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003665 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003666 return( ret );
3667 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003668 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003669 }
3670
3671 ret = mbedtls_ssl_handle_message_type( ssl );
3672
Hanno Becker40f50842018-08-15 14:48:01 +01003673#if defined(MBEDTLS_SSL_PROTO_DTLS)
3674 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3675 {
3676 /* Buffer future message */
3677 ret = ssl_buffer_message( ssl );
3678 if( ret != 0 )
3679 return( ret );
3680
3681 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3682 }
3683#endif /* MBEDTLS_SSL_PROTO_DTLS */
3684
Hanno Becker90333da2017-10-10 11:27:13 +01003685 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3686 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003687
3688 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003689 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003690 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003691 return( ret );
3692 }
3693
Hanno Becker327c93b2018-08-15 13:56:18 +01003694 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003695 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003696 {
3697 mbedtls_ssl_update_handshake_status( ssl );
3698 }
Simon Butcher99000142016-10-13 17:21:01 +01003699 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003700 else
Simon Butcher99000142016-10-13 17:21:01 +01003701 {
Hanno Becker02f59072018-08-15 14:00:24 +01003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003703 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003704 }
3705
3706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3707
3708 return( 0 );
3709}
3710
Hanno Becker40f50842018-08-15 14:48:01 +01003711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003712static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003713{
Hanno Becker40f50842018-08-15 14:48:01 +01003714 if( ssl->in_left > ssl->next_record_offset )
3715 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003716
Hanno Becker40f50842018-08-15 14:48:01 +01003717 return( 0 );
3718}
3719
3720static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3721{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003722 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003723 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003724 int ret = 0;
3725
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003726 if( hs == NULL )
3727 return( -1 );
3728
Hanno Beckere00ae372018-08-20 09:39:42 +01003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3730
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003731 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3732 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3733 {
3734 /* Check if we have seen a ChangeCipherSpec before.
3735 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003736 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003737 {
3738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3739 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003740 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003741 }
3742
Hanno Becker39b8bc92018-08-28 17:17:13 +01003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003744 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3745 ssl->in_msglen = 1;
3746 ssl->in_msg[0] = 1;
3747
3748 /* As long as they are equal, the exact value doesn't matter. */
3749 ssl->in_left = 0;
3750 ssl->next_record_offset = 0;
3751
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003752 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003753 goto exit;
3754 }
Hanno Becker37f95322018-08-16 13:55:32 +01003755
Hanno Beckerb8f50142018-08-28 10:01:34 +01003756#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003757 /* Debug only */
3758 {
3759 unsigned offset;
3760 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3761 {
3762 hs_buf = &hs->buffering.hs[offset];
3763 if( hs_buf->is_valid == 1 )
3764 {
3765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3766 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003767 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003768 }
3769 }
3770 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003771#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003772
3773 /* Check if we have buffered and/or fully reassembled the
3774 * next handshake message. */
3775 hs_buf = &hs->buffering.hs[0];
3776 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3777 {
3778 /* Synthesize a record containing the buffered HS message. */
3779 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3780 ( hs_buf->data[2] << 8 ) |
3781 hs_buf->data[3];
3782
3783 /* Double-check that we haven't accidentally buffered
3784 * a message that doesn't fit into the input buffer. */
3785 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3789 }
3790
3791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3792 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3793 hs_buf->data, msg_len + 12 );
3794
3795 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3796 ssl->in_hslen = msg_len + 12;
3797 ssl->in_msglen = msg_len + 12;
3798 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3799
3800 ret = 0;
3801 goto exit;
3802 }
3803 else
3804 {
3805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3806 hs->in_msg_seq ) );
3807 }
3808
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003809 ret = -1;
3810
3811exit:
3812
3813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3814 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003815}
3816
Hanno Beckera02b0b42018-08-21 17:20:27 +01003817static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3818 size_t desired )
3819{
3820 int offset;
3821 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3823 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003824
Hanno Becker01315ea2018-08-21 17:22:17 +01003825 /* Get rid of future records epoch first, if such exist. */
3826 ssl_free_buffered_record( ssl );
3827
3828 /* Check if we have enough space available now. */
3829 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3830 hs->buffering.total_bytes_buffered ) )
3831 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003833 return( 0 );
3834 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003835
Hanno Becker4f432ad2018-08-28 10:02:32 +01003836 /* We don't have enough space to buffer the next expected handshake
3837 * message. Remove buffers used for future messages to gain space,
3838 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003839 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3840 offset >= 0; offset-- )
3841 {
3842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3843 offset ) );
3844
Hanno Beckerb309b922018-08-23 13:18:05 +01003845 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003846
3847 /* Check if we have enough space available now. */
3848 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3849 hs->buffering.total_bytes_buffered ) )
3850 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003852 return( 0 );
3853 }
3854 }
3855
3856 return( -1 );
3857}
3858
Hanno Becker40f50842018-08-15 14:48:01 +01003859static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3860{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003861 int ret = 0;
3862 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3863
3864 if( hs == NULL )
3865 return( 0 );
3866
3867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3868
3869 switch( ssl->in_msgtype )
3870 {
3871 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003873
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003874 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003875 break;
3876
3877 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003878 {
3879 unsigned recv_msg_seq_offset;
3880 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3881 mbedtls_ssl_hs_buffer *hs_buf;
3882 size_t msg_len = ssl->in_hslen - 12;
3883
3884 /* We should never receive an old handshake
3885 * message - double-check nonetheless. */
3886 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3887 {
3888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3890 }
3891
3892 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3893 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3894 {
3895 /* Silently ignore -- message too far in the future */
3896 MBEDTLS_SSL_DEBUG_MSG( 2,
3897 ( "Ignore future HS message with sequence number %u, "
3898 "buffering window %u - %u",
3899 recv_msg_seq, ssl->handshake->in_msg_seq,
3900 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3901
3902 goto exit;
3903 }
3904
3905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3906 recv_msg_seq, recv_msg_seq_offset ) );
3907
3908 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3909
3910 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003911 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003912 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003913 size_t reassembly_buf_sz;
3914
Hanno Becker37f95322018-08-16 13:55:32 +01003915 hs_buf->is_fragmented =
3916 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3917
3918 /* We copy the message back into the input buffer
3919 * after reassembly, so check that it's not too large.
3920 * This is an implementation-specific limitation
3921 * and not one from the standard, hence it is not
3922 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003923 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003924 {
3925 /* Ignore message */
3926 goto exit;
3927 }
3928
Hanno Beckere0b150f2018-08-21 15:51:03 +01003929 /* Check if we have enough space to buffer the message. */
3930 if( hs->buffering.total_bytes_buffered >
3931 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3932 {
3933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3935 }
3936
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003937 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3938 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003939
3940 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3941 hs->buffering.total_bytes_buffered ) )
3942 {
3943 if( recv_msg_seq_offset > 0 )
3944 {
3945 /* If we can't buffer a future message because
3946 * of space limitations -- ignore. */
3947 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",
3948 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
3949 (unsigned) hs->buffering.total_bytes_buffered ) );
3950 goto exit;
3951 }
Hanno Beckere1801392018-08-21 16:51:05 +01003952 else
3953 {
3954 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",
3955 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
3956 (unsigned) hs->buffering.total_bytes_buffered ) );
3957 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003958
Hanno Beckera02b0b42018-08-21 17:20:27 +01003959 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003960 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003961 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",
3962 (unsigned) msg_len,
3963 (unsigned) reassembly_buf_sz,
3964 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003965 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003966 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3967 goto exit;
3968 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003969 }
3970
3971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
3972 msg_len ) );
3973
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003974 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3975 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003976 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003977 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003978 goto exit;
3979 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003980 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003981
3982 /* Prepare final header: copy msg_type, length and message_seq,
3983 * then add standardised fragment_offset and fragment_length */
3984 memcpy( hs_buf->data, ssl->in_msg, 6 );
3985 memset( hs_buf->data + 6, 0, 3 );
3986 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3987
3988 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01003989
3990 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003991 }
3992 else
3993 {
3994 /* Make sure msg_type and length are consistent */
3995 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
3996 {
3997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
3998 /* Ignore */
3999 goto exit;
4000 }
4001 }
4002
Hanno Becker4422bbb2018-08-20 09:40:19 +01004003 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004004 {
4005 size_t frag_len, frag_off;
4006 unsigned char * const msg = hs_buf->data + 12;
4007
4008 /*
4009 * Check and copy current fragment
4010 */
4011
4012 /* Validation of header fields already done in
4013 * mbedtls_ssl_prepare_handshake_record(). */
4014 frag_off = ssl_get_hs_frag_off( ssl );
4015 frag_len = ssl_get_hs_frag_len( ssl );
4016
4017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4018 frag_off, frag_len ) );
4019 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4020
4021 if( hs_buf->is_fragmented )
4022 {
4023 unsigned char * const bitmask = msg + msg_len;
4024 ssl_bitmask_set( bitmask, frag_off, frag_len );
4025 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4026 msg_len ) == 0 );
4027 }
4028 else
4029 {
4030 hs_buf->is_complete = 1;
4031 }
4032
4033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4034 hs_buf->is_complete ? "" : "not yet " ) );
4035 }
4036
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004037 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004038 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004039
4040 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004041 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004042 break;
4043 }
4044
4045exit:
4046
4047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4048 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004049}
4050#endif /* MBEDTLS_SSL_PROTO_DTLS */
4051
Hanno Becker1097b342018-08-15 14:09:41 +01004052static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004053{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004054 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004055 * Consume last content-layer message and potentially
4056 * update in_msglen which keeps track of the contents'
4057 * consumption state.
4058 *
4059 * (1) Handshake messages:
4060 * Remove last handshake message, move content
4061 * and adapt in_msglen.
4062 *
4063 * (2) Alert messages:
4064 * Consume whole record content, in_msglen = 0.
4065 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004066 * (3) Change cipher spec:
4067 * Consume whole record content, in_msglen = 0.
4068 *
4069 * (4) Application data:
4070 * Don't do anything - the record layer provides
4071 * the application data as a stream transport
4072 * and consumes through mbedtls_ssl_read only.
4073 *
4074 */
4075
4076 /* Case (1): Handshake messages */
4077 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004078 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004079 /* Hard assertion to be sure that no application data
4080 * is in flight, as corrupting ssl->in_msglen during
4081 * ssl->in_offt != NULL is fatal. */
4082 if( ssl->in_offt != NULL )
4083 {
4084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4086 }
4087
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004088 /*
4089 * Get next Handshake message in the current record
4090 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004091
Hanno Becker4a810fb2017-05-24 16:27:30 +01004092 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004093 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004094 * current handshake content: If DTLS handshake
4095 * fragmentation is used, that's the fragment
4096 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004097 * size here is faulty and should be changed at
4098 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004099 * (2) While it doesn't seem to cause problems, one
4100 * has to be very careful not to assume that in_hslen
4101 * is always <= in_msglen in a sensible communication.
4102 * Again, it's wrong for DTLS handshake fragmentation.
4103 * The following check is therefore mandatory, and
4104 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004105 * Additionally, ssl->in_hslen might be arbitrarily out of
4106 * bounds after handling a DTLS message with an unexpected
4107 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004108 */
4109 if( ssl->in_hslen < ssl->in_msglen )
4110 {
4111 ssl->in_msglen -= ssl->in_hslen;
4112 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4113 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004114
Hanno Becker4a810fb2017-05-24 16:27:30 +01004115 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4116 ssl->in_msg, ssl->in_msglen );
4117 }
4118 else
4119 {
4120 ssl->in_msglen = 0;
4121 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004122
Hanno Becker4a810fb2017-05-24 16:27:30 +01004123 ssl->in_hslen = 0;
4124 }
4125 /* Case (4): Application data */
4126 else if( ssl->in_offt != NULL )
4127 {
4128 return( 0 );
4129 }
4130 /* Everything else (CCS & Alerts) */
4131 else
4132 {
4133 ssl->in_msglen = 0;
4134 }
4135
Hanno Becker1097b342018-08-15 14:09:41 +01004136 return( 0 );
4137}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004138
Hanno Beckere74d5562018-08-15 14:26:08 +01004139static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4140{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004141 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004142 return( 1 );
4143
4144 return( 0 );
4145}
4146
Hanno Becker5f066e72018-08-16 14:56:31 +01004147#if defined(MBEDTLS_SSL_PROTO_DTLS)
4148
4149static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4150{
4151 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4152 if( hs == NULL )
4153 return;
4154
Hanno Becker01315ea2018-08-21 17:22:17 +01004155 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004156 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004157 hs->buffering.total_bytes_buffered -=
4158 hs->buffering.future_record.len;
4159
4160 mbedtls_free( hs->buffering.future_record.data );
4161 hs->buffering.future_record.data = NULL;
4162 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004163}
4164
4165static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4166{
4167 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4168 unsigned char * rec;
4169 size_t rec_len;
4170 unsigned rec_epoch;
4171
4172 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4173 return( 0 );
4174
4175 if( hs == NULL )
4176 return( 0 );
4177
Hanno Becker5f066e72018-08-16 14:56:31 +01004178 rec = hs->buffering.future_record.data;
4179 rec_len = hs->buffering.future_record.len;
4180 rec_epoch = hs->buffering.future_record.epoch;
4181
4182 if( rec == NULL )
4183 return( 0 );
4184
Hanno Becker4cb782d2018-08-20 11:19:05 +01004185 /* Only consider loading future records if the
4186 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004187 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004188 return( 0 );
4189
Hanno Becker5f066e72018-08-16 14:56:31 +01004190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4191
4192 if( rec_epoch != ssl->in_epoch )
4193 {
4194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4195 goto exit;
4196 }
4197
4198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4199
4200 /* Double-check that the record is not too large */
4201 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4202 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4203 {
4204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4206 }
4207
4208 memcpy( ssl->in_hdr, rec, rec_len );
4209 ssl->in_left = rec_len;
4210 ssl->next_record_offset = 0;
4211
4212 ssl_free_buffered_record( ssl );
4213
4214exit:
4215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4216 return( 0 );
4217}
4218
Hanno Becker519f15d2019-07-11 12:43:20 +01004219static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4220 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004221{
4222 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004223
4224 /* Don't buffer future records outside handshakes. */
4225 if( hs == NULL )
4226 return( 0 );
4227
4228 /* Only buffer handshake records (we are only interested
4229 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004230 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004231 return( 0 );
4232
4233 /* Don't buffer more than one future epoch record. */
4234 if( hs->buffering.future_record.data != NULL )
4235 return( 0 );
4236
Hanno Becker01315ea2018-08-21 17:22:17 +01004237 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004238 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004239 hs->buffering.total_bytes_buffered ) )
4240 {
4241 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 +01004242 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004243 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004244 return( 0 );
4245 }
4246
Hanno Becker5f066e72018-08-16 14:56:31 +01004247 /* Buffer record */
4248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4249 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004250 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004251
4252 /* ssl_parse_record_header() only considers records
4253 * of the next epoch as candidates for buffering. */
4254 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004255 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004256
4257 hs->buffering.future_record.data =
4258 mbedtls_calloc( 1, hs->buffering.future_record.len );
4259 if( hs->buffering.future_record.data == NULL )
4260 {
4261 /* If we run out of RAM trying to buffer a
4262 * record from the next epoch, just ignore. */
4263 return( 0 );
4264 }
4265
Hanno Becker519f15d2019-07-11 12:43:20 +01004266 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004267
Hanno Becker519f15d2019-07-11 12:43:20 +01004268 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004269 return( 0 );
4270}
4271
4272#endif /* MBEDTLS_SSL_PROTO_DTLS */
4273
Hanno Beckere74d5562018-08-15 14:26:08 +01004274static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004275{
Janos Follath865b3eb2019-12-16 11:46:15 +00004276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004277 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004278
Hanno Becker5f066e72018-08-16 14:56:31 +01004279#if defined(MBEDTLS_SSL_PROTO_DTLS)
4280 /* We might have buffered a future record; if so,
4281 * and if the epoch matches now, load it.
4282 * On success, this call will set ssl->in_left to
4283 * the length of the buffered record, so that
4284 * the calls to ssl_fetch_input() below will
4285 * essentially be no-ops. */
4286 ret = ssl_load_buffered_record( ssl );
4287 if( ret != 0 )
4288 return( ret );
4289#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004290
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004291 /* Ensure that we have enough space available for the default form
4292 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4293 * with no space for CIDs counted in). */
4294 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4295 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004298 return( ret );
4299 }
4300
Hanno Beckere5e7e782019-07-11 12:29:35 +01004301 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4302 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004306 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004307 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4308 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004309 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004310 if( ret != 0 )
4311 return( ret );
4312
4313 /* Fall through to handling of unexpected records */
4314 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4315 }
4316
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004317 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4318 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004319#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004320 /* Reset in pointers to default state for TLS/DTLS records,
4321 * assuming no CID and no offset between record content and
4322 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004323 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004324
Hanno Becker7ae20e02019-07-12 08:33:49 +01004325 /* Setup internal message pointers from record structure. */
4326 ssl->in_msgtype = rec.type;
4327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4328 ssl->in_len = ssl->in_cid + rec.cid_len;
4329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4330 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4331 ssl->in_msglen = rec.data_len;
4332
Hanno Becker2fddd372019-07-10 14:37:41 +01004333 ret = ssl_check_client_reconnect( ssl );
4334 if( ret != 0 )
4335 return( ret );
4336#endif
4337
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004338 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004339 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004340
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4342 "(header)" ) );
4343 }
4344 else
4345 {
4346 /* Skip invalid record and the rest of the datagram */
4347 ssl->next_record_offset = 0;
4348 ssl->in_left = 0;
4349
4350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4351 "(header)" ) );
4352 }
4353
4354 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004355 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004356 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004357 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004358#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004359 {
4360 return( ret );
4361 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004362 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004366 {
Hanno Beckera8814792019-07-10 15:01:45 +01004367 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004368 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004369 if( ssl->next_record_offset < ssl->in_left )
4370 {
4371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4372 }
4373 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004374 else
4375#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004376 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004377 /*
4378 * Fetch record contents from underlying transport.
4379 */
Hanno Beckera3175662019-07-11 12:50:29 +01004380 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004381 if( ret != 0 )
4382 {
4383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4384 return( ret );
4385 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004386
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004387 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004388 }
4389
4390 /*
4391 * Decrypt record contents.
4392 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004393
Hanno Beckerfdf66042019-07-11 13:07:45 +01004394 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004397 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004398 {
4399 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004400 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004401 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004402 /* Except when waiting for Finished as a bad mac here
4403 * probably means something went wrong in the handshake
4404 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4405 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4406 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4407 {
4408#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4409 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4410 {
4411 mbedtls_ssl_send_alert_message( ssl,
4412 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4413 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4414 }
4415#endif
4416 return( ret );
4417 }
4418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004420 if( ssl->conf->badmac_limit != 0 &&
4421 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004425 }
4426#endif
4427
Hanno Becker4a810fb2017-05-24 16:27:30 +01004428 /* As above, invalid records cause
4429 * dismissal of the whole datagram. */
4430
4431 ssl->next_record_offset = 0;
4432 ssl->in_left = 0;
4433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004435 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004436 }
4437
4438 return( ret );
4439 }
4440 else
4441#endif
4442 {
4443 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4445 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 mbedtls_ssl_send_alert_message( ssl,
4448 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4449 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004450 }
4451#endif
4452 return( ret );
4453 }
4454 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004455
Hanno Becker44d89b22019-07-12 09:40:44 +01004456
4457 /* Reset in pointers to default state for TLS/DTLS records,
4458 * assuming no CID and no offset between record content and
4459 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004460 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4462 ssl->in_len = ssl->in_cid + rec.cid_len;
4463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004464 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004465
Hanno Becker8685c822019-07-12 09:37:30 +01004466 /* The record content type may change during decryption,
4467 * so re-read it. */
4468 ssl->in_msgtype = rec.type;
4469 /* Also update the input buffer, because unfortunately
4470 * the server-side ssl_parse_client_hello() reparses the
4471 * record header when receiving a ClientHello initiating
4472 * a renegotiation. */
4473 ssl->in_hdr[0] = rec.type;
4474 ssl->in_msg = rec.buf + rec.data_offset;
4475 ssl->in_msglen = rec.data_len;
4476 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4477 ssl->in_len[1] = (unsigned char)( rec.data_len );
4478
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004479#if defined(MBEDTLS_ZLIB_SUPPORT)
4480 if( ssl->transform_in != NULL &&
4481 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4482 {
4483 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4484 {
4485 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4486 return( ret );
4487 }
4488
4489 /* Check actual (decompress) record content length against
4490 * configured maximum. */
4491 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4492 {
4493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4494 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4495 }
4496 }
4497#endif /* MBEDTLS_ZLIB_SUPPORT */
4498
Simon Butcher99000142016-10-13 17:21:01 +01004499 return( 0 );
4500}
4501
4502int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4503{
Janos Follath865b3eb2019-12-16 11:46:15 +00004504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004505
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004506 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004507 * Handle particular types of records
4508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004510 {
Simon Butcher99000142016-10-13 17:21:01 +01004511 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4512 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004513 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 }
4516
Hanno Beckere678eaa2018-08-21 14:57:46 +01004517 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004518 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004519 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004520 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4522 ssl->in_msglen ) );
4523 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004524 }
4525
Hanno Beckere678eaa2018-08-21 14:57:46 +01004526 if( ssl->in_msg[0] != 1 )
4527 {
4528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4529 ssl->in_msg[0] ) );
4530 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4531 }
4532
4533#if defined(MBEDTLS_SSL_PROTO_DTLS)
4534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4535 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4536 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4537 {
4538 if( ssl->handshake == NULL )
4539 {
4540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4542 }
4543
4544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4545 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4546 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004547#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004548 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004552 if( ssl->in_msglen != 2 )
4553 {
4554 /* Note: Standard allows for more than one 2 byte alert
4555 to be packed in a single message, but Mbed TLS doesn't
4556 currently support this. */
4557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4558 ssl->in_msglen ) );
4559 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4560 }
4561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 ssl->in_msg[0], ssl->in_msg[1] ) );
4564
4565 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004566 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004571 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 }
4574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4576 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4579 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004581
4582#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4583 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4584 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4585 {
Hanno Becker90333da2017-10-10 11:27:13 +01004586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004587 /* Will be handled when trying to parse ServerHello */
4588 return( 0 );
4589 }
4590#endif
4591
4592#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4593 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4594 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4595 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4596 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4597 {
4598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4599 /* Will be handled in mbedtls_ssl_parse_certificate() */
4600 return( 0 );
4601 }
4602#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4603
4604 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004605 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 }
4607
Hanno Beckerc76c6192017-06-06 10:03:17 +01004608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004610 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004611 /* Drop unexpected ApplicationData records,
4612 * except at the beginning of renegotiations */
4613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4614 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4615#if defined(MBEDTLS_SSL_RENEGOTIATION)
4616 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4617 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004618#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004619 )
4620 {
4621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4622 return( MBEDTLS_ERR_SSL_NON_FATAL );
4623 }
4624
4625 if( ssl->handshake != NULL &&
4626 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4627 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004628 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004629 }
4630 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004631#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004632
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 return( 0 );
4634}
4635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004637{
irwir6c0da642019-09-26 21:07:41 +03004638 return( mbedtls_ssl_send_alert_message( ssl,
4639 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4640 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004641}
4642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004644 unsigned char level,
4645 unsigned char message )
4646{
Janos Follath865b3eb2019-12-16 11:46:15 +00004647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004649 if( ssl == NULL || ssl->conf == NULL )
4650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004656 ssl->out_msglen = 2;
4657 ssl->out_msg[0] = level;
4658 ssl->out_msg[1] = message;
4659
Hanno Becker67bc7c32018-08-06 11:33:50 +01004660 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004663 return( ret );
4664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004666
4667 return( 0 );
4668}
4669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004671{
Janos Follath865b3eb2019-12-16 11:46:15 +00004672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 ssl->out_msglen = 1;
4678 ssl->out_msg[0] = 1;
4679
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 ssl->state++;
4681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004682 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 return( ret );
4686 }
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004689
4690 return( 0 );
4691}
4692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004694{
Janos Follath865b3eb2019-12-16 11:46:15 +00004695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004698
Hanno Becker327c93b2018-08-15 13:56:18 +01004699 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 return( ret );
4703 }
4704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4709 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 }
4712
Hanno Beckere678eaa2018-08-21 14:57:46 +01004713 /* CCS records are only accepted if they have length 1 and content '1',
4714 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004716 /*
4717 * Switch to our negotiated transform and session parameters for inbound
4718 * data.
4719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004721 ssl->transform_in = ssl->transform_negotiate;
4722 ssl->session_in = ssl->session_negotiate;
4723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004728 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004729#endif
4730
4731 /* Increment epoch */
4732 if( ++ssl->in_epoch == 0 )
4733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004735 /* This is highly unlikely to happen for legitimate reasons, so
4736 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004738 }
4739 }
4740 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004742 memset( ssl->in_ctr, 0, 8 );
4743
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004744 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4747 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4753 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004755 }
4756 }
4757#endif
4758
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 ssl->state++;
4760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004762
4763 return( 0 );
4764}
4765
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004766/* Once ssl->out_hdr as the address of the beginning of the
4767 * next outgoing record is set, deduce the other pointers.
4768 *
4769 * Note: For TLS, we save the implicit record sequence number
4770 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4771 * and the caller has to make sure there's space for this.
4772 */
4773
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004774void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4775 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004776{
4777#if defined(MBEDTLS_SSL_PROTO_DTLS)
4778 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4779 {
4780 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004781#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004782 ssl->out_cid = ssl->out_ctr + 8;
4783 ssl->out_len = ssl->out_cid;
4784 if( transform != NULL )
4785 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004786#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004787 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004788#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004789 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004790 }
4791 else
4792#endif
4793 {
4794 ssl->out_ctr = ssl->out_hdr - 8;
4795 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004796#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004797 ssl->out_cid = ssl->out_len;
4798#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004799 ssl->out_iv = ssl->out_hdr + 5;
4800 }
4801
4802 /* Adjust out_msg to make space for explicit IV, if used. */
4803 if( transform != NULL &&
4804 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
4805 {
4806 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
4807 }
4808 else
4809 ssl->out_msg = ssl->out_iv;
4810}
4811
4812/* Once ssl->in_hdr as the address of the beginning of the
4813 * next incoming record is set, deduce the other pointers.
4814 *
4815 * Note: For TLS, we save the implicit record sequence number
4816 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4817 * and the caller has to make sure there's space for this.
4818 */
4819
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004820void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004821{
Hanno Becker79594fd2019-05-08 09:38:41 +01004822 /* This function sets the pointers to match the case
4823 * of unprotected TLS/DTLS records, with both ssl->in_iv
4824 * and ssl->in_msg pointing to the beginning of the record
4825 * content.
4826 *
4827 * When decrypting a protected record, ssl->in_msg
4828 * will be shifted to point to the beginning of the
4829 * record plaintext.
4830 */
4831
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004832#if defined(MBEDTLS_SSL_PROTO_DTLS)
4833 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4834 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004835 /* This sets the header pointers to match records
4836 * without CID. When we receive a record containing
4837 * a CID, the fields are shifted accordingly in
4838 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004839 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004840#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004841 ssl->in_cid = ssl->in_ctr + 8;
4842 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004843#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004844 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004845#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004846 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004847 }
4848 else
4849#endif
4850 {
4851 ssl->in_ctr = ssl->in_hdr - 8;
4852 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004853#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004854 ssl->in_cid = ssl->in_len;
4855#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004856 ssl->in_iv = ssl->in_hdr + 5;
4857 }
4858
Hanno Becker79594fd2019-05-08 09:38:41 +01004859 /* This will be adjusted at record decryption time. */
4860 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004861}
4862
Paul Bakker5121ce52009-01-03 21:22:43 +00004863/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004864 * Setup an SSL context
4865 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004866
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004867void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004868{
4869 /* Set the incoming and outgoing record pointers. */
4870#if defined(MBEDTLS_SSL_PROTO_DTLS)
4871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4872 {
4873 ssl->out_hdr = ssl->out_buf;
4874 ssl->in_hdr = ssl->in_buf;
4875 }
4876 else
4877#endif /* MBEDTLS_SSL_PROTO_DTLS */
4878 {
4879 ssl->out_hdr = ssl->out_buf + 8;
4880 ssl->in_hdr = ssl->in_buf + 8;
4881 }
4882
4883 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004884 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4885 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004886}
4887
Paul Bakker5121ce52009-01-03 21:22:43 +00004888/*
4889 * SSL get accessors
4890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892{
4893 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4894}
4895
Hanno Becker8b170a02017-10-10 11:51:19 +01004896int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4897{
4898 /*
4899 * Case A: We're currently holding back
4900 * a message for further processing.
4901 */
4902
4903 if( ssl->keep_current_message == 1 )
4904 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004906 return( 1 );
4907 }
4908
4909 /*
4910 * Case B: Further records are pending in the current datagram.
4911 */
4912
4913#if defined(MBEDTLS_SSL_PROTO_DTLS)
4914 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4915 ssl->in_left > ssl->next_record_offset )
4916 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004918 return( 1 );
4919 }
4920#endif /* MBEDTLS_SSL_PROTO_DTLS */
4921
4922 /*
4923 * Case C: A handshake message is being processed.
4924 */
4925
Hanno Becker8b170a02017-10-10 11:51:19 +01004926 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4927 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004929 return( 1 );
4930 }
4931
4932 /*
4933 * Case D: An application data message is being processed
4934 */
4935 if( ssl->in_offt != NULL )
4936 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004938 return( 1 );
4939 }
4940
4941 /*
4942 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004943 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004944 * we implement support for multiple alerts in single records.
4945 */
4946
4947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4948 return( 0 );
4949}
4950
Paul Bakker43ca69c2011-01-15 17:35:19 +00004951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004953{
Hanno Becker3136ede2018-08-17 15:28:19 +01004954 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004956 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004957
Hanno Becker5903de42019-05-03 14:46:38 +01004958 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4959
Hanno Becker78640902018-08-13 16:35:15 +01004960 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004961 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_ZLIB_SUPPORT)
4964 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
4965 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004966#endif
4967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_MODE_GCM:
4971 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004972 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004974 transform_expansion = transform->minlen;
4975 break;
4976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004978
4979 block_size = mbedtls_cipher_get_block_size(
4980 &transform->cipher_ctx_enc );
4981
Hanno Becker3136ede2018-08-17 15:28:19 +01004982 /* Expansion due to the addition of the MAC. */
4983 transform_expansion += transform->maclen;
4984
4985 /* Expansion due to the addition of CBC padding;
4986 * Theoretically up to 256 bytes, but we never use
4987 * more than the block size of the underlying cipher. */
4988 transform_expansion += block_size;
4989
4990 /* For TLS 1.1 or higher, an explicit IV is added
4991 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01004992#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
4993 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01004994 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004995#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004996
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004997 break;
4998
4999 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005002 }
5003
Hanno Beckera0e20d02019-05-15 14:03:01 +01005004#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005005 if( transform->out_cid_len != 0 )
5006 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005007#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005008
Hanno Becker5903de42019-05-03 14:46:38 +01005009 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005010}
5011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005013/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005014 * Check record counters and renegotiate if they're above the limit.
5015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005017{
Hanno Beckerdd772292020-02-05 10:38:31 +00005018 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005019 int in_ctr_cmp;
5020 int out_ctr_cmp;
5021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5023 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005024 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005025 {
5026 return( 0 );
5027 }
5028
Andres AG2196c7f2016-12-15 17:01:16 +00005029 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5030 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005031 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005032 ssl->conf->renego_period + ep_len, 8 - ep_len );
5033
5034 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005035 {
5036 return( 0 );
5037 }
5038
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005043
5044/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005045 * Receive application data decrypted from the SSL layer
5046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005048{
Janos Follath865b3eb2019-12-16 11:46:15 +00005049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005050 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005051
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005052 if( ssl == NULL || ssl->conf == NULL )
5053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005061 return( ret );
5062
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005063 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005065 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005066 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005067 return( ret );
5068 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005069 }
5070#endif
5071
Hanno Becker4a810fb2017-05-24 16:27:30 +01005072 /*
5073 * Check if renegotiation is necessary and/or handshake is
5074 * in process. If yes, perform/continue, and fall through
5075 * if an unexpected packet is received while the client
5076 * is waiting for the ServerHello.
5077 *
5078 * (There is no equivalent to the last condition on
5079 * the server-side as it is not treated as within
5080 * a handshake while waiting for the ClientHello
5081 * after a renegotiation request.)
5082 */
5083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005085 ret = ssl_check_ctr_renegotiate( ssl );
5086 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5087 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005090 return( ret );
5091 }
5092#endif
5093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005097 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5098 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 return( ret );
5102 }
5103 }
5104
Hanno Beckere41158b2017-10-23 13:30:32 +01005105 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005106 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005108 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005109 if( ssl->f_get_timer != NULL &&
5110 ssl->f_get_timer( ssl->p_timer ) == -1 )
5111 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005112 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005113 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005114
Hanno Becker327c93b2018-08-15 13:56:18 +01005115 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005117 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5118 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005119
Hanno Becker4a810fb2017-05-24 16:27:30 +01005120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5121 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122 }
5123
5124 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005126 {
5127 /*
5128 * OpenSSL sends empty messages to randomize the IV
5129 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005130 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005133 return( 0 );
5134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136 return( ret );
5137 }
5138 }
5139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005143
Hanno Becker4a810fb2017-05-24 16:27:30 +01005144 /*
5145 * - For client-side, expect SERVER_HELLO_REQUEST.
5146 * - For server-side, expect CLIENT_HELLO.
5147 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5148 */
5149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005151 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005153 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005156
5157 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005159 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005160 {
5161 continue;
5162 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005165 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005166#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005167
Hanno Becker4a810fb2017-05-24 16:27:30 +01005168#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005169 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005173
5174 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005176 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005177 {
5178 continue;
5179 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005182 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005183#endif /* MBEDTLS_SSL_SRV_C */
5184
Hanno Becker21df7f92017-10-17 11:03:26 +01005185#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005186 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005187 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5188 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5189 ssl->conf->allow_legacy_renegotiation ==
5190 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5191 {
5192 /*
5193 * Accept renegotiation request
5194 */
Paul Bakker48916f92012-09-16 19:57:18 +00005195
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005196 /* DTLS clients need to know renego is server-initiated */
5197#if defined(MBEDTLS_SSL_PROTO_DTLS)
5198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5199 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5200 {
5201 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5202 }
5203#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005204 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005205 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5206 ret != 0 )
5207 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5209 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005210 return( ret );
5211 }
5212 }
5213 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005214#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005215 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005216 /*
5217 * Refuse renegotiation
5218 */
5219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_SSL_PROTO_SSL3)
5223 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005224 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005225 /* SSLv3 does not have a "no_renegotiation" warning, so
5226 we send a fatal alert and abort the connection. */
5227 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5228 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5229 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005230 }
5231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5233#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5234 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5235 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5238 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5239 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005240 {
5241 return( ret );
5242 }
Paul Bakker48916f92012-09-16 19:57:18 +00005243 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5246 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005250 }
Paul Bakker48916f92012-09-16 19:57:18 +00005251 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005252
Hanno Becker90333da2017-10-10 11:27:13 +01005253 /* At this point, we don't know whether the renegotiation has been
5254 * completed or not. The cases to consider are the following:
5255 * 1) The renegotiation is complete. In this case, no new record
5256 * has been read yet.
5257 * 2) The renegotiation is incomplete because the client received
5258 * an application data record while awaiting the ServerHello.
5259 * 3) The renegotiation is incomplete because the client received
5260 * a non-handshake, non-application data message while awaiting
5261 * the ServerHello.
5262 * In each of these case, looping will be the proper action:
5263 * - For 1), the next iteration will read a new record and check
5264 * if it's application data.
5265 * - For 2), the loop condition isn't satisfied as application data
5266 * is present, hence continue is the same as break
5267 * - For 3), the loop condition is satisfied and read_record
5268 * will re-deliver the message that was held back by the client
5269 * when expecting the ServerHello.
5270 */
5271 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005272 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005273#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005275 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005276 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005277 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005278 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005281 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005283 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005284 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5289 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005292 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005293 }
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5298 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 }
5300
5301 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005302
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005303 /* We're going to return something now, cancel timer,
5304 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005306 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005307
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005309 /* If we requested renego but received AppData, resend HelloRequest.
5310 * Do it now, after setting in_offt, to avoid taking this branch
5311 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005315 {
Hanno Becker786300f2020-02-05 10:46:40 +00005316 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005317 {
Hanno Becker786300f2020-02-05 10:46:40 +00005318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5319 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005320 return( ret );
5321 }
5322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005324#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005325 }
5326
5327 n = ( len < ssl->in_msglen )
5328 ? len : ssl->in_msglen;
5329
5330 memcpy( buf, ssl->in_offt, n );
5331 ssl->in_msglen -= n;
5332
5333 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005334 {
5335 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005337 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005340 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005341 /* more data available */
5342 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005346
Paul Bakker23986e52011-04-24 08:57:21 +00005347 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005348}
5349
5350/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005351 * Send application data to be encrypted by the SSL layer, taking care of max
5352 * fragment length and buffer size.
5353 *
5354 * According to RFC 5246 Section 6.2.1:
5355 *
5356 * Zero-length fragments of Application data MAY be sent as they are
5357 * potentially useful as a traffic analysis countermeasure.
5358 *
5359 * Therefore, it is possible that the input message length is 0 and the
5360 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005361 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005362static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005363 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005364{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005365 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5366 const size_t max_len = (size_t) ret;
5367
5368 if( ret < 0 )
5369 {
5370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5371 return( ret );
5372 }
5373
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005374 if( len > max_len )
5375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005377 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005380 "maximum fragment length: %d > %d",
5381 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005383 }
5384 else
5385#endif
5386 len = max_len;
5387 }
Paul Bakker887bd502011-06-08 13:10:54 +00005388
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 if( ssl->out_left != 0 )
5390 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005391 /*
5392 * The user has previously tried to send the data and
5393 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5394 * written. In this case, we expect the high-level write function
5395 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005400 return( ret );
5401 }
5402 }
Paul Bakker887bd502011-06-08 13:10:54 +00005403 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005404 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005405 /*
5406 * The user is trying to send a message the first time, so we need to
5407 * copy the data into the internal buffers and setup the data structure
5408 * to keep track of partial writes
5409 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005410 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005412 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005413
Hanno Becker67bc7c32018-08-06 11:33:50 +01005414 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005417 return( ret );
5418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 }
5420
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005421 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005422}
5423
5424/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005425 * Write application data, doing 1/n-1 splitting if necessary.
5426 *
5427 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005428 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005429 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005432static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005433 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005434{
Janos Follath865b3eb2019-12-16 11:46:15 +00005435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005436
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005437 if( ssl->conf->cbc_record_splitting ==
5438 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005439 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5441 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5442 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005443 {
5444 return( ssl_write_real( ssl, buf, len ) );
5445 }
5446
5447 if( ssl->split_done == 0 )
5448 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005449 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005450 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005451 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005452 }
5453
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005454 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5455 return( ret );
5456 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005457
5458 return( ret + 1 );
5459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005461
5462/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005463 * Write application data (public-facing wrapper)
5464 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005465int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005466{
Janos Follath865b3eb2019-12-16 11:46:15 +00005467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005468
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005470
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005471 if( ssl == NULL || ssl->conf == NULL )
5472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5473
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005474#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005475 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5476 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005477 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005478 return( ret );
5479 }
5480#endif
5481
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005482 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005483 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005484 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005485 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005487 return( ret );
5488 }
5489 }
5490
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005491#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005492 ret = ssl_write_split( ssl, buf, len );
5493#else
5494 ret = ssl_write_real( ssl, buf, len );
5495#endif
5496
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005498
5499 return( ret );
5500}
5501
5502/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 * Notify the peer that the connection is being closed
5504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506{
Janos Follath865b3eb2019-12-16 11:46:15 +00005507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005509 if( ssl == NULL || ssl->conf == NULL )
5510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005514 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5520 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5521 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005524 return( ret );
5525 }
5526 }
5527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005530 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005531}
5532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005534{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005535 if( transform == NULL )
5536 return;
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005539 deflateEnd( &transform->ctx_deflate );
5540 inflateEnd( &transform->ctx_inflate );
5541#endif
5542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5544 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005545
Hanno Beckerd56ed242018-01-03 15:32:51 +00005546#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 mbedtls_md_free( &transform->md_ctx_enc );
5548 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005549#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005550
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005551 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005552}
5553
Hanno Becker0271f962018-08-16 13:23:47 +01005554#if defined(MBEDTLS_SSL_PROTO_DTLS)
5555
Hanno Becker533ab5f2020-02-05 10:49:13 +00005556void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005557{
5558 unsigned offset;
5559 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5560
5561 if( hs == NULL )
5562 return;
5563
Hanno Becker283f5ef2018-08-24 09:34:47 +01005564 ssl_free_buffered_record( ssl );
5565
Hanno Becker0271f962018-08-16 13:23:47 +01005566 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005567 ssl_buffering_free_slot( ssl, offset );
5568}
5569
5570static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5571 uint8_t slot )
5572{
5573 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5574 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005575
5576 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5577 return;
5578
Hanno Beckere605b192018-08-21 15:59:07 +01005579 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005580 {
Hanno Beckere605b192018-08-21 15:59:07 +01005581 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005582 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005583 mbedtls_free( hs_buf->data );
5584 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005585 }
5586}
5587
5588#endif /* MBEDTLS_SSL_PROTO_DTLS */
5589
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005590/*
5591 * Convert version numbers to/from wire format
5592 * and, for DTLS, to/from TLS equivalent.
5593 *
5594 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005595 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005596 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5597 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005600 unsigned char ver[2] )
5601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602#if defined(MBEDTLS_SSL_PROTO_DTLS)
5603 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005606 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5607
5608 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5609 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5610 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005611 else
5612#else
5613 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005614#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005615 {
5616 ver[0] = (unsigned char) major;
5617 ver[1] = (unsigned char) minor;
5618 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005619}
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005622 const unsigned char ver[2] )
5623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624#if defined(MBEDTLS_SSL_PROTO_DTLS)
5625 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005626 {
5627 *major = 255 - ver[0] + 2;
5628 *minor = 255 - ver[1] + 1;
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005631 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5632 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005633 else
5634#else
5635 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005636#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005637 {
5638 *major = ver[0];
5639 *minor = ver[1];
5640 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005641}
5642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643#endif /* MBEDTLS_SSL_TLS_C */