blob: 0d74e6d827a6bf2daf50cd5c25aec6bfe9df13ab [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Hanno Beckerf1a38282020-02-05 16:14:29 +00005 * Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000019 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000020 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Gilles Peskinedb09ef62020-06-03 01:43:33 +020031#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
SimonBd5800b72016-04-26 07:43:27 +010035#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#include <stdlib.h>
39#define mbedtls_calloc calloc
40#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010041#endif
42
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020044#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "mbedtls/psa_util.h"
54#include "psa/crypto.h"
55#endif
56
Janos Follath23bdca02016-10-07 14:47:14 +010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020059#endif
60
Hanno Beckercd9dcda2018-08-28 17:18:56 +010061static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020063/*
64 * Start a timer.
65 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020066 */
Hanno Becker0f57a652020-02-05 10:37:26 +000067void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020068{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020069 if( ssl->f_set_timer == NULL )
70 return;
71
72 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
73 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074}
75
76/*
77 * Return -1 is timer is expired, 0 if it isn't.
78 */
Hanno Becker7876d122020-02-05 10:39:31 +000079int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020081 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020082 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083
84 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089
90 return( 0 );
91}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092
Hanno Beckercfe45792019-07-03 16:13:00 +010093#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010094static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
95 unsigned char *buf,
96 size_t len,
97 mbedtls_record *rec );
98
Hanno Beckercfe45792019-07-03 16:13:00 +010099int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
100 unsigned char *buf,
101 size_t buflen )
102{
Hanno Becker54229812019-07-12 14:40:00 +0100103 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
105 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
106
107 /* We don't support record checking in TLS because
108 * (a) there doesn't seem to be a usecase for it, and
109 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
110 * and we'd need to backup the transform here.
111 */
112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
113 {
114 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
115 goto exit;
116 }
117#if defined(MBEDTLS_SSL_PROTO_DTLS)
118 else
119 {
irwir734f0cf2019-09-26 21:03:24 +0300120 mbedtls_record rec;
121
Hanno Becker54229812019-07-12 14:40:00 +0100122 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
123 if( ret != 0 )
124 {
125 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
126 goto exit;
127 }
128
129 if( ssl->transform_in != NULL )
130 {
131 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
132 if( ret != 0 )
133 {
134 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
135 goto exit;
136 }
137 }
138 }
139#endif /* MBEDTLS_SSL_PROTO_DTLS */
140
141exit:
142 /* On success, we have decrypted the buffer in-place, so make
143 * sure we don't leak any plaintext data. */
144 mbedtls_platform_zeroize( buf, buflen );
145
146 /* For the purpose of this API, treat messages with unexpected CID
147 * as well as such from future epochs as unexpected. */
148 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
149 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
150 {
151 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
152 }
153
154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
155 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100156}
157#endif /* MBEDTLS_SSL_RECORD_CHECKING */
158
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159#define SSL_DONT_FORCE_FLUSH 0
160#define SSL_FORCE_FLUSH 1
161
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100163
Hanno Beckerd5847772018-08-28 10:09:23 +0100164/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100165static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
166 uint8_t slot );
167static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
168static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
169static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
170static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100171static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
172 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100173static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100174
Hanno Becker11682cc2018-08-22 14:41:02 +0100175static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100176{
Hanno Becker89490712020-02-05 10:50:12 +0000177 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000178#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
179 size_t out_buf_len = ssl->out_buf_len;
180#else
181 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
182#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100183
Darryl Greenb33cc762019-11-28 14:29:44 +0000184 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100185 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186
Darryl Greenb33cc762019-11-28 14:29:44 +0000187 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100188}
189
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
191{
Hanno Becker11682cc2018-08-22 14:41:02 +0100192 size_t const bytes_written = ssl->out_left;
193 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194
195 /* Double-check that the write-index hasn't gone
196 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100197 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100198 {
199 /* Should never happen... */
200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
201 }
202
203 return( (int) ( mtu - bytes_written ) );
204}
205
206static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
207{
Janos Follath865b3eb2019-12-16 11:46:15 +0000208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400210 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211
212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400213 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 if( max_len > mfl )
216 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100217
218 /* By the standard (RFC 6066 Sect. 4), the MFL extension
219 * only limits the maximum record payload size, so in theory
220 * we would be allowed to pack multiple records of payload size
221 * MFL into a single datagram. However, this would mean that there's
222 * no way to explicitly communicate MTU restrictions to the peer.
223 *
224 * The following reduction of max_len makes sure that we never
225 * write datagrams larger than MFL + Record Expansion Overhead.
226 */
227 if( max_len <= ssl->out_left )
228 return( 0 );
229
230 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231#endif
232
233 ret = ssl_get_remaining_space_in_datagram( ssl );
234 if( ret < 0 )
235 return( ret );
236 remaining = (size_t) ret;
237
238 ret = mbedtls_ssl_get_record_expansion( ssl );
239 if( ret < 0 )
240 return( ret );
241 expansion = (size_t) ret;
242
243 if( remaining <= expansion )
244 return( 0 );
245
246 remaining -= expansion;
247 if( remaining >= max_len )
248 remaining = max_len;
249
250 return( (int) remaining );
251}
252
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200253/*
254 * Double the retransmit timeout value, within the allowed range,
255 * returning -1 if the maximum value has already been reached.
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258{
259 uint32_t new_timeout;
260
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200261 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262 return( -1 );
263
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200264 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
265 * in the following way: after the initial transmission and a first
266 * retransmission, back off to a temporary estimated MTU of 508 bytes.
267 * This value is guaranteed to be deliverable (if not guaranteed to be
268 * delivered) of any compliant IPv4 (and IPv6) network, and should work
269 * on most non-IP stacks too. */
270 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400271 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200272 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
274 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200275
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200276 new_timeout = 2 * ssl->handshake->retransmit_timeout;
277
278 /* Avoid arithmetic overflow and range overflow */
279 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200280 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200282 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283 }
284
285 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200287 ssl->handshake->retransmit_timeout ) );
288
289 return( 0 );
290}
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200294 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 ssl->handshake->retransmit_timeout ) );
297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
301int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200302 const unsigned char *key_enc, const unsigned char *key_dec,
303 size_t keylen,
304 const unsigned char *iv_enc, const unsigned char *iv_dec,
305 size_t ivlen,
306 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200307 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
309int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
311int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
312int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
313#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000314
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200315/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +0100316 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +0000317#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +0200318 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
319 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
320 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
321/* This function makes sure every byte in the memory region is accessed
322 * (in ascending addresses order) */
323static void ssl_read_memory( unsigned char *p, size_t len )
324{
325 unsigned char acc = 0;
326 volatile unsigned char force;
327
328 for( ; len != 0; p++, len-- )
329 acc ^= *p;
330
331 force = acc;
332 (void) force;
333}
334#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
335
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100336/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200338 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000339
Hanno Beckerccc13d02020-05-04 12:30:04 +0100340#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
341 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100342
343static size_t ssl_compute_padding_length( size_t len,
344 size_t granularity )
345{
346 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
347}
348
Hanno Becker581bc1b2020-05-04 12:20:03 +0100349/* This functions transforms a (D)TLS plaintext fragment and a record content
350 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
351 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
352 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100353 *
354 * struct {
355 * opaque content[DTLSPlaintext.length];
356 * ContentType real_type;
357 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100358 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100359 *
360 * Input:
361 * - `content`: The beginning of the buffer holding the
362 * plaintext to be wrapped.
363 * - `*content_size`: The length of the plaintext in Bytes.
364 * - `max_len`: The number of Bytes available starting from
365 * `content`. This must be `>= *content_size`.
366 * - `rec_type`: The desired record content type.
367 *
368 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100369 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
370 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100371 *
372 * Returns:
373 * - `0` on success.
374 * - A negative error code if `max_len` didn't offer enough space
375 * for the expansion.
376 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100377static int ssl_build_inner_plaintext( unsigned char *content,
378 size_t *content_size,
379 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100380 uint8_t rec_type,
381 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100382{
383 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100384
385 /* Write real content type */
386 if( remaining == 0 )
387 return( -1 );
388 content[ len ] = rec_type;
389 len++;
390 remaining--;
391
392 if( remaining < pad )
393 return( -1 );
394 memset( content + len, 0, pad );
395 len += pad;
396 remaining -= pad;
397
398 *content_size = len;
399 return( 0 );
400}
401
Hanno Becker581bc1b2020-05-04 12:20:03 +0100402/* This function parses a (D)TLSInnerPlaintext structure.
403 * See ssl_build_inner_plaintext() for details. */
404static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100405 size_t *content_size,
406 uint8_t *rec_type )
407{
408 size_t remaining = *content_size;
409
410 /* Determine length of padding by skipping zeroes from the back. */
411 do
412 {
413 if( remaining == 0 )
414 return( -1 );
415 remaining--;
416 } while( content[ remaining ] == 0 );
417
418 *content_size = remaining;
419 *rec_type = content[ remaining ];
420
421 return( 0 );
422}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100423#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
424 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100425
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100426/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100427 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000428static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100429 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100430 mbedtls_record *rec,
431 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000432{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100433 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100434 *
435 * additional_data = seq_num + TLSCompressed.type +
436 * TLSCompressed.version + TLSCompressed.length;
437 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100438 * For the CID extension, this is extended as follows
439 * (quoting draft-ietf-tls-dtls-connection-id-05,
440 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100441 *
442 * additional_data = seq_num + DTLSPlaintext.type +
443 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100444 * cid +
445 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100446 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100447 *
448 * For TLS 1.3, the record sequence number is dropped from the AAD
449 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100450 */
451
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100452 unsigned char *cur = add_data;
453
454#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
455 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
456#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
457 {
458 ((void) minor_ver);
459 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
460 cur += sizeof( rec->ctr );
461 }
462
463 *cur = rec->type;
464 cur++;
465
466 memcpy( cur, rec->ver, sizeof( rec->ver ) );
467 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100468
Hanno Beckera0e20d02019-05-15 14:03:01 +0100469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100470 if( rec->cid_len != 0 )
471 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100472 memcpy( cur, rec->cid, rec->cid_len );
473 cur += rec->cid_len;
474
475 *cur = rec->cid_len;
476 cur++;
477
478 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
479 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
480 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100481 }
482 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100483#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100484 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100485 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
486 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
487 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100488 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100489
490 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000491}
492
Hanno Becker9d062f92020-02-07 10:26:36 +0000493#if defined(MBEDTLS_SSL_PROTO_SSL3)
494
495#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
496
497/*
498 * SSLv3.0 MAC functions
499 */
500static void ssl_mac( mbedtls_md_context_t *md_ctx,
501 const unsigned char *secret,
502 const unsigned char *buf, size_t len,
503 const unsigned char *ctr, int type,
504 unsigned char out[SSL3_MAC_MAX_BYTES] )
505{
506 unsigned char header[11];
507 unsigned char padding[48];
508 int padlen;
509 int md_size = mbedtls_md_get_size( md_ctx->md_info );
510 int md_type = mbedtls_md_get_type( md_ctx->md_info );
511
512 /* Only MD5 and SHA-1 supported */
513 if( md_type == MBEDTLS_MD_MD5 )
514 padlen = 48;
515 else
516 padlen = 40;
517
518 memcpy( header, ctr, 8 );
519 header[ 8] = (unsigned char) type;
520 header[ 9] = (unsigned char)( len >> 8 );
521 header[10] = (unsigned char)( len );
522
523 memset( padding, 0x36, padlen );
524 mbedtls_md_starts( md_ctx );
525 mbedtls_md_update( md_ctx, secret, md_size );
526 mbedtls_md_update( md_ctx, padding, padlen );
527 mbedtls_md_update( md_ctx, header, 11 );
528 mbedtls_md_update( md_ctx, buf, len );
529 mbedtls_md_finish( md_ctx, out );
530
531 memset( padding, 0x5C, padlen );
532 mbedtls_md_starts( md_ctx );
533 mbedtls_md_update( md_ctx, secret, md_size );
534 mbedtls_md_update( md_ctx, padding, padlen );
535 mbedtls_md_update( md_ctx, out, md_size );
536 mbedtls_md_finish( md_ctx, out );
537}
538#endif /* MBEDTLS_SSL_PROTO_SSL3 */
539
Hanno Becker67a37db2020-05-28 16:27:07 +0100540#if defined(MBEDTLS_GCM_C) || \
541 defined(MBEDTLS_CCM_C) || \
542 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100543static int ssl_transform_aead_dynamic_iv_is_explicit(
544 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100545{
Hanno Becker17263802020-05-28 07:05:48 +0100546 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100547}
548
Hanno Becker17263802020-05-28 07:05:48 +0100549/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
550 *
551 * Concretely, this occurs in two variants:
552 *
553 * a) Fixed and dynamic IV lengths add up to total IV length, giving
554 * IV = fixed_iv || dynamic_iv
555 *
Hanno Becker15952812020-06-04 13:31:46 +0100556 * This variant is used in TLS 1.2 when used with GCM or CCM.
557 *
Hanno Becker17263802020-05-28 07:05:48 +0100558 * b) Fixed IV lengths matches total IV length, giving
559 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100560 *
561 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
562 *
563 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100564 *
565 * This function has the precondition that
566 *
567 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
568 *
569 * which has to be ensured by the caller. If this precondition
570 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100571 */
572static void ssl_build_record_nonce( unsigned char *dst_iv,
573 size_t dst_iv_len,
574 unsigned char const *fixed_iv,
575 size_t fixed_iv_len,
576 unsigned char const *dynamic_iv,
577 size_t dynamic_iv_len )
578{
579 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100580
581 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100582 memset( dst_iv, 0, dst_iv_len );
583 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100584
Hanno Becker17263802020-05-28 07:05:48 +0100585 dst_iv += dst_iv_len - dynamic_iv_len;
586 for( i = 0; i < dynamic_iv_len; i++ )
587 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100588}
Hanno Becker67a37db2020-05-28 16:27:07 +0100589#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100590
Hanno Beckera18d1322018-01-03 14:27:32 +0000591int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
592 mbedtls_ssl_transform *transform,
593 mbedtls_record *rec,
594 int (*f_rng)(void *, unsigned char *, size_t),
595 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100598 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000599 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100600 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100601 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000602 size_t post_avail;
603
604 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000605#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200606 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000607 ((void) ssl);
608#endif
609
610 /* The PRNG is used for dynamic IV generation that's used
611 * for CBC transformations in TLS 1.1 and TLS 1.2. */
612#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
613 ( defined(MBEDTLS_AES_C) || \
614 defined(MBEDTLS_ARIA_C) || \
615 defined(MBEDTLS_CAMELLIA_C) ) && \
616 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
617 ((void) f_rng);
618 ((void) p_rng);
619#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000623 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100624 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
627 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100628 if( rec == NULL
629 || rec->buf == NULL
630 || rec->buf_len < rec->data_offset
631 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100632#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100633 || rec->cid_len != 0
634#endif
635 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000636 {
637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100639 }
640
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000641 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100642 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000644 data, rec->data_len );
645
646 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
647
648 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
649 {
650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
651 (unsigned) rec->data_len,
652 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
654 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100655
Hanno Becker92313402020-05-20 13:58:58 +0100656 /* The following two code paths implement the (D)TLSInnerPlaintext
657 * structure present in TLS 1.3 and DTLS 1.2 + CID.
658 *
659 * See ssl_build_inner_plaintext() for more information.
660 *
661 * Note that this changes `rec->data_len`, and hence
662 * `post_avail` needs to be recalculated afterwards.
663 *
664 * Note also that the two code paths cannot occur simultaneously
665 * since they apply to different versions of the protocol. There
666 * is hence no risk of double-addition of the inner plaintext.
667 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100668#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
669 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
670 {
Hanno Becker13996922020-05-28 16:15:19 +0100671 size_t padding =
672 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100673 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100674 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100675 &rec->data_len,
676 post_avail,
677 rec->type,
678 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100679 {
680 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
681 }
682
683 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
684 }
685#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
686
Hanno Beckera0e20d02019-05-15 14:03:01 +0100687#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100688 /*
689 * Add CID information
690 */
691 rec->cid_len = transform->out_cid_len;
692 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
693 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100694
695 if( rec->cid_len != 0 )
696 {
Hanno Becker13996922020-05-28 16:15:19 +0100697 size_t padding =
698 ssl_compute_padding_length( rec->data_len,
699 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100700 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100701 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100702 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100703 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100704 * Note that this changes `rec->data_len`, and hence
705 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100706 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100707 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100708 &rec->data_len,
709 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100710 rec->type,
711 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100712 {
713 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
714 }
715
716 rec->type = MBEDTLS_SSL_MSG_CID;
717 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100718#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100719
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100720 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
721
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100723 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 */
Hanno Becker52344c22018-01-03 15:24:20 +0000725#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( mode == MBEDTLS_MODE_STREAM ||
727 ( mode == MBEDTLS_MODE_CBC
728#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000729 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100730#endif
731 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000733 if( post_avail < transform->maclen )
734 {
735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
736 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
737 }
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000740 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200741 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000742 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000743 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
744 data, rec->data_len, rec->ctr, rec->type, mac );
745 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200746 }
747 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
750 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000751 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200752 {
Hanno Becker992b6872017-11-09 18:57:39 +0000753 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
754
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100755 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
756 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000757
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000758 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100759 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000760 mbedtls_md_hmac_update( &transform->md_ctx_enc,
761 data, rec->data_len );
762 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
763 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
764
765 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200766 }
767 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200768#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200772 }
773
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000774 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
775 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200776
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000777 rec->data_len += transform->maclen;
778 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100779 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200780 }
Hanno Becker52344c22018-01-03 15:24:20 +0000781#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200783 /*
784 * Encrypt
785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
787 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000790 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000792 "including %d bytes of padding",
793 rec->data_len, 0 ) );
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, transform->ivlen,
797 data, rec->data_len,
798 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200801 return( ret );
802 }
803
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000804 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200808 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 }
Paul Bakker68884e32013-01-07 18:20:04 +0100810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000812
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200813#if defined(MBEDTLS_GCM_C) || \
814 defined(MBEDTLS_CCM_C) || \
815 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200817 mode == MBEDTLS_MODE_CCM ||
818 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000819 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000820 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200821 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100822 unsigned char *dynamic_iv;
823 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100824 int dynamic_iv_is_explicit =
825 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000826
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100827 /* Check that there's space for the authentication tag. */
828 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000829 {
830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
831 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
832 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000833
Paul Bakker68884e32013-01-07 18:20:04 +0100834 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100835 * Build nonce for AEAD encryption.
836 *
837 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
838 * part of the IV is prepended to the ciphertext and
839 * can be chosen freely - in particular, it need not
840 * agree with the record sequence number.
841 * However, since ChaChaPoly as well as all AEAD modes
842 * in TLS 1.3 use the record sequence number as the
843 * dynamic part of the nonce, we uniformly use the
844 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100845 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100846 dynamic_iv = rec->ctr;
847 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200848
Hanno Becker17263802020-05-28 07:05:48 +0100849 ssl_build_record_nonce( iv, sizeof( iv ),
850 transform->iv_enc,
851 transform->fixed_ivlen,
852 dynamic_iv,
853 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100854
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100855 /*
856 * Build additional data for AEAD encryption.
857 * This depends on the TLS version.
858 */
859 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
860 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100861
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100863 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200864 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100865 dynamic_iv,
866 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000867 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100868 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200870 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000871 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000872
Paul Bakker68884e32013-01-07 18:20:04 +0100873 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200874 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200875 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200877 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000878 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +0100879 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000880 data, rec->data_len, /* source */
881 data, &rec->data_len, /* destination */
882 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200885 return( ret );
886 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000887 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
888 data + rec->data_len, transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100889 /* Account for authentication tag. */
890 rec->data_len += transform->taglen;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000891 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100892
893 /*
894 * Prefix record content with dynamic IV in case it is explicit.
895 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100896 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100897 {
898 if( rec->data_offset < dynamic_iv_len )
899 {
900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
901 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
902 }
903
904 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
905 rec->data_offset -= dynamic_iv_len;
906 rec->data_len += dynamic_iv_len;
907 }
908
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100909 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000910 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100912#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +0000914 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000918 size_t padlen, i;
919 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000920
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000921 /* Currently we're always using minimal padding
922 * (up to 255 bytes would be allowed). */
923 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
924 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 padlen = 0;
926
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000927 /* Check there's enough space in the buffer for the padding. */
928 if( post_avail < padlen + 1 )
929 {
930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
931 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
932 }
933
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000935 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000937 rec->data_len += padlen + 1;
938 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000941 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000942 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
943 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000944 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000945 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000946 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000947 if( f_rng == NULL )
948 {
949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
951 }
952
953 if( rec->data_offset < transform->ivlen )
954 {
955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
957 }
958
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000959 /*
960 * Generate IV
961 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000962 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000963 if( ret != 0 )
964 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000965
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000966 memcpy( data - transform->ivlen, transform->iv_enc,
967 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000968
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000973 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000974 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200975 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000977 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
978 transform->iv_enc,
979 transform->ivlen,
980 data, rec->data_len,
981 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200984 return( ret );
985 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200986
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000987 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200991 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000994 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200995 {
996 /*
997 * Save IV in SSL3 and TLS1
998 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000999 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
1000 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001002 else
Paul Bakkercca5b812013-08-31 17:40:26 +02001003#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001004 {
1005 data -= transform->ivlen;
1006 rec->data_offset -= transform->ivlen;
1007 rec->data_len += transform->ivlen;
1008 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001011 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001012 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001013 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1014
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001015 /*
1016 * MAC(MAC_write_key, seq_num +
1017 * TLSCipherText.type +
1018 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001019 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001020 * IV + // except for TLS 1.0
1021 * ENC(content + padding + padding_length));
1022 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001023
1024 if( post_avail < transform->maclen)
1025 {
1026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1027 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1028 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001029
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001030 ssl_extract_add_data_from_record( add_data, &add_data_len,
1031 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +01001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001034 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001035 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001036
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001037 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001038 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001039 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1040 data, rec->data_len );
1041 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1042 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001043
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001044 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001045
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001046 rec->data_len += transform->maclen;
1047 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001048 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001054 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001060 /* Make extra sure authentication was performed, exactly once */
1061 if( auth_done != 1 )
1062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001065 }
1066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
1069 return( 0 );
1070}
1071
Hanno Becker605949f2019-07-12 08:23:59 +01001072int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001073 mbedtls_ssl_transform *transform,
1074 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001075{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001076 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001078 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001079#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001080 size_t padlen = 0, correct = 1;
1081#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001082 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001083 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001084 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001085
Hanno Beckera18d1322018-01-03 14:27:32 +00001086#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001087 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001088 ((void) ssl);
1089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001092 if( rec == NULL ||
1093 rec->buf == NULL ||
1094 rec->buf_len < rec->data_offset ||
1095 rec->buf_len - rec->data_offset < rec->data_len )
1096 {
1097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001099 }
1100
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001101 data = rec->buf + rec->data_offset;
1102 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Hanno Beckera0e20d02019-05-15 14:03:01 +01001104#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001105 /*
1106 * Match record's CID with incoming CID.
1107 */
Hanno Becker938489a2019-05-08 13:02:22 +01001108 if( rec->cid_len != transform->in_cid_len ||
1109 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1110 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001111 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001112 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001113#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1116 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001117 {
1118 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001119 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1120 transform->iv_dec,
1121 transform->ivlen,
1122 data, rec->data_len,
1123 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001126 return( ret );
1127 }
1128
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001129 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 }
Paul Bakker68884e32013-01-07 18:20:04 +01001135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001137#if defined(MBEDTLS_GCM_C) || \
1138 defined(MBEDTLS_CCM_C) || \
1139 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001141 mode == MBEDTLS_MODE_CCM ||
1142 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001143 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001144 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001145 unsigned char *dynamic_iv;
1146 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001147
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001148 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001149 * Extract dynamic part of nonce for AEAD decryption.
1150 *
1151 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1152 * part of the IV is prepended to the ciphertext and
1153 * can be chosen freely - in particular, it need not
1154 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001155 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001156 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001157 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001158 {
1159 if( rec->data_len < dynamic_iv_len )
1160 {
1161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) ",
1162 rec->data_len,
1163 dynamic_iv_len ) );
1164 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1165 }
1166 dynamic_iv = data;
1167
1168 data += dynamic_iv_len;
1169 rec->data_offset += dynamic_iv_len;
1170 rec->data_len -= dynamic_iv_len;
1171 }
Hanno Becker17263802020-05-28 07:05:48 +01001172 else
1173 {
1174 dynamic_iv = rec->ctr;
1175 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001176
1177 /* Check that there's space for the authentication tag. */
1178 if( rec->data_len < transform->taglen )
1179 {
1180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < taglen (%d) " ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001182 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001183 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001184
Hanno Beckerdf8be222020-05-21 15:30:57 +01001185 /*
1186 * Prepare nonce from dynamic and static parts.
1187 */
Hanno Becker17263802020-05-28 07:05:48 +01001188 ssl_build_record_nonce( iv, sizeof( iv ),
1189 transform->iv_dec,
1190 transform->fixed_ivlen,
1191 dynamic_iv,
1192 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001193
Hanno Beckerdf8be222020-05-21 15:30:57 +01001194 /*
1195 * Build additional data for AEAD encryption.
1196 * This depends on the TLS version.
1197 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001198 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1199 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001200 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001201 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001202
Hanno Beckerd96a6522019-07-10 13:55:25 +01001203 /* Because of the check above, we know that there are
1204 * explicit_iv_len Bytes preceeding data, and taglen
1205 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001206 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001207 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001208
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001209 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001210 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001211 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001212
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001213 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001214 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001215 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001216 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
1217 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001218 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001219 data, rec->data_len,
1220 data, &olen,
1221 data + rec->data_len,
1222 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1227 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001228
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001229 return( ret );
1230 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001231 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001232
Hanno Beckerd96a6522019-07-10 13:55:25 +01001233 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001234 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001238 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1242#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001243 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001246 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 /*
Paul Bakker45829992013-01-03 14:52:21 +01001249 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001252 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1253 {
1254 /* The ciphertext is prefixed with the CBC IV. */
1255 minlen += transform->ivlen;
1256 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001257#endif
Paul Bakker45829992013-01-03 14:52:21 +01001258
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001259 /* Size considerations:
1260 *
1261 * - The CBC cipher text must not be empty and hence
1262 * at least of size transform->ivlen.
1263 *
1264 * Together with the potential IV-prefix, this explains
1265 * the first of the two checks below.
1266 *
1267 * - The record must contain a MAC, either in plain or
1268 * encrypted, depending on whether Encrypt-then-MAC
1269 * is used or not.
1270 * - If it is, the message contains the IV-prefix,
1271 * the CBC ciphertext, and the MAC.
1272 * - If it is not, the padded plaintext, and hence
1273 * the CBC ciphertext, has at least length maclen + 1
1274 * because there is at least the padding length byte.
1275 *
1276 * As the CBC ciphertext is not empty, both cases give the
1277 * lower bound minlen + maclen + 1 on the record size, which
1278 * we test for in the second check below.
1279 */
1280 if( rec->data_len < minlen + transform->ivlen ||
1281 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001284 "+ 1 ) ( + expl IV )", rec->data_len,
1285 transform->ivlen,
1286 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001288 }
1289
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001290 /*
1291 * Authenticate before decrypt if enabled
1292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001294 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001295 {
Hanno Becker992b6872017-11-09 18:57:39 +00001296 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001299
Hanno Beckerd96a6522019-07-10 13:55:25 +01001300 /* Update data_len in tandem with add_data.
1301 *
1302 * The subtraction is safe because of the previous check
1303 * data_len >= minlen + maclen + 1.
1304 *
1305 * Afterwards, we know that data + data_len is followed by at
1306 * least maclen Bytes, which justifies the call to
1307 * mbedtls_ssl_safer_memcmp() below.
1308 *
1309 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001310 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001311 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1312 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001313
Hanno Beckerd96a6522019-07-10 13:55:25 +01001314 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001315 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1316 add_data_len );
1317 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1318 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001319 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1320 data, rec->data_len );
1321 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1322 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001323
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001324 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1325 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001326 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001327 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001328
Hanno Beckerd96a6522019-07-10 13:55:25 +01001329 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001330 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1331 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001335 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001336 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001339
1340 /*
1341 * Check length sanity
1342 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001343
1344 /* We know from above that data_len > minlen >= 0,
1345 * so the following check in particular implies that
1346 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001347 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001350 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001352 }
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001355 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001356 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001357 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001358 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001359 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001360 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001361 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001362
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001363 data += transform->ivlen;
1364 rec->data_offset += transform->ivlen;
1365 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001368
Hanno Beckerd96a6522019-07-10 13:55:25 +01001369 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1370
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001371 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1372 transform->iv_dec, transform->ivlen,
1373 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001376 return( ret );
1377 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001378
Hanno Beckerd96a6522019-07-10 13:55:25 +01001379 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001380 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1383 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001384 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001387 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001388 {
1389 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001390 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1391 * records is equivalent to CBC decryption of the concatenation
1392 * of the records; in other words, IVs are maintained across
1393 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001394 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001395 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1396 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001400 /* Safe since data_len >= minlen + maclen + 1, so after having
1401 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001402 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1403 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001404 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001405
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001406 if( auth_done == 1 )
1407 {
1408 correct *= ( rec->data_len >= padlen + 1 );
1409 padlen *= ( rec->data_len >= padlen + 1 );
1410 }
1411 else
Paul Bakker45829992013-01-03 14:52:21 +01001412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001414 if( rec->data_len < transform->maclen + padlen + 1 )
1415 {
1416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1417 rec->data_len,
1418 transform->maclen,
1419 padlen + 1 ) );
1420 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001421#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001422
1423 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
1424 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01001425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001427 padlen++;
1428
1429 /* Regardless of the validity of the padding,
1430 * we have data_len >= padlen here. */
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001433 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001435 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_SSL_DEBUG_ALL)
1438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001439 "should be no more than %d",
1440 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001441#endif
Paul Bakker45829992013-01-03 14:52:21 +01001442 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444 }
1445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1447#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1448 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001449 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001451 /* The padding check involves a series of up to 256
1452 * consecutive memory reads at the end of the record
1453 * plaintext buffer. In order to hide the length and
1454 * validity of the padding, always perform exactly
1455 * `min(256,plaintext_len)` reads (but take into account
1456 * only the last `padlen` bytes for the padding check). */
1457 size_t pad_count = 0;
1458 size_t real_count = 0;
1459 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001460
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001461 /* Index of first padding byte; it has been ensured above
1462 * that the subtraction is safe. */
1463 size_t const padding_idx = rec->data_len - padlen;
1464 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1465 size_t const start_idx = rec->data_len - num_checks;
1466 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001467
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001468 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001469 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001470 real_count |= ( idx >= padding_idx );
1471 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001472 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001473 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001476 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001478#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001479 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1483 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001487 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001488
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001489 /* If the padding was found to be invalid, padlen == 0
1490 * and the subtraction is safe. If the padding was found valid,
1491 * padlen hasn't been changed and the previous assertion
1492 * data_len >= padlen still holds. */
1493 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001495 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001497 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001503#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001505 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
1508 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001509 * Authenticate if not done yet.
1510 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 */
Hanno Becker52344c22018-01-03 15:24:20 +00001512#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001513 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001514 {
Hanno Becker992b6872017-11-09 18:57:39 +00001515 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001516
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001517 /* If the initial value of padlen was such that
1518 * data_len < maclen + padlen + 1, then padlen
1519 * got reset to 1, and the initial check
1520 * data_len >= minlen + maclen + 1
1521 * guarantees that at this point we still
1522 * have at least data_len >= maclen.
1523 *
1524 * If the initial value of padlen was such that
1525 * data_len >= maclen + padlen + 1, then we have
1526 * subtracted either padlen + 1 (if the padding was correct)
1527 * or 0 (if the padding was incorrect) since then,
1528 * hence data_len >= maclen in any case.
1529 */
1530 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001531 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1532 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001535 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001536 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001537 ssl_mac( &transform->md_ctx_dec,
1538 transform->mac_dec,
1539 data, rec->data_len,
1540 rec->ctr, rec->type,
1541 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001542 }
1543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1545#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1546 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001547 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001548 {
1549 /*
1550 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02001551 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001552 *
1553 * Known timing attacks:
1554 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1555 *
Gilles Peskine20b44082018-05-29 14:06:49 +02001556 * To compensate for different timings for the MAC calculation
1557 * depending on how much padding was removed (which is determined
1558 * by padlen), process extra_run more blocks through the hash
1559 * function.
1560 *
1561 * The formula in the paper is
1562 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
1563 * where L1 is the size of the header plus the decrypted message
1564 * plus CBC padding and L2 is the size of the header plus the
1565 * decrypted message. This is for an underlying hash function
1566 * with 64-byte blocks.
1567 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
1568 * correctly. We round down instead of up, so -56 is the correct
1569 * value for our calculations instead of -55.
1570 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02001571 * Repeat the formula rather than defining a block_size variable.
1572 * This avoids requiring division by a variable at runtime
1573 * (which would be marginally less efficient and would require
1574 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001575 */
1576 size_t j, extra_run = 0;
Manuel Pégourié-Gonnardf4e3fc92020-06-12 11:14:35 +02001577 /* This size is enough to server either as input to
1578 * md_process() or as output to md_finish() */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001579 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001580
1581 /*
1582 * The next two sizes are the minimum and maximum values of
1583 * in_msglen over all padlen values.
1584 *
1585 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001586 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001587 *
1588 * Note that max_len + maclen is never more than the buffer
1589 * length, as we previously did in_msglen -= maclen too.
1590 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001591 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001592 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1593
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001594 memset( tmp, 0, sizeof( tmp ) );
1595
1596 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001597 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001598#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1599 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001600 case MBEDTLS_MD_MD5:
1601 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001602 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001603 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001604 extra_run =
1605 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1606 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001607 break;
1608#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001609#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001610 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001611 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001612 extra_run =
1613 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1614 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001615 break;
1616#endif
1617 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1620 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001621
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001622 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001623
Hanno Beckercab87e62019-04-29 13:52:53 +01001624 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1625 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001626 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1627 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001628 /* Make sure we access everything even when padlen > 0. This
1629 * makes the synchronisation requirements for just-in-time
1630 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001631 ssl_read_memory( data + rec->data_len, padlen );
1632 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001633
Manuel Pégourié-Gonnardf4e3fc92020-06-12 11:14:35 +02001634 /* Dummy calls to compression function.
1635 * Call mbedtls_md_process at least once due to cache attacks
1636 * that observe whether md_process() was called of not.
1637 * Respect the usual start-(process|update)-finish sequence for
1638 * the sake of hardware accelerators that might require it. */
1639 mbedtls_md_starts( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001640 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001641 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Manuel Pégourié-Gonnardf4e3fc92020-06-12 11:14:35 +02001642 mbedtls_md_finish( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001643
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001644 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001645
1646 /* Make sure we access all the memory that could contain the MAC,
1647 * before we check it in the next code block. This makes the
1648 * synchronisation requirements for just-in-time Prime+Probe
1649 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001650 ssl_read_memory( data + min_len,
1651 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 }
1653 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1655 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001661#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001662 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1663 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001666 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1667 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_SSL_DEBUG_ALL)
1670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001671#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 correct = 0;
1673 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001674 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001676
1677 /*
1678 * Finally check the correct flag
1679 */
1680 if( correct == 0 )
1681 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001682#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683
1684 /* Make extra sure authentication was performed, exactly once */
1685 if( auth_done != 1 )
1686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Hanno Beckerccc13d02020-05-04 12:30:04 +01001691#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1692 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1693 {
1694 /* Remove inner padding and infer true content type. */
1695 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1696 &rec->type );
1697
1698 if( ret != 0 )
1699 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1700 }
1701#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1702
Hanno Beckera0e20d02019-05-15 14:03:01 +01001703#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001704 if( rec->cid_len != 0 )
1705 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001706 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1707 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001708 if( ret != 0 )
1709 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1710 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001711#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
1715 return( 0 );
1716}
1717
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001718#undef MAC_NONE
1719#undef MAC_PLAINTEXT
1720#undef MAC_CIPHERTEXT
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001723/*
1724 * Compression/decompression functions
1725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727{
Janos Follath865b3eb2019-12-16 11:46:15 +00001728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001729 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001730 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001732 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001733#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1734 size_t out_buf_len = ssl->out_buf_len;
1735#else
1736 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1737#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001741 if( len_pre == 0 )
1742 return( 0 );
1743
Paul Bakker2770fbd2012-07-03 13:30:23 +00001744 memcpy( msg_pre, ssl->out_msg, len_pre );
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001747 ssl->out_msglen ) );
1748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 ssl->out_msg, ssl->out_msglen );
1751
Paul Bakker48916f92012-09-16 19:57:18 +00001752 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1753 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1754 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001755 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756
Paul Bakker48916f92012-09-16 19:57:18 +00001757 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758 if( ret != Z_OK )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1761 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762 }
1763
Darryl Greenb33cc762019-11-28 14:29:44 +00001764 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001765 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001768 ssl->out_msglen ) );
1769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771 ssl->out_msg, ssl->out_msglen );
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001774
1775 return( 0 );
1776}
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001779{
Janos Follath865b3eb2019-12-16 11:46:15 +00001780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001781 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001782 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001784 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001785#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1786 size_t in_buf_len = ssl->in_buf_len;
1787#else
1788 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1789#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001792
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001793 if( len_pre == 0 )
1794 return( 0 );
1795
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 memcpy( msg_pre, ssl->in_msg, len_pre );
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 ssl->in_msglen ) );
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802 ssl->in_msg, ssl->in_msglen );
1803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1805 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1806 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001807 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808
Paul Bakker48916f92012-09-16 19:57:18 +00001809 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810 if( ret != Z_OK )
1811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1813 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001814 }
1815
Darryl Greenb33cc762019-11-28 14:29:44 +00001816 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001817 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820 ssl->in_msglen ) );
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823 ssl->in_msg, ssl->in_msglen );
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001826
1827 return( 0 );
1828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001830
Paul Bakker5121ce52009-01-03 21:22:43 +00001831/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001832 * Fill the input message buffer by appending data to it.
1833 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001834 *
1835 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1836 * available (from this read and/or a previous one). Otherwise, an error code
1837 * is returned (possibly EOF or WANT_READ).
1838 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001839 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1840 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1841 * since we always read a whole datagram at once.
1842 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001843 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001844 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847{
Janos Follath865b3eb2019-12-16 11:46:15 +00001848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001849 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001850#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1851 size_t in_buf_len = ssl->in_buf_len;
1852#else
1853 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001858 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001861 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001863 }
1864
Darryl Greenb33cc762019-11-28 14:29:44 +00001865 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001869 }
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001872 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001874 uint32_t timeout;
1875
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001876 /* Just to be sure */
1877 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1878 {
1879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1880 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1881 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1882 }
1883
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001884 /*
1885 * The point is, we need to always read a full datagram at once, so we
1886 * sometimes read more then requested, and handle the additional data.
1887 * It could be the rest of the current record (while fetching the
1888 * header) and/or some other records in the same datagram.
1889 */
1890
1891 /*
1892 * Move to the next record in the already read datagram if applicable
1893 */
1894 if( ssl->next_record_offset != 0 )
1895 {
1896 if( ssl->in_left < ssl->next_record_offset )
1897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001900 }
1901
1902 ssl->in_left -= ssl->next_record_offset;
1903
1904 if( ssl->in_left != 0 )
1905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001907 ssl->next_record_offset ) );
1908 memmove( ssl->in_hdr,
1909 ssl->in_hdr + ssl->next_record_offset,
1910 ssl->in_left );
1911 }
1912
1913 ssl->next_record_offset = 0;
1914 }
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001918
1919 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001920 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001921 */
1922 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001925 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001926 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001927
1928 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001929 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001930 * are not at the beginning of a new record, the caller did something
1931 * wrong.
1932 */
1933 if( ssl->in_left != 0 )
1934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001937 }
1938
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001939 /*
1940 * Don't even try to read if time's out already.
1941 * This avoids by-passing the timer when repeatedly receiving messages
1942 * that will end up being dropped.
1943 */
Hanno Becker7876d122020-02-05 10:39:31 +00001944 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001945 {
1946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001947 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001948 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001949 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001950 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001951 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001954 timeout = ssl->handshake->retransmit_timeout;
1955 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001956 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001959
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001960 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001961 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1962 timeout );
1963 else
1964 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001967
1968 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001970 }
1971
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001972 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001975 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001978 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001979 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001982 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001983 }
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001988 return( ret );
1989 }
1990
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001991 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001992 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001994 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001996 {
Hanno Becker786300f2020-02-05 10:46:40 +00001997 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001998 {
Hanno Becker786300f2020-02-05 10:46:40 +00001999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
2000 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002001 return( ret );
2002 }
2003
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002004 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002007 }
2008
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 if( ret < 0 )
2010 return( ret );
2011
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002012 ssl->in_left = ret;
2013 }
2014 else
2015#endif
2016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002018 ssl->in_left, nb_want ) );
2019
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002020 while( ssl->in_left < nb_want )
2021 {
2022 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002023
Hanno Becker7876d122020-02-05 10:39:31 +00002024 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002025 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2026 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002027 {
2028 if( ssl->f_recv_timeout != NULL )
2029 {
2030 ret = ssl->f_recv_timeout( ssl->p_bio,
2031 ssl->in_hdr + ssl->in_left, len,
2032 ssl->conf->read_timeout );
2033 }
2034 else
2035 {
2036 ret = ssl->f_recv( ssl->p_bio,
2037 ssl->in_hdr + ssl->in_left, len );
2038 }
2039 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002042 ssl->in_left, nb_want ) );
2043 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002044
2045 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002047
2048 if( ret < 0 )
2049 return( ret );
2050
makise-homuraaf9513b2020-08-24 18:26:27 +03002051 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002052 {
Darryl Green11999bb2018-03-13 15:22:58 +00002053 MBEDTLS_SSL_DEBUG_MSG( 1,
2054 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002055 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2057 }
2058
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002059 ssl->in_left += ret;
2060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 }
2062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
2065 return( 0 );
2066}
2067
2068/*
2069 * Flush any data not yet written
2070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072{
Janos Follath865b3eb2019-12-16 11:46:15 +00002073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002074 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002078 if( ssl->f_send == NULL )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002081 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002083 }
2084
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002085 /* Avoid incrementing counter if data is flushed */
2086 if( ssl->out_left == 0 )
2087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002089 return( 0 );
2090 }
2091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 while( ssl->out_left > 0 )
2093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002095 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Hanno Becker2b1e3542018-08-06 11:19:13 +01002097 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002098 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
2102 if( ret <= 0 )
2103 return( ret );
2104
makise-homuraaf9513b2020-08-24 18:26:27 +03002105 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002106 {
Darryl Green11999bb2018-03-13 15:22:58 +00002107 MBEDTLS_SSL_DEBUG_MSG( 1,
2108 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002109 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2111 }
2112
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 ssl->out_left -= ret;
2114 }
2115
Hanno Becker2b1e3542018-08-06 11:19:13 +01002116#if defined(MBEDTLS_SSL_PROTO_DTLS)
2117 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002118 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002119 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002120 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002121 else
2122#endif
2123 {
2124 ssl->out_hdr = ssl->out_buf + 8;
2125 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002126 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
2130 return( 0 );
2131}
2132
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002133/*
2134 * Functions to handle the DTLS retransmission state machine
2135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002137/*
2138 * Append current handshake message to current outgoing flight
2139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2144 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2145 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002146
2147 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002148 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002149 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002152 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002153 }
2154
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002155 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002156 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002159 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002160 }
2161
2162 /* Copy current handshake message with headers */
2163 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2164 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002165 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002166 msg->next = NULL;
2167
2168 /* Append to the current flight */
2169 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002170 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002171 else
2172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002174 while( cur->next != NULL )
2175 cur = cur->next;
2176 cur->next = msg;
2177 }
2178
Hanno Becker3b235902018-08-06 09:54:53 +01002179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002180 return( 0 );
2181}
2182
2183/*
2184 * Free the current flight of handshake messages
2185 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002186void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_ssl_flight_item *cur = flight;
2189 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002190
2191 while( cur != NULL )
2192 {
2193 next = cur->next;
2194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 mbedtls_free( cur->p );
2196 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002197
2198 cur = next;
2199 }
2200}
2201
2202/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002203 * Swap transform_out and out_ctr with the alternative ones
2204 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002205static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002208 unsigned char tmp_out_ctr[8];
2209
2210 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002213 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002214 }
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002217
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002218 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002219 tmp_transform = ssl->transform_out;
2220 ssl->transform_out = ssl->handshake->alt_transform_out;
2221 ssl->handshake->alt_transform_out = tmp_transform;
2222
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002223 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002224 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2225 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002226 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002227
2228 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002229 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2232 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002233 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002234 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2235 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2238 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002239 }
2240 }
2241#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002242
2243 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002244}
2245
2246/*
2247 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002248 */
2249int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2250{
2251 int ret = 0;
2252
2253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2254
2255 ret = mbedtls_ssl_flight_transmit( ssl );
2256
2257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2258
2259 return( ret );
2260}
2261
2262/*
2263 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002264 *
2265 * Need to remember the current message in case flush_output returns
2266 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002267 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002268 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002269int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002270{
Janos Follath865b3eb2019-12-16 11:46:15 +00002271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002275 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002277
2278 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002279 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002280 ret = ssl_swap_epochs( ssl );
2281 if( ret != 0 )
2282 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002285 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002286
2287 while( ssl->handshake->cur_msg != NULL )
2288 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002289 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002290 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002291
Hanno Beckere1dcb032018-08-17 16:47:58 +01002292 int const is_finished =
2293 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2294 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2295
Hanno Becker04da1892018-08-14 13:22:10 +01002296 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2297 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2298
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002299 /* Swap epochs before sending Finished: we can't do it after
2300 * sending ChangeCipherSpec, in case write returns WANT_READ.
2301 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002302 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002303 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002305 ret = ssl_swap_epochs( ssl );
2306 if( ret != 0 )
2307 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002308 }
2309
Hanno Becker67bc7c32018-08-06 11:33:50 +01002310 ret = ssl_get_remaining_payload_in_datagram( ssl );
2311 if( ret < 0 )
2312 return( ret );
2313 max_frag_len = (size_t) ret;
2314
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002315 /* CCS is copied as is, while HS messages may need fragmentation */
2316 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2317 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002318 if( max_frag_len == 0 )
2319 {
2320 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2321 return( ret );
2322
2323 continue;
2324 }
2325
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002326 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002327 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002328 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002329
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002330 /* Update position inside current message */
2331 ssl->handshake->cur_msg_p += cur->len;
2332 }
2333 else
2334 {
2335 const unsigned char * const p = ssl->handshake->cur_msg_p;
2336 const size_t hs_len = cur->len - 12;
2337 const size_t frag_off = p - ( cur->p + 12 );
2338 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002339 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002340
Hanno Beckere1dcb032018-08-17 16:47:58 +01002341 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002342 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002343 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002344 {
2345 ret = ssl_swap_epochs( ssl );
2346 if( ret != 0 )
2347 return( ret );
2348 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002349
Hanno Becker67bc7c32018-08-06 11:33:50 +01002350 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2351 return( ret );
2352
2353 continue;
2354 }
2355 max_hs_frag_len = max_frag_len - 12;
2356
2357 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2358 max_hs_frag_len : rem_len;
2359
2360 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002361 {
2362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002363 (unsigned) cur_hs_frag_len,
2364 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002365 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002366
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002367 /* Messages are stored with handshake headers as if not fragmented,
2368 * copy beginning of headers then fill fragmentation fields.
2369 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2370 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002371
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002372 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2373 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2374 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2375
Hanno Becker67bc7c32018-08-06 11:33:50 +01002376 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2377 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2378 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002379
2380 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2381
Hanno Becker3f7b9732018-08-28 09:53:25 +01002382 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002383 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2384 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002385 ssl->out_msgtype = cur->type;
2386
2387 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002388 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002389 }
2390
2391 /* If done with the current message move to the next one if any */
2392 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2393 {
2394 if( cur->next != NULL )
2395 {
2396 ssl->handshake->cur_msg = cur->next;
2397 ssl->handshake->cur_msg_p = cur->next->p + 12;
2398 }
2399 else
2400 {
2401 ssl->handshake->cur_msg = NULL;
2402 ssl->handshake->cur_msg_p = NULL;
2403 }
2404 }
2405
2406 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002407 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002410 return( ret );
2411 }
2412 }
2413
Hanno Becker67bc7c32018-08-06 11:33:50 +01002414 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2415 return( ret );
2416
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002417 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2419 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002420 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002423 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002424 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002425
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002427
2428 return( 0 );
2429}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002430
2431/*
2432 * To be called when the last message of an incoming flight is received.
2433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002435{
2436 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002437 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002438 ssl->handshake->flight = NULL;
2439 ssl->handshake->cur_msg = NULL;
2440
2441 /* The next incoming flight will start with this msg_seq */
2442 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2443
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002444 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002445 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002446
Hanno Becker0271f962018-08-16 13:23:47 +01002447 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002448 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002449
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002450 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002451 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2454 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002457 }
2458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002460}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002461
2462/*
2463 * To be called when the last message of an outgoing flight is send.
2464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002466{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002467 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002468 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2471 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002474 }
2475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002479
Paul Bakker5121ce52009-01-03 21:22:43 +00002480/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002481 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002483
2484/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002485 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002486 *
2487 * - fill in handshake headers
2488 * - update handshake checksum
2489 * - DTLS: save message for resending
2490 * - then pass to the record layer
2491 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002492 * DTLS: except for HelloRequest, messages are only queued, and will only be
2493 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002494 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002495 * Inputs:
2496 * - ssl->out_msglen: 4 + actual handshake message len
2497 * (4 is the size of handshake headers for TLS)
2498 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2499 * - ssl->out_msg + 4: the handshake message body
2500 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002501 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002502 * - ssl->out_msglen: the length of the record contents
2503 * (including handshake headers but excluding record headers)
2504 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002505 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002506int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507{
Janos Follath865b3eb2019-12-16 11:46:15 +00002508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002509 const size_t hs_len = ssl->out_msglen - 4;
2510 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2513
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002514 /*
2515 * Sanity checks
2516 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002517 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002518 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2519 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002520 /* In SSLv3, the client might send a NoCertificate alert. */
2521#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2522 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2523 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2524 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2525#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2526 {
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2529 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002532 /* Whenever we send anything different from a
2533 * HelloRequest we should be in a handshake - double check. */
2534 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2535 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002536 ssl->handshake == NULL )
2537 {
2538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002544 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002546 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002549 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002550#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002551
Hanno Beckerb50a2532018-08-06 11:52:54 +01002552 /* Double-check that we did not exceed the bounds
2553 * of the outgoing record buffer.
2554 * This should never fail as the various message
2555 * writing functions must obey the bounds of the
2556 * outgoing record buffer, but better be safe.
2557 *
2558 * Note: We deliberately do not check for the MTU or MFL here.
2559 */
2560 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2563 "size %u, maximum %u",
2564 (unsigned) ssl->out_msglen,
2565 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2567 }
2568
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002569 /*
2570 * Fill handshake headers
2571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002574 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2575 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2576 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002578 /*
2579 * DTLS has additional fields in the Handshake layer,
2580 * between the length field and the actual payload:
2581 * uint16 message_seq;
2582 * uint24 fragment_offset;
2583 * uint24 fragment_length;
2584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002587 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002588 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002589 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2592 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002593 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002594 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2596 }
2597
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002598 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002599 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002600
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002601 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002602 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002603 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002604 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2605 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2606 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002607 }
2608 else
2609 {
2610 ssl->out_msg[4] = 0;
2611 ssl->out_msg[5] = 0;
2612 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002613
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002614 /* Handshake hashes are computed without fragmentation,
2615 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002616 memset( ssl->out_msg + 6, 0x00, 3 );
2617 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002620
Hanno Becker0207e532018-08-28 10:28:28 +01002621 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002622 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2623 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 }
2625
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002626 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002628 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002629 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2630 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002631 {
2632 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002635 return( ret );
2636 }
2637 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002638 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002639#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002640 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002641 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002642 {
2643 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2644 return( ret );
2645 }
2646 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002647
2648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2649
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002650 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002651}
2652
2653/*
2654 * Record layer functions
2655 */
2656
2657/*
2658 * Write current record.
2659 *
2660 * Uses:
2661 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2662 * - ssl->out_msglen: length of the record content (excl headers)
2663 * - ssl->out_msg: record content
2664 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002665int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002666{
2667 int ret, done = 0;
2668 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002669 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002670
2671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002674 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002676 {
2677 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002680 return( ret );
2681 }
2682
2683 len = ssl->out_msglen;
2684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2688 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 ret = mbedtls_ssl_hw_record_write( ssl );
2693 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2696 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002697 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002698
2699 if( ret == 0 )
2700 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002703 if( !done )
2704 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002705 unsigned i;
2706 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002707#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2708 size_t out_buf_len = ssl->out_buf_len;
2709#else
2710 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2711#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002712 /* Skip writing the record content type to after the encryption,
2713 * as it may change when using the CID extension. */
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002716 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002717
Hanno Becker19859472018-08-06 09:40:20 +01002718 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002719 ssl->out_len[0] = (unsigned char)( len >> 8 );
2720 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002721
Paul Bakker48916f92012-09-16 19:57:18 +00002722 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002723 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002724 mbedtls_record rec;
2725
2726 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002727 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002728 rec.data_len = ssl->out_msglen;
2729 rec.data_offset = ssl->out_msg - rec.buf;
2730
2731 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2732 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2733 ssl->conf->transport, rec.ver );
2734 rec.type = ssl->out_msgtype;
2735
Hanno Beckera0e20d02019-05-15 14:03:01 +01002736#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002737 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002738 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002739#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002740
Hanno Beckera18d1322018-01-03 14:27:32 +00002741 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002742 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002745 return( ret );
2746 }
2747
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002748 if( rec.data_offset != 0 )
2749 {
2750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2752 }
2753
Hanno Becker6430faf2019-05-08 11:57:13 +01002754 /* Update the record content type and CID. */
2755 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002756#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002757 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002758#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002759 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002760 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2761 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002762 }
2763
Hanno Becker5903de42019-05-03 14:46:38 +01002764 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002765
2766#if defined(MBEDTLS_SSL_PROTO_DTLS)
2767 /* In case of DTLS, double-check that we don't exceed
2768 * the remaining space in the datagram. */
2769 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2770 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002771 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002772 if( ret < 0 )
2773 return( ret );
2774
2775 if( protected_record_size > (size_t) ret )
2776 {
2777 /* Should never happen */
2778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2779 }
2780 }
2781#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002782
Hanno Becker6430faf2019-05-08 11:57:13 +01002783 /* Now write the potentially updated record content type. */
2784 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002787 "version = [%d:%d], msglen = %d",
2788 ssl->out_hdr[0], ssl->out_hdr[1],
2789 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002792 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002793
2794 ssl->out_left += protected_record_size;
2795 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002796 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002797
Hanno Beckerdd772292020-02-05 10:38:31 +00002798 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002799 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2800 break;
2801
2802 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002803 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002804 {
2805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2806 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 }
2809
Hanno Becker67bc7c32018-08-06 11:33:50 +01002810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002811 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2812 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002813 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002814 size_t remaining;
2815 ret = ssl_get_remaining_payload_in_datagram( ssl );
2816 if( ret < 0 )
2817 {
2818 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2819 ret );
2820 return( ret );
2821 }
2822
2823 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002824 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002825 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002826 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002827 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002828 else
2829 {
Hanno Becker513815a2018-08-20 11:56:09 +01002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002831 }
2832 }
2833#endif /* MBEDTLS_SSL_PROTO_DTLS */
2834
2835 if( ( flush == SSL_FORCE_FLUSH ) &&
2836 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 return( ret );
2840 }
2841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 return( 0 );
2845}
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002848
2849static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2850{
2851 if( ssl->in_msglen < ssl->in_hslen ||
2852 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2853 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2854 {
2855 return( 1 );
2856 }
2857 return( 0 );
2858}
Hanno Becker44650b72018-08-16 12:51:11 +01002859
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002860static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002861{
2862 return( ( ssl->in_msg[9] << 16 ) |
2863 ( ssl->in_msg[10] << 8 ) |
2864 ssl->in_msg[11] );
2865}
2866
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002867static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002868{
2869 return( ( ssl->in_msg[6] << 16 ) |
2870 ( ssl->in_msg[7] << 8 ) |
2871 ssl->in_msg[8] );
2872}
2873
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002874static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002875{
2876 uint32_t msg_len, frag_off, frag_len;
2877
2878 msg_len = ssl_get_hs_total_len( ssl );
2879 frag_off = ssl_get_hs_frag_off( ssl );
2880 frag_len = ssl_get_hs_frag_len( ssl );
2881
2882 if( frag_off > msg_len )
2883 return( -1 );
2884
2885 if( frag_len > msg_len - frag_off )
2886 return( -1 );
2887
2888 if( frag_len + 12 > ssl->in_msglen )
2889 return( -1 );
2890
2891 return( 0 );
2892}
2893
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002894/*
2895 * Mark bits in bitmask (used for DTLS HS reassembly)
2896 */
2897static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2898{
2899 unsigned int start_bits, end_bits;
2900
2901 start_bits = 8 - ( offset % 8 );
2902 if( start_bits != 8 )
2903 {
2904 size_t first_byte_idx = offset / 8;
2905
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002906 /* Special case */
2907 if( len <= start_bits )
2908 {
2909 for( ; len != 0; len-- )
2910 mask[first_byte_idx] |= 1 << ( start_bits - len );
2911
2912 /* Avoid potential issues with offset or len becoming invalid */
2913 return;
2914 }
2915
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002916 offset += start_bits; /* Now offset % 8 == 0 */
2917 len -= start_bits;
2918
2919 for( ; start_bits != 0; start_bits-- )
2920 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2921 }
2922
2923 end_bits = len % 8;
2924 if( end_bits != 0 )
2925 {
2926 size_t last_byte_idx = ( offset + len ) / 8;
2927
2928 len -= end_bits; /* Now len % 8 == 0 */
2929
2930 for( ; end_bits != 0; end_bits-- )
2931 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2932 }
2933
2934 memset( mask + offset / 8, 0xFF, len / 8 );
2935}
2936
2937/*
2938 * Check that bitmask is full
2939 */
2940static int ssl_bitmask_check( unsigned char *mask, size_t len )
2941{
2942 size_t i;
2943
2944 for( i = 0; i < len / 8; i++ )
2945 if( mask[i] != 0xFF )
2946 return( -1 );
2947
2948 for( i = 0; i < len % 8; i++ )
2949 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2950 return( -1 );
2951
2952 return( 0 );
2953}
2954
Hanno Becker56e205e2018-08-16 09:06:12 +01002955/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002956static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002957 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002958{
Hanno Becker56e205e2018-08-16 09:06:12 +01002959 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002960
Hanno Becker56e205e2018-08-16 09:06:12 +01002961 alloc_len = 12; /* Handshake header */
2962 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002963
Hanno Beckerd07df862018-08-16 09:14:58 +01002964 if( add_bitmap )
2965 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002966
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002967 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002968}
Hanno Becker56e205e2018-08-16 09:06:12 +01002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002971
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002972static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002973{
2974 return( ( ssl->in_msg[1] << 16 ) |
2975 ( ssl->in_msg[2] << 8 ) |
2976 ssl->in_msg[3] );
2977}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002978
Simon Butcher99000142016-10-13 17:21:01 +01002979int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002984 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002986 }
2987
Hanno Becker12555c62018-08-16 12:47:53 +01002988 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002991 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002992 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002996 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002998 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002999
Hanno Becker44650b72018-08-16 12:51:11 +01003000 if( ssl_check_hs_header( ssl ) != 0 )
3001 {
3002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3003 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3004 }
3005
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003006 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003007 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3008 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3009 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3010 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003011 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003012 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3013 {
3014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3015 recv_msg_seq,
3016 ssl->handshake->in_msg_seq ) );
3017 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3018 }
3019
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003020 /* Retransmit only on last message from previous flight, to avoid
3021 * too many retransmissions.
3022 * Besides, No sane server ever retransmits HelloVerifyRequest */
3023 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003027 "message_seq = %d, start_of_flight = %d",
3028 recv_msg_seq,
3029 ssl->handshake->in_flight_start_seq ) );
3030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003034 return( ret );
3035 }
3036 }
3037 else
3038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003040 "message_seq = %d, expected = %d",
3041 recv_msg_seq,
3042 ssl->handshake->in_msg_seq ) );
3043 }
3044
Hanno Becker90333da2017-10-10 11:27:13 +01003045 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003046 }
3047 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003048
Hanno Becker6d97ef52018-08-16 13:09:04 +01003049 /* Message reassembly is handled alongside buffering of future
3050 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003051 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003052 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003053 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003056 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003057 }
3058 }
3059 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003061 /* With TLS we don't handle fragmentation (for now) */
3062 if( ssl->in_msglen < ssl->in_hslen )
3063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3065 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003066 }
3067
Simon Butcher99000142016-10-13 17:21:01 +01003068 return( 0 );
3069}
3070
3071void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3072{
Hanno Becker0271f962018-08-16 13:23:47 +01003073 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003074
Hanno Becker0271f962018-08-16 13:23:47 +01003075 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003076 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003077 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003078 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003079
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003080 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003083 ssl->handshake != NULL )
3084 {
Hanno Becker0271f962018-08-16 13:23:47 +01003085 unsigned offset;
3086 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003087
Hanno Becker0271f962018-08-16 13:23:47 +01003088 /* Increment handshake sequence number */
3089 hs->in_msg_seq++;
3090
3091 /*
3092 * Clear up handshake buffering and reassembly structure.
3093 */
3094
3095 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003096 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003097
3098 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003099 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3100 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003101 offset++, hs_buf++ )
3102 {
3103 *hs_buf = *(hs_buf + 1);
3104 }
3105
3106 /* Create a fresh last entry */
3107 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003108 }
3109#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003110}
3111
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003112/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003113 * DTLS anti-replay: RFC 6347 4.1.2.6
3114 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003115 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3116 * Bit n is set iff record number in_window_top - n has been seen.
3117 *
3118 * Usually, in_window_top is the last record number seen and the lsb of
3119 * in_window is set. The only exception is the initial state (record number 0
3120 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003123void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003124{
3125 ssl->in_window_top = 0;
3126 ssl->in_window = 0;
3127}
3128
3129static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3130{
3131 return( ( (uint64_t) buf[0] << 40 ) |
3132 ( (uint64_t) buf[1] << 32 ) |
3133 ( (uint64_t) buf[2] << 24 ) |
3134 ( (uint64_t) buf[3] << 16 ) |
3135 ( (uint64_t) buf[4] << 8 ) |
3136 ( (uint64_t) buf[5] ) );
3137}
3138
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003139static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3140{
Janos Follath865b3eb2019-12-16 11:46:15 +00003141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003142 unsigned char *original_in_ctr;
3143
3144 // save original in_ctr
3145 original_in_ctr = ssl->in_ctr;
3146
3147 // use counter from record
3148 ssl->in_ctr = record_in_ctr;
3149
3150 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3151
3152 // restore the counter
3153 ssl->in_ctr = original_in_ctr;
3154
3155 return ret;
3156}
3157
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003158/*
3159 * Return 0 if sequence number is acceptable, -1 otherwise
3160 */
Hanno Becker0183d692019-07-12 08:50:37 +01003161int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003162{
3163 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3164 uint64_t bit;
3165
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003166 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003167 return( 0 );
3168
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003169 if( rec_seqnum > ssl->in_window_top )
3170 return( 0 );
3171
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003172 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003173
3174 if( bit >= 64 )
3175 return( -1 );
3176
3177 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3178 return( -1 );
3179
3180 return( 0 );
3181}
3182
3183/*
3184 * Update replay window on new validated record
3185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003187{
3188 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3189
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003190 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003191 return;
3192
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193 if( rec_seqnum > ssl->in_window_top )
3194 {
3195 /* Update window_top and the contents of the window */
3196 uint64_t shift = rec_seqnum - ssl->in_window_top;
3197
3198 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003199 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003200 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003201 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003202 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003203 ssl->in_window |= 1;
3204 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003205
3206 ssl->in_window_top = rec_seqnum;
3207 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003208 else
3209 {
3210 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003211 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003212
3213 if( bit < 64 ) /* Always true, but be extra sure */
3214 ssl->in_window |= (uint64_t) 1 << bit;
3215 }
3216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003218
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003219#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003220/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003221 * Without any SSL context, check if a datagram looks like a ClientHello with
3222 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003223 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003224 *
3225 * - if cookie is valid, return 0
3226 * - if ClientHello looks superficially valid but cookie is not,
3227 * fill obuf and set olen, then
3228 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3229 * - otherwise return a specific error code
3230 */
3231static int ssl_check_dtls_clihlo_cookie(
3232 mbedtls_ssl_cookie_write_t *f_cookie_write,
3233 mbedtls_ssl_cookie_check_t *f_cookie_check,
3234 void *p_cookie,
3235 const unsigned char *cli_id, size_t cli_id_len,
3236 const unsigned char *in, size_t in_len,
3237 unsigned char *obuf, size_t buf_len, size_t *olen )
3238{
3239 size_t sid_len, cookie_len;
3240 unsigned char *p;
3241
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003242 /*
3243 * Structure of ClientHello with record and handshake headers,
3244 * and expected values. We don't need to check a lot, more checks will be
3245 * done when actually parsing the ClientHello - skipping those checks
3246 * avoids code duplication and does not make cookie forging any easier.
3247 *
3248 * 0-0 ContentType type; copied, must be handshake
3249 * 1-2 ProtocolVersion version; copied
3250 * 3-4 uint16 epoch; copied, must be 0
3251 * 5-10 uint48 sequence_number; copied
3252 * 11-12 uint16 length; (ignored)
3253 *
3254 * 13-13 HandshakeType msg_type; (ignored)
3255 * 14-16 uint24 length; (ignored)
3256 * 17-18 uint16 message_seq; copied
3257 * 19-21 uint24 fragment_offset; copied, must be 0
3258 * 22-24 uint24 fragment_length; (ignored)
3259 *
3260 * 25-26 ProtocolVersion client_version; (ignored)
3261 * 27-58 Random random; (ignored)
3262 * 59-xx SessionID session_id; 1 byte len + sid_len content
3263 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3264 * ...
3265 *
3266 * Minimum length is 61 bytes.
3267 */
3268 if( in_len < 61 ||
3269 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3270 in[3] != 0 || in[4] != 0 ||
3271 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3272 {
3273 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3274 }
3275
3276 sid_len = in[59];
3277 if( sid_len > in_len - 61 )
3278 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3279
3280 cookie_len = in[60 + sid_len];
3281 if( cookie_len > in_len - 60 )
3282 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3283
3284 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3285 cli_id, cli_id_len ) == 0 )
3286 {
3287 /* Valid cookie */
3288 return( 0 );
3289 }
3290
3291 /*
3292 * If we get here, we've got an invalid cookie, let's prepare HVR.
3293 *
3294 * 0-0 ContentType type; copied
3295 * 1-2 ProtocolVersion version; copied
3296 * 3-4 uint16 epoch; copied
3297 * 5-10 uint48 sequence_number; copied
3298 * 11-12 uint16 length; olen - 13
3299 *
3300 * 13-13 HandshakeType msg_type; hello_verify_request
3301 * 14-16 uint24 length; olen - 25
3302 * 17-18 uint16 message_seq; copied
3303 * 19-21 uint24 fragment_offset; copied
3304 * 22-24 uint24 fragment_length; olen - 25
3305 *
3306 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3307 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3308 *
3309 * Minimum length is 28.
3310 */
3311 if( buf_len < 28 )
3312 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3313
3314 /* Copy most fields and adapt others */
3315 memcpy( obuf, in, 25 );
3316 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3317 obuf[25] = 0xfe;
3318 obuf[26] = 0xff;
3319
3320 /* Generate and write actual cookie */
3321 p = obuf + 28;
3322 if( f_cookie_write( p_cookie,
3323 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3324 {
3325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3326 }
3327
3328 *olen = p - obuf;
3329
3330 /* Go back and fill length fields */
3331 obuf[27] = (unsigned char)( *olen - 28 );
3332
3333 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3334 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3335 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3336
3337 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3338 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3339
3340 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3341}
3342
3343/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003344 * Handle possible client reconnect with the same UDP quadruplet
3345 * (RFC 6347 Section 4.2.8).
3346 *
3347 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3348 * that looks like a ClientHello.
3349 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003350 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003351 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003352 * - if the input looks like a ClientHello with a valid cookie,
3353 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003354 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003355 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003356 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003357 * This function is called (through ssl_check_client_reconnect()) when an
3358 * unexpected record is found in ssl_get_next_record(), which will discard the
3359 * record if we return 0, and bubble up the return value otherwise (this
3360 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3361 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003362 */
3363static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3364{
Janos Follath865b3eb2019-12-16 11:46:15 +00003365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003366 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003367
Hanno Becker2fddd372019-07-10 14:37:41 +01003368 if( ssl->conf->f_cookie_write == NULL ||
3369 ssl->conf->f_cookie_check == NULL )
3370 {
3371 /* If we can't use cookies to verify reachability of the peer,
3372 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3374 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003375 return( 0 );
3376 }
3377
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003378 ret = ssl_check_dtls_clihlo_cookie(
3379 ssl->conf->f_cookie_write,
3380 ssl->conf->f_cookie_check,
3381 ssl->conf->p_cookie,
3382 ssl->cli_id, ssl->cli_id_len,
3383 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003384 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003385
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003386 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3387
3388 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003389 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003390 int send_ret;
3391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3392 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3393 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003394 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003395 * If the error is permanent we'll catch it later,
3396 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003397 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3398 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3399 (void) send_ret;
3400
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003401 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003402 }
3403
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003404 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003405 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003407 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003408 {
3409 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3410 return( ret );
3411 }
3412
3413 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003414 }
3415
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003416 return( ret );
3417}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003418#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003419
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003420static int ssl_check_record_type( uint8_t record_type )
3421{
3422 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3423 record_type != MBEDTLS_SSL_MSG_ALERT &&
3424 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3425 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3426 {
3427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3428 }
3429
3430 return( 0 );
3431}
3432
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003433/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003434 * ContentType type;
3435 * ProtocolVersion version;
3436 * uint16 epoch; // DTLS only
3437 * uint48 sequence_number; // DTLS only
3438 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003439 *
3440 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003441 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003442 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3443 *
3444 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003445 * 1. proceed with the record if this function returns 0
3446 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3447 * 3. return CLIENT_RECONNECT if this function return that value
3448 * 4. drop the whole datagram if this function returns anything else.
3449 * Point 2 is needed when the peer is resending, and we have already received
3450 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003451 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003452static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003453 unsigned char *buf,
3454 size_t len,
3455 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003456{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003457 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Hanno Beckere5e7e782019-07-11 12:29:35 +01003459 size_t const rec_hdr_type_offset = 0;
3460 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003461
Hanno Beckere5e7e782019-07-11 12:29:35 +01003462 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3463 rec_hdr_type_len;
3464 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
Hanno Beckere5e7e782019-07-11 12:29:35 +01003466 size_t const rec_hdr_ctr_len = 8;
3467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003468 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003469 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3470 rec_hdr_version_len;
3471
Hanno Beckera0e20d02019-05-15 14:03:01 +01003472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003473 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3474 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003475 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003476#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3477#endif /* MBEDTLS_SSL_PROTO_DTLS */
3478
3479 size_t rec_hdr_len_offset; /* To be determined */
3480 size_t const rec_hdr_len_len = 2;
3481
3482 /*
3483 * Check minimum lengths for record header.
3484 */
3485
3486#if defined(MBEDTLS_SSL_PROTO_DTLS)
3487 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3488 {
3489 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3490 }
3491 else
3492#endif /* MBEDTLS_SSL_PROTO_DTLS */
3493 {
3494 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3495 }
3496
3497 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3498 {
3499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3500 (unsigned) len,
3501 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3502 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3503 }
3504
3505 /*
3506 * Parse and validate record content type
3507 */
3508
3509 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003510
3511 /* Check record content type */
3512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3513 rec->cid_len = 0;
3514
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003515 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003516 ssl->conf->cid_len != 0 &&
3517 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003518 {
3519 /* Shift pointers to account for record header including CID
3520 * struct {
3521 * ContentType special_type = tls12_cid;
3522 * ProtocolVersion version;
3523 * uint16 epoch;
3524 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003525 * opaque cid[cid_length]; // Additional field compared to
3526 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003527 * uint16 length;
3528 * opaque enc_content[DTLSCiphertext.length];
3529 * } DTLSCiphertext;
3530 */
3531
3532 /* So far, we only support static CID lengths
3533 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003534 rec_hdr_cid_len = ssl->conf->cid_len;
3535 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003536
Hanno Beckere5e7e782019-07-11 12:29:35 +01003537 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003538 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3540 (unsigned) len,
3541 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003543 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003544
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003545 /* configured CID len is guaranteed at most 255, see
3546 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3547 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003548 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003549 }
3550 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003551#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003552 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003553 if( ssl_check_record_type( rec->type ) )
3554 {
Hanno Becker54229812019-07-12 14:40:00 +01003555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3556 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3558 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003559 }
3560
Hanno Beckere5e7e782019-07-11 12:29:35 +01003561 /*
3562 * Parse and validate record version
3563 */
3564
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003565 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3566 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003567 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3568 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003569 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003570
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003571 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3574 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 }
3576
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003577 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3580 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 }
3582
Hanno Beckere5e7e782019-07-11 12:29:35 +01003583 /*
3584 * Parse/Copy record sequence number.
3585 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003586
Hanno Beckere5e7e782019-07-11 12:29:35 +01003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
3588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003589 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003590 /* Copy explicit record sequence number from input buffer. */
3591 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3592 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003593 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003594 else
3595#endif /* MBEDTLS_SSL_PROTO_DTLS */
3596 {
3597 /* Copy implicit record sequence number from SSL context structure. */
3598 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3599 }
Paul Bakker40e46942009-01-03 21:51:57 +00003600
Hanno Beckere5e7e782019-07-11 12:29:35 +01003601 /*
3602 * Parse record length.
3603 */
3604
Hanno Beckere5e7e782019-07-11 12:29:35 +01003605 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003606 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3607 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003608 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003611 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003612 rec->type,
3613 major_ver, minor_ver, rec->data_len ) );
3614
3615 rec->buf = buf;
3616 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003617
Hanno Beckerd417cc92019-07-26 08:20:27 +01003618 if( rec->data_len == 0 )
3619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003621 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003622 * DTLS-related tests.
3623 * Check epoch before checking length constraint because
3624 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3625 * message gets duplicated before the corresponding Finished message,
3626 * the second ChangeCipherSpec should be discarded because it belongs
3627 * to an old epoch, but not because its length is shorter than
3628 * the minimum record length for packets using the new record transform.
3629 * Note that these two kinds of failures are handled differently,
3630 * as an unexpected record is silently skipped but an invalid
3631 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003632 */
3633#if defined(MBEDTLS_SSL_PROTO_DTLS)
3634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3635 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003636 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003637
Hanno Becker955a5c92019-07-10 17:12:07 +01003638 /* Check that the datagram is large enough to contain a record
3639 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003640 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003641 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3643 (unsigned) len,
3644 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003645 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3646 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003647
Hanno Becker37cfe732019-07-10 17:20:01 +01003648 /* Records from other, non-matching epochs are silently discarded.
3649 * (The case of same-port Client reconnects must be considered in
3650 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003651 if( rec_epoch != ssl->in_epoch )
3652 {
3653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3654 "expected %d, received %d",
3655 ssl->in_epoch, rec_epoch ) );
3656
Hanno Becker552f7472019-07-19 10:59:12 +01003657 /* Records from the next epoch are considered for buffering
3658 * (concretely: early Finished messages). */
3659 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003660 {
Hanno Becker552f7472019-07-19 10:59:12 +01003661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3662 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003663 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003664
Hanno Becker2fddd372019-07-10 14:37:41 +01003665 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003666 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003667#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003668 /* For records from the correct epoch, check whether their
3669 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003670 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3671 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003672 {
3673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3674 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3675 }
3676#endif
3677 }
3678#endif /* MBEDTLS_SSL_PROTO_DTLS */
3679
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003680 return( 0 );
3681}
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Hanno Becker2fddd372019-07-10 14:37:41 +01003684#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3685static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3686{
3687 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3688
3689 /*
3690 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3691 * access the first byte of record content (handshake type), as we
3692 * have an active transform (possibly iv_len != 0), so use the
3693 * fact that the record header len is 13 instead.
3694 */
3695 if( rec_epoch == 0 &&
3696 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3697 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3698 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3699 ssl->in_left > 13 &&
3700 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3701 {
3702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3703 "from the same port" ) );
3704 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 }
3706
3707 return( 0 );
3708}
Hanno Becker2fddd372019-07-10 14:37:41 +01003709#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003711/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003712 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003713 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003714static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3715 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003716{
3717 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003720 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3723 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 ret = mbedtls_ssl_hw_record_read( ssl );
3728 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3731 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003732 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003733
3734 if( ret == 0 )
3735 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003736 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003738 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003740 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003741
Hanno Beckera18d1322018-01-03 14:27:32 +00003742 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003743 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003746
Hanno Beckera0e20d02019-05-15 14:03:01 +01003747#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003748 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3749 ssl->conf->ignore_unexpected_cid
3750 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3751 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003753 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003754 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003755#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003756
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 return( ret );
3758 }
3759
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003760 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003761 {
3762 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003763 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003764 }
3765
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003766 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003767 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003768
Hanno Beckera0e20d02019-05-15 14:03:01 +01003769#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003770 /* We have already checked the record content type
3771 * in ssl_parse_record_header(), failing or silently
3772 * dropping the record in the case of an unknown type.
3773 *
3774 * Since with the use of CIDs, the record content type
3775 * might change during decryption, re-check the record
3776 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003777 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003778 {
3779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3780 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3781 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003782#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003783
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003784 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003785 {
3786#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3787 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003788 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003789 {
3790 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3792 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3793 }
3794#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3795
3796 ssl->nb_zero++;
3797
3798 /*
3799 * Three or more empty messages may be a DoS attack
3800 * (excessive CPU consumption).
3801 */
3802 if( ssl->nb_zero > 3 )
3803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003805 "messages, possible DoS attack" ) );
3806 /* Treat the records as if they were not properly authenticated,
3807 * thereby failing the connection if we see more than allowed
3808 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003809 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3810 }
3811 }
3812 else
3813 ssl->nb_zero = 0;
3814
3815#if defined(MBEDTLS_SSL_PROTO_DTLS)
3816 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3817 {
3818 ; /* in_ctr read from peer, not maintained internally */
3819 }
3820 else
3821#endif
3822 {
3823 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003824 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003825 if( ++ssl->in_ctr[i - 1] != 0 )
3826 break;
3827
3828 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003829 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003830 {
3831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3832 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3833 }
3834 }
3835
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 }
3837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003842 }
3843#endif
3844
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003845 /* Check actual (decrypted) record content length against
3846 * configured maximum. */
3847 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3848 {
3849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3850 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3851 }
3852
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003853 return( 0 );
3854}
3855
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003856/*
3857 * Read a record.
3858 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003859 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3860 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3861 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003862 */
Hanno Becker1097b342018-08-15 14:09:41 +01003863
3864/* Helper functions for mbedtls_ssl_read_record(). */
3865static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003866static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3867static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003868
Hanno Becker327c93b2018-08-15 13:56:18 +01003869int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003870 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003871{
Janos Follath865b3eb2019-12-16 11:46:15 +00003872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003875
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003876 if( ssl->keep_current_message == 0 )
3877 {
3878 do {
Simon Butcher99000142016-10-13 17:21:01 +01003879
Hanno Becker26994592018-08-15 14:14:59 +01003880 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003881 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003882 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003883
Hanno Beckere74d5562018-08-15 14:26:08 +01003884 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003885 {
Hanno Becker40f50842018-08-15 14:48:01 +01003886#if defined(MBEDTLS_SSL_PROTO_DTLS)
3887 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003888
Hanno Becker40f50842018-08-15 14:48:01 +01003889 /* We only check for buffered messages if the
3890 * current datagram is fully consumed. */
3891 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003892 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003893 {
Hanno Becker40f50842018-08-15 14:48:01 +01003894 if( ssl_load_buffered_message( ssl ) == 0 )
3895 have_buffered = 1;
3896 }
3897
3898 if( have_buffered == 0 )
3899#endif /* MBEDTLS_SSL_PROTO_DTLS */
3900 {
3901 ret = ssl_get_next_record( ssl );
3902 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3903 continue;
3904
3905 if( ret != 0 )
3906 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003907 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003908 return( ret );
3909 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003910 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003911 }
3912
3913 ret = mbedtls_ssl_handle_message_type( ssl );
3914
Hanno Becker40f50842018-08-15 14:48:01 +01003915#if defined(MBEDTLS_SSL_PROTO_DTLS)
3916 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3917 {
3918 /* Buffer future message */
3919 ret = ssl_buffer_message( ssl );
3920 if( ret != 0 )
3921 return( ret );
3922
3923 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3924 }
3925#endif /* MBEDTLS_SSL_PROTO_DTLS */
3926
Hanno Becker90333da2017-10-10 11:27:13 +01003927 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3928 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003929
3930 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003931 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003932 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003933 return( ret );
3934 }
3935
Hanno Becker327c93b2018-08-15 13:56:18 +01003936 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003937 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003938 {
3939 mbedtls_ssl_update_handshake_status( ssl );
3940 }
Simon Butcher99000142016-10-13 17:21:01 +01003941 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003942 else
Simon Butcher99000142016-10-13 17:21:01 +01003943 {
Hanno Becker02f59072018-08-15 14:00:24 +01003944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003945 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003946 }
3947
3948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3949
3950 return( 0 );
3951}
3952
Hanno Becker40f50842018-08-15 14:48:01 +01003953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003954static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003955{
Hanno Becker40f50842018-08-15 14:48:01 +01003956 if( ssl->in_left > ssl->next_record_offset )
3957 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003958
Hanno Becker40f50842018-08-15 14:48:01 +01003959 return( 0 );
3960}
3961
3962static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3963{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003964 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003965 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003966 int ret = 0;
3967
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003968 if( hs == NULL )
3969 return( -1 );
3970
Hanno Beckere00ae372018-08-20 09:39:42 +01003971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3972
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003973 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3974 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3975 {
3976 /* Check if we have seen a ChangeCipherSpec before.
3977 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003978 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003979 {
3980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3981 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003982 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003983 }
3984
Hanno Becker39b8bc92018-08-28 17:17:13 +01003985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003986 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3987 ssl->in_msglen = 1;
3988 ssl->in_msg[0] = 1;
3989
3990 /* As long as they are equal, the exact value doesn't matter. */
3991 ssl->in_left = 0;
3992 ssl->next_record_offset = 0;
3993
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003994 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003995 goto exit;
3996 }
Hanno Becker37f95322018-08-16 13:55:32 +01003997
Hanno Beckerb8f50142018-08-28 10:01:34 +01003998#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003999 /* Debug only */
4000 {
4001 unsigned offset;
4002 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4003 {
4004 hs_buf = &hs->buffering.hs[offset];
4005 if( hs_buf->is_valid == 1 )
4006 {
4007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4008 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004009 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004010 }
4011 }
4012 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004013#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004014
4015 /* Check if we have buffered and/or fully reassembled the
4016 * next handshake message. */
4017 hs_buf = &hs->buffering.hs[0];
4018 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4019 {
4020 /* Synthesize a record containing the buffered HS message. */
4021 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4022 ( hs_buf->data[2] << 8 ) |
4023 hs_buf->data[3];
4024
4025 /* Double-check that we haven't accidentally buffered
4026 * a message that doesn't fit into the input buffer. */
4027 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4028 {
4029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4031 }
4032
4033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4034 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4035 hs_buf->data, msg_len + 12 );
4036
4037 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4038 ssl->in_hslen = msg_len + 12;
4039 ssl->in_msglen = msg_len + 12;
4040 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4041
4042 ret = 0;
4043 goto exit;
4044 }
4045 else
4046 {
4047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4048 hs->in_msg_seq ) );
4049 }
4050
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004051 ret = -1;
4052
4053exit:
4054
4055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4056 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004057}
4058
Hanno Beckera02b0b42018-08-21 17:20:27 +01004059static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4060 size_t desired )
4061{
4062 int offset;
4063 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4065 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004066
Hanno Becker01315ea2018-08-21 17:22:17 +01004067 /* Get rid of future records epoch first, if such exist. */
4068 ssl_free_buffered_record( ssl );
4069
4070 /* Check if we have enough space available now. */
4071 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4072 hs->buffering.total_bytes_buffered ) )
4073 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004075 return( 0 );
4076 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004077
Hanno Becker4f432ad2018-08-28 10:02:32 +01004078 /* We don't have enough space to buffer the next expected handshake
4079 * message. Remove buffers used for future messages to gain space,
4080 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004081 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4082 offset >= 0; offset-- )
4083 {
4084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4085 offset ) );
4086
Hanno Beckerb309b922018-08-23 13:18:05 +01004087 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004088
4089 /* Check if we have enough space available now. */
4090 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4091 hs->buffering.total_bytes_buffered ) )
4092 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004094 return( 0 );
4095 }
4096 }
4097
4098 return( -1 );
4099}
4100
Hanno Becker40f50842018-08-15 14:48:01 +01004101static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4102{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004103 int ret = 0;
4104 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4105
4106 if( hs == NULL )
4107 return( 0 );
4108
4109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4110
4111 switch( ssl->in_msgtype )
4112 {
4113 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004115
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004116 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004117 break;
4118
4119 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004120 {
4121 unsigned recv_msg_seq_offset;
4122 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4123 mbedtls_ssl_hs_buffer *hs_buf;
4124 size_t msg_len = ssl->in_hslen - 12;
4125
4126 /* We should never receive an old handshake
4127 * message - double-check nonetheless. */
4128 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4132 }
4133
4134 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4135 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4136 {
4137 /* Silently ignore -- message too far in the future */
4138 MBEDTLS_SSL_DEBUG_MSG( 2,
4139 ( "Ignore future HS message with sequence number %u, "
4140 "buffering window %u - %u",
4141 recv_msg_seq, ssl->handshake->in_msg_seq,
4142 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4143
4144 goto exit;
4145 }
4146
4147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4148 recv_msg_seq, recv_msg_seq_offset ) );
4149
4150 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4151
4152 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004153 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004154 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004155 size_t reassembly_buf_sz;
4156
Hanno Becker37f95322018-08-16 13:55:32 +01004157 hs_buf->is_fragmented =
4158 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4159
4160 /* We copy the message back into the input buffer
4161 * after reassembly, so check that it's not too large.
4162 * This is an implementation-specific limitation
4163 * and not one from the standard, hence it is not
4164 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004165 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004166 {
4167 /* Ignore message */
4168 goto exit;
4169 }
4170
Hanno Beckere0b150f2018-08-21 15:51:03 +01004171 /* Check if we have enough space to buffer the message. */
4172 if( hs->buffering.total_bytes_buffered >
4173 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4177 }
4178
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004179 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4180 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004181
4182 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4183 hs->buffering.total_bytes_buffered ) )
4184 {
4185 if( recv_msg_seq_offset > 0 )
4186 {
4187 /* If we can't buffer a future message because
4188 * of space limitations -- ignore. */
4189 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",
4190 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4191 (unsigned) hs->buffering.total_bytes_buffered ) );
4192 goto exit;
4193 }
Hanno Beckere1801392018-08-21 16:51:05 +01004194 else
4195 {
4196 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",
4197 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4198 (unsigned) hs->buffering.total_bytes_buffered ) );
4199 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004200
Hanno Beckera02b0b42018-08-21 17:20:27 +01004201 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004202 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004203 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",
4204 (unsigned) msg_len,
4205 (unsigned) reassembly_buf_sz,
4206 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004207 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004208 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4209 goto exit;
4210 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004211 }
4212
4213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4214 msg_len ) );
4215
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004216 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4217 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004218 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004219 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004220 goto exit;
4221 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004222 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004223
4224 /* Prepare final header: copy msg_type, length and message_seq,
4225 * then add standardised fragment_offset and fragment_length */
4226 memcpy( hs_buf->data, ssl->in_msg, 6 );
4227 memset( hs_buf->data + 6, 0, 3 );
4228 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4229
4230 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004231
4232 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004233 }
4234 else
4235 {
4236 /* Make sure msg_type and length are consistent */
4237 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4240 /* Ignore */
4241 goto exit;
4242 }
4243 }
4244
Hanno Becker4422bbb2018-08-20 09:40:19 +01004245 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004246 {
4247 size_t frag_len, frag_off;
4248 unsigned char * const msg = hs_buf->data + 12;
4249
4250 /*
4251 * Check and copy current fragment
4252 */
4253
4254 /* Validation of header fields already done in
4255 * mbedtls_ssl_prepare_handshake_record(). */
4256 frag_off = ssl_get_hs_frag_off( ssl );
4257 frag_len = ssl_get_hs_frag_len( ssl );
4258
4259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4260 frag_off, frag_len ) );
4261 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4262
4263 if( hs_buf->is_fragmented )
4264 {
4265 unsigned char * const bitmask = msg + msg_len;
4266 ssl_bitmask_set( bitmask, frag_off, frag_len );
4267 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4268 msg_len ) == 0 );
4269 }
4270 else
4271 {
4272 hs_buf->is_complete = 1;
4273 }
4274
4275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4276 hs_buf->is_complete ? "" : "not yet " ) );
4277 }
4278
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004279 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004280 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004281
4282 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004283 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004284 break;
4285 }
4286
4287exit:
4288
4289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4290 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004291}
4292#endif /* MBEDTLS_SSL_PROTO_DTLS */
4293
Hanno Becker1097b342018-08-15 14:09:41 +01004294static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004295{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004296 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004297 * Consume last content-layer message and potentially
4298 * update in_msglen which keeps track of the contents'
4299 * consumption state.
4300 *
4301 * (1) Handshake messages:
4302 * Remove last handshake message, move content
4303 * and adapt in_msglen.
4304 *
4305 * (2) Alert messages:
4306 * Consume whole record content, in_msglen = 0.
4307 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004308 * (3) Change cipher spec:
4309 * Consume whole record content, in_msglen = 0.
4310 *
4311 * (4) Application data:
4312 * Don't do anything - the record layer provides
4313 * the application data as a stream transport
4314 * and consumes through mbedtls_ssl_read only.
4315 *
4316 */
4317
4318 /* Case (1): Handshake messages */
4319 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004320 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004321 /* Hard assertion to be sure that no application data
4322 * is in flight, as corrupting ssl->in_msglen during
4323 * ssl->in_offt != NULL is fatal. */
4324 if( ssl->in_offt != NULL )
4325 {
4326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4328 }
4329
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004330 /*
4331 * Get next Handshake message in the current record
4332 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004333
Hanno Becker4a810fb2017-05-24 16:27:30 +01004334 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004335 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004336 * current handshake content: If DTLS handshake
4337 * fragmentation is used, that's the fragment
4338 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004339 * size here is faulty and should be changed at
4340 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004341 * (2) While it doesn't seem to cause problems, one
4342 * has to be very careful not to assume that in_hslen
4343 * is always <= in_msglen in a sensible communication.
4344 * Again, it's wrong for DTLS handshake fragmentation.
4345 * The following check is therefore mandatory, and
4346 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004347 * Additionally, ssl->in_hslen might be arbitrarily out of
4348 * bounds after handling a DTLS message with an unexpected
4349 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004350 */
4351 if( ssl->in_hslen < ssl->in_msglen )
4352 {
4353 ssl->in_msglen -= ssl->in_hslen;
4354 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4355 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004356
Hanno Becker4a810fb2017-05-24 16:27:30 +01004357 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4358 ssl->in_msg, ssl->in_msglen );
4359 }
4360 else
4361 {
4362 ssl->in_msglen = 0;
4363 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004364
Hanno Becker4a810fb2017-05-24 16:27:30 +01004365 ssl->in_hslen = 0;
4366 }
4367 /* Case (4): Application data */
4368 else if( ssl->in_offt != NULL )
4369 {
4370 return( 0 );
4371 }
4372 /* Everything else (CCS & Alerts) */
4373 else
4374 {
4375 ssl->in_msglen = 0;
4376 }
4377
Hanno Becker1097b342018-08-15 14:09:41 +01004378 return( 0 );
4379}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004380
Hanno Beckere74d5562018-08-15 14:26:08 +01004381static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4382{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004383 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004384 return( 1 );
4385
4386 return( 0 );
4387}
4388
Hanno Becker5f066e72018-08-16 14:56:31 +01004389#if defined(MBEDTLS_SSL_PROTO_DTLS)
4390
4391static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4392{
4393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4394 if( hs == NULL )
4395 return;
4396
Hanno Becker01315ea2018-08-21 17:22:17 +01004397 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004398 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004399 hs->buffering.total_bytes_buffered -=
4400 hs->buffering.future_record.len;
4401
4402 mbedtls_free( hs->buffering.future_record.data );
4403 hs->buffering.future_record.data = NULL;
4404 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004405}
4406
4407static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4408{
4409 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4410 unsigned char * rec;
4411 size_t rec_len;
4412 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004413#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4414 size_t in_buf_len = ssl->in_buf_len;
4415#else
4416 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4417#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004418 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4419 return( 0 );
4420
4421 if( hs == NULL )
4422 return( 0 );
4423
Hanno Becker5f066e72018-08-16 14:56:31 +01004424 rec = hs->buffering.future_record.data;
4425 rec_len = hs->buffering.future_record.len;
4426 rec_epoch = hs->buffering.future_record.epoch;
4427
4428 if( rec == NULL )
4429 return( 0 );
4430
Hanno Becker4cb782d2018-08-20 11:19:05 +01004431 /* Only consider loading future records if the
4432 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004433 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004434 return( 0 );
4435
Hanno Becker5f066e72018-08-16 14:56:31 +01004436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4437
4438 if( rec_epoch != ssl->in_epoch )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4441 goto exit;
4442 }
4443
4444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4445
4446 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004447 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4451 }
4452
4453 memcpy( ssl->in_hdr, rec, rec_len );
4454 ssl->in_left = rec_len;
4455 ssl->next_record_offset = 0;
4456
4457 ssl_free_buffered_record( ssl );
4458
4459exit:
4460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4461 return( 0 );
4462}
4463
Hanno Becker519f15d2019-07-11 12:43:20 +01004464static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4465 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004466{
4467 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004468
4469 /* Don't buffer future records outside handshakes. */
4470 if( hs == NULL )
4471 return( 0 );
4472
4473 /* Only buffer handshake records (we are only interested
4474 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004475 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004476 return( 0 );
4477
4478 /* Don't buffer more than one future epoch record. */
4479 if( hs->buffering.future_record.data != NULL )
4480 return( 0 );
4481
Hanno Becker01315ea2018-08-21 17:22:17 +01004482 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004483 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004484 hs->buffering.total_bytes_buffered ) )
4485 {
4486 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 +01004487 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004488 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004489 return( 0 );
4490 }
4491
Hanno Becker5f066e72018-08-16 14:56:31 +01004492 /* Buffer record */
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4494 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004495 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004496
4497 /* ssl_parse_record_header() only considers records
4498 * of the next epoch as candidates for buffering. */
4499 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004500 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004501
4502 hs->buffering.future_record.data =
4503 mbedtls_calloc( 1, hs->buffering.future_record.len );
4504 if( hs->buffering.future_record.data == NULL )
4505 {
4506 /* If we run out of RAM trying to buffer a
4507 * record from the next epoch, just ignore. */
4508 return( 0 );
4509 }
4510
Hanno Becker519f15d2019-07-11 12:43:20 +01004511 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004512
Hanno Becker519f15d2019-07-11 12:43:20 +01004513 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004514 return( 0 );
4515}
4516
4517#endif /* MBEDTLS_SSL_PROTO_DTLS */
4518
Hanno Beckere74d5562018-08-15 14:26:08 +01004519static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004520{
Janos Follath865b3eb2019-12-16 11:46:15 +00004521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004522 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004523
Hanno Becker5f066e72018-08-16 14:56:31 +01004524#if defined(MBEDTLS_SSL_PROTO_DTLS)
4525 /* We might have buffered a future record; if so,
4526 * and if the epoch matches now, load it.
4527 * On success, this call will set ssl->in_left to
4528 * the length of the buffered record, so that
4529 * the calls to ssl_fetch_input() below will
4530 * essentially be no-ops. */
4531 ret = ssl_load_buffered_record( ssl );
4532 if( ret != 0 )
4533 return( ret );
4534#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004535
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004536 /* Ensure that we have enough space available for the default form
4537 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4538 * with no space for CIDs counted in). */
4539 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4540 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004543 return( ret );
4544 }
4545
Hanno Beckere5e7e782019-07-11 12:29:35 +01004546 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4547 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004551 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004552 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4553 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004554 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004555 if( ret != 0 )
4556 return( ret );
4557
4558 /* Fall through to handling of unexpected records */
4559 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4560 }
4561
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004562 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4563 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004564#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004565 /* Reset in pointers to default state for TLS/DTLS records,
4566 * assuming no CID and no offset between record content and
4567 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004568 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004569
Hanno Becker7ae20e02019-07-12 08:33:49 +01004570 /* Setup internal message pointers from record structure. */
4571 ssl->in_msgtype = rec.type;
4572#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4573 ssl->in_len = ssl->in_cid + rec.cid_len;
4574#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4575 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4576 ssl->in_msglen = rec.data_len;
4577
Hanno Becker2fddd372019-07-10 14:37:41 +01004578 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004579 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004580 if( ret != 0 )
4581 return( ret );
4582#endif
4583
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004584 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004585 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004586
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4588 "(header)" ) );
4589 }
4590 else
4591 {
4592 /* Skip invalid record and the rest of the datagram */
4593 ssl->next_record_offset = 0;
4594 ssl->in_left = 0;
4595
4596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4597 "(header)" ) );
4598 }
4599
4600 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004601 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004602 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004603 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004604#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004605 {
4606 return( ret );
4607 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004608 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004611 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004612 {
Hanno Beckera8814792019-07-10 15:01:45 +01004613 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004614 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004615 if( ssl->next_record_offset < ssl->in_left )
4616 {
4617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4618 }
4619 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004620 else
4621#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004622 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004623 /*
4624 * Fetch record contents from underlying transport.
4625 */
Hanno Beckera3175662019-07-11 12:50:29 +01004626 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004627 if( ret != 0 )
4628 {
4629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4630 return( ret );
4631 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004632
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004633 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004634 }
4635
4636 /*
4637 * Decrypt record contents.
4638 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004639
Hanno Beckerfdf66042019-07-11 13:07:45 +01004640 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004643 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004644 {
4645 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004646 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004647 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004648 /* Except when waiting for Finished as a bad mac here
4649 * probably means something went wrong in the handshake
4650 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4651 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4652 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4653 {
4654#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4655 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4656 {
4657 mbedtls_ssl_send_alert_message( ssl,
4658 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4659 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4660 }
4661#endif
4662 return( ret );
4663 }
4664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004666 if( ssl->conf->badmac_limit != 0 &&
4667 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4670 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004671 }
4672#endif
4673
Hanno Becker4a810fb2017-05-24 16:27:30 +01004674 /* As above, invalid records cause
4675 * dismissal of the whole datagram. */
4676
4677 ssl->next_record_offset = 0;
4678 ssl->in_left = 0;
4679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004681 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004682 }
4683
4684 return( ret );
4685 }
4686 else
4687#endif
4688 {
4689 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4691 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 mbedtls_ssl_send_alert_message( ssl,
4694 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4695 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004696 }
4697#endif
4698 return( ret );
4699 }
4700 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004701
Hanno Becker44d89b22019-07-12 09:40:44 +01004702
4703 /* Reset in pointers to default state for TLS/DTLS records,
4704 * assuming no CID and no offset between record content and
4705 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004706 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4708 ssl->in_len = ssl->in_cid + rec.cid_len;
4709#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004710 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004711
Hanno Becker8685c822019-07-12 09:37:30 +01004712 /* The record content type may change during decryption,
4713 * so re-read it. */
4714 ssl->in_msgtype = rec.type;
4715 /* Also update the input buffer, because unfortunately
4716 * the server-side ssl_parse_client_hello() reparses the
4717 * record header when receiving a ClientHello initiating
4718 * a renegotiation. */
4719 ssl->in_hdr[0] = rec.type;
4720 ssl->in_msg = rec.buf + rec.data_offset;
4721 ssl->in_msglen = rec.data_len;
4722 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4723 ssl->in_len[1] = (unsigned char)( rec.data_len );
4724
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004725#if defined(MBEDTLS_ZLIB_SUPPORT)
4726 if( ssl->transform_in != NULL &&
4727 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4728 {
4729 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4730 {
4731 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4732 return( ret );
4733 }
4734
4735 /* Check actual (decompress) record content length against
4736 * configured maximum. */
4737 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4738 {
4739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4740 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4741 }
4742 }
4743#endif /* MBEDTLS_ZLIB_SUPPORT */
4744
Simon Butcher99000142016-10-13 17:21:01 +01004745 return( 0 );
4746}
4747
4748int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4749{
Janos Follath865b3eb2019-12-16 11:46:15 +00004750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004751
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004752 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004753 * Handle particular types of records
4754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 {
Simon Butcher99000142016-10-13 17:21:01 +01004757 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4758 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004759 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 }
4762
Hanno Beckere678eaa2018-08-21 14:57:46 +01004763 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004764 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004765 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004766 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4768 ssl->in_msglen ) );
4769 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004770 }
4771
Hanno Beckere678eaa2018-08-21 14:57:46 +01004772 if( ssl->in_msg[0] != 1 )
4773 {
4774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4775 ssl->in_msg[0] ) );
4776 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4777 }
4778
4779#if defined(MBEDTLS_SSL_PROTO_DTLS)
4780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4781 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4782 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4783 {
4784 if( ssl->handshake == NULL )
4785 {
4786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4787 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4788 }
4789
4790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4791 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4792 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004793#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004794 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004798 if( ssl->in_msglen != 2 )
4799 {
4800 /* Note: Standard allows for more than one 2 byte alert
4801 to be packed in a single message, but Mbed TLS doesn't
4802 currently support this. */
4803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4804 ssl->in_msglen ) );
4805 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4806 }
4807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004809 ssl->in_msg[0], ssl->in_msg[1] ) );
4810
4811 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004812 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004817 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 }
4820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4822 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4825 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004827
4828#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4829 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4830 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4831 {
Hanno Becker90333da2017-10-10 11:27:13 +01004832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004833 /* Will be handled when trying to parse ServerHello */
4834 return( 0 );
4835 }
4836#endif
4837
4838#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4839 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4840 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4841 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4842 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4843 {
4844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4845 /* Will be handled in mbedtls_ssl_parse_certificate() */
4846 return( 0 );
4847 }
4848#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4849
4850 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004851 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
4853
Hanno Beckerc76c6192017-06-06 10:03:17 +01004854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004856 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004857 /* Drop unexpected ApplicationData records,
4858 * except at the beginning of renegotiations */
4859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4860 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4861#if defined(MBEDTLS_SSL_RENEGOTIATION)
4862 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4863 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004864#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004865 )
4866 {
4867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4868 return( MBEDTLS_ERR_SSL_NON_FATAL );
4869 }
4870
4871 if( ssl->handshake != NULL &&
4872 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4873 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004874 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004875 }
4876 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004877#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004878
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 return( 0 );
4880}
4881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004883{
irwir6c0da642019-09-26 21:07:41 +03004884 return( mbedtls_ssl_send_alert_message( ssl,
4885 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4886 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004887}
4888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004890 unsigned char level,
4891 unsigned char message )
4892{
Janos Follath865b3eb2019-12-16 11:46:15 +00004893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004894
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004895 if( ssl == NULL || ssl->conf == NULL )
4896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004902 ssl->out_msglen = 2;
4903 ssl->out_msg[0] = level;
4904 ssl->out_msg[1] = message;
4905
Hanno Becker67bc7c32018-08-06 11:33:50 +01004906 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004909 return( ret );
4910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004912
4913 return( 0 );
4914}
4915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004917{
Janos Follath865b3eb2019-12-16 11:46:15 +00004918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 ssl->out_msglen = 1;
4924 ssl->out_msg[0] = 1;
4925
Paul Bakker5121ce52009-01-03 21:22:43 +00004926 ssl->state++;
4927
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004928 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004931 return( ret );
4932 }
4933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004935
4936 return( 0 );
4937}
4938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004940{
Janos Follath865b3eb2019-12-16 11:46:15 +00004941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004944
Hanno Becker327c93b2018-08-15 13:56:18 +01004945 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 return( ret );
4949 }
4950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004954 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4955 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004957 }
4958
Hanno Beckere678eaa2018-08-21 14:57:46 +01004959 /* CCS records are only accepted if they have length 1 and content '1',
4960 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004961
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004962 /*
4963 * Switch to our negotiated transform and session parameters for inbound
4964 * data.
4965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004967 ssl->transform_in = ssl->transform_negotiate;
4968 ssl->session_in = ssl->session_negotiate;
4969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004974 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004975#endif
4976
4977 /* Increment epoch */
4978 if( ++ssl->in_epoch == 0 )
4979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004981 /* This is highly unlikely to happen for legitimate reasons, so
4982 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004984 }
4985 }
4986 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004988 memset( ssl->in_ctr, 0, 8 );
4989
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004990 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4993 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4999 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005001 }
5002 }
5003#endif
5004
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 ssl->state++;
5006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005008
5009 return( 0 );
5010}
5011
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005012/* Once ssl->out_hdr as the address of the beginning of the
5013 * next outgoing record is set, deduce the other pointers.
5014 *
5015 * Note: For TLS, we save the implicit record sequence number
5016 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5017 * and the caller has to make sure there's space for this.
5018 */
5019
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005020static size_t ssl_transform_get_explicit_iv_len(
5021 mbedtls_ssl_transform const *transform )
5022{
5023 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5024 return( 0 );
5025
5026 return( transform->ivlen - transform->fixed_ivlen );
5027}
5028
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005029void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5030 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005031{
5032#if defined(MBEDTLS_SSL_PROTO_DTLS)
5033 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5034 {
5035 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005037 ssl->out_cid = ssl->out_ctr + 8;
5038 ssl->out_len = ssl->out_cid;
5039 if( transform != NULL )
5040 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005041#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005042 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005043#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005044 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005045 }
5046 else
5047#endif
5048 {
5049 ssl->out_ctr = ssl->out_hdr - 8;
5050 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005051#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005052 ssl->out_cid = ssl->out_len;
5053#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005054 ssl->out_iv = ssl->out_hdr + 5;
5055 }
5056
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005057 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005058 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005059 if( transform != NULL )
5060 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005061}
5062
5063/* Once ssl->in_hdr as the address of the beginning of the
5064 * next incoming record is set, deduce the other pointers.
5065 *
5066 * Note: For TLS, we save the implicit record sequence number
5067 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5068 * and the caller has to make sure there's space for this.
5069 */
5070
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005071void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005072{
Hanno Becker79594fd2019-05-08 09:38:41 +01005073 /* This function sets the pointers to match the case
5074 * of unprotected TLS/DTLS records, with both ssl->in_iv
5075 * and ssl->in_msg pointing to the beginning of the record
5076 * content.
5077 *
5078 * When decrypting a protected record, ssl->in_msg
5079 * will be shifted to point to the beginning of the
5080 * record plaintext.
5081 */
5082
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005083#if defined(MBEDTLS_SSL_PROTO_DTLS)
5084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5085 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005086 /* This sets the header pointers to match records
5087 * without CID. When we receive a record containing
5088 * a CID, the fields are shifted accordingly in
5089 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005090 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005091#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005092 ssl->in_cid = ssl->in_ctr + 8;
5093 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005094#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005095 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005096#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005097 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005098 }
5099 else
5100#endif
5101 {
5102 ssl->in_ctr = ssl->in_hdr - 8;
5103 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005104#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005105 ssl->in_cid = ssl->in_len;
5106#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005107 ssl->in_iv = ssl->in_hdr + 5;
5108 }
5109
Hanno Becker79594fd2019-05-08 09:38:41 +01005110 /* This will be adjusted at record decryption time. */
5111 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005112}
5113
Paul Bakker5121ce52009-01-03 21:22:43 +00005114/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005115 * Setup an SSL context
5116 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005117
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005118void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005119{
5120 /* Set the incoming and outgoing record pointers. */
5121#if defined(MBEDTLS_SSL_PROTO_DTLS)
5122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5123 {
5124 ssl->out_hdr = ssl->out_buf;
5125 ssl->in_hdr = ssl->in_buf;
5126 }
5127 else
5128#endif /* MBEDTLS_SSL_PROTO_DTLS */
5129 {
5130 ssl->out_hdr = ssl->out_buf + 8;
5131 ssl->in_hdr = ssl->in_buf + 8;
5132 }
5133
5134 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005135 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5136 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005137}
5138
Paul Bakker5121ce52009-01-03 21:22:43 +00005139/*
5140 * SSL get accessors
5141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005143{
5144 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5145}
5146
Hanno Becker8b170a02017-10-10 11:51:19 +01005147int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5148{
5149 /*
5150 * Case A: We're currently holding back
5151 * a message for further processing.
5152 */
5153
5154 if( ssl->keep_current_message == 1 )
5155 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005157 return( 1 );
5158 }
5159
5160 /*
5161 * Case B: Further records are pending in the current datagram.
5162 */
5163
5164#if defined(MBEDTLS_SSL_PROTO_DTLS)
5165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5166 ssl->in_left > ssl->next_record_offset )
5167 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005169 return( 1 );
5170 }
5171#endif /* MBEDTLS_SSL_PROTO_DTLS */
5172
5173 /*
5174 * Case C: A handshake message is being processed.
5175 */
5176
Hanno Becker8b170a02017-10-10 11:51:19 +01005177 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5178 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005180 return( 1 );
5181 }
5182
5183 /*
5184 * Case D: An application data message is being processed
5185 */
5186 if( ssl->in_offt != NULL )
5187 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005189 return( 1 );
5190 }
5191
5192 /*
5193 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005194 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005195 * we implement support for multiple alerts in single records.
5196 */
5197
5198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5199 return( 0 );
5200}
5201
Paul Bakker43ca69c2011-01-15 17:35:19 +00005202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005204{
Hanno Becker3136ede2018-08-17 15:28:19 +01005205 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005206 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005207 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005208
Hanno Becker5903de42019-05-03 14:46:38 +01005209 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5210
Hanno Becker78640902018-08-13 16:35:15 +01005211 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005212 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214#if defined(MBEDTLS_ZLIB_SUPPORT)
5215 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5216 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005217#endif
5218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 case MBEDTLS_MODE_GCM:
5222 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005223 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005225 transform_expansion = transform->minlen;
5226 break;
5227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005229
5230 block_size = mbedtls_cipher_get_block_size(
5231 &transform->cipher_ctx_enc );
5232
Hanno Becker3136ede2018-08-17 15:28:19 +01005233 /* Expansion due to the addition of the MAC. */
5234 transform_expansion += transform->maclen;
5235
5236 /* Expansion due to the addition of CBC padding;
5237 * Theoretically up to 256 bytes, but we never use
5238 * more than the block size of the underlying cipher. */
5239 transform_expansion += block_size;
5240
5241 /* For TLS 1.1 or higher, an explicit IV is added
5242 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005243#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5244 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005245 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005246#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005247
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005248 break;
5249
5250 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005253 }
5254
Hanno Beckera0e20d02019-05-15 14:03:01 +01005255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005256 if( transform->out_cid_len != 0 )
5257 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005258#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005259
Hanno Becker5903de42019-05-03 14:46:38 +01005260 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005261}
5262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005264/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005265 * Check record counters and renegotiate if they're above the limit.
5266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005268{
Hanno Beckerdd772292020-02-05 10:38:31 +00005269 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005270 int in_ctr_cmp;
5271 int out_ctr_cmp;
5272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5274 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005275 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005276 {
5277 return( 0 );
5278 }
5279
Andres AG2196c7f2016-12-15 17:01:16 +00005280 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5281 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005282 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005283 ssl->conf->renego_period + ep_len, 8 - ep_len );
5284
5285 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005286 {
5287 return( 0 );
5288 }
5289
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005294
5295/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 * Receive application data decrypted from the SSL layer
5297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005299{
Janos Follath865b3eb2019-12-16 11:46:15 +00005300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005301 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005303 if( ssl == NULL || ssl->conf == NULL )
5304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005312 return( ret );
5313
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005314 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005316 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005317 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005318 return( ret );
5319 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005320 }
5321#endif
5322
Hanno Becker4a810fb2017-05-24 16:27:30 +01005323 /*
5324 * Check if renegotiation is necessary and/or handshake is
5325 * in process. If yes, perform/continue, and fall through
5326 * if an unexpected packet is received while the client
5327 * is waiting for the ServerHello.
5328 *
5329 * (There is no equivalent to the last condition on
5330 * the server-side as it is not treated as within
5331 * a handshake while waiting for the ClientHello
5332 * after a renegotiation request.)
5333 */
5334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005336 ret = ssl_check_ctr_renegotiate( ssl );
5337 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5338 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005341 return( ret );
5342 }
5343#endif
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005348 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5349 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 return( ret );
5353 }
5354 }
5355
Hanno Beckere41158b2017-10-23 13:30:32 +01005356 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005357 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005359 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005360 if( ssl->f_get_timer != NULL &&
5361 ssl->f_get_timer( ssl->p_timer ) == -1 )
5362 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005363 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005364 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005365
Hanno Becker327c93b2018-08-15 13:56:18 +01005366 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005368 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5369 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005370
Hanno Becker4a810fb2017-05-24 16:27:30 +01005371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5372 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005373 }
5374
5375 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 {
5378 /*
5379 * OpenSSL sends empty messages to randomize the IV
5380 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005384 return( 0 );
5385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 return( ret );
5388 }
5389 }
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005394
Hanno Becker4a810fb2017-05-24 16:27:30 +01005395 /*
5396 * - For client-side, expect SERVER_HELLO_REQUEST.
5397 * - For server-side, expect CLIENT_HELLO.
5398 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5399 */
5400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005402 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005404 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005407
5408 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005411 {
5412 continue;
5413 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005416 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005417#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005418
Hanno Becker4a810fb2017-05-24 16:27:30 +01005419#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005420 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005424
5425 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005428 {
5429 continue;
5430 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005433 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434#endif /* MBEDTLS_SSL_SRV_C */
5435
Hanno Becker21df7f92017-10-17 11:03:26 +01005436#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005437 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005438 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5439 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5440 ssl->conf->allow_legacy_renegotiation ==
5441 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5442 {
5443 /*
5444 * Accept renegotiation request
5445 */
Paul Bakker48916f92012-09-16 19:57:18 +00005446
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005447 /* DTLS clients need to know renego is server-initiated */
5448#if defined(MBEDTLS_SSL_PROTO_DTLS)
5449 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5450 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5451 {
5452 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5453 }
5454#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005455 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005456 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5457 ret != 0 )
5458 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5460 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005461 return( ret );
5462 }
5463 }
5464 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005465#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005466 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 /*
5468 * Refuse renegotiation
5469 */
5470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473#if defined(MBEDTLS_SSL_PROTO_SSL3)
5474 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005475 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005476 /* SSLv3 does not have a "no_renegotiation" warning, so
5477 we send a fatal alert and abort the connection. */
5478 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5479 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5480 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005481 }
5482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5484#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5485 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5486 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5489 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5490 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005491 {
5492 return( ret );
5493 }
Paul Bakker48916f92012-09-16 19:57:18 +00005494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005495 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5497 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005501 }
Paul Bakker48916f92012-09-16 19:57:18 +00005502 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005503
Hanno Becker90333da2017-10-10 11:27:13 +01005504 /* At this point, we don't know whether the renegotiation has been
5505 * completed or not. The cases to consider are the following:
5506 * 1) The renegotiation is complete. In this case, no new record
5507 * has been read yet.
5508 * 2) The renegotiation is incomplete because the client received
5509 * an application data record while awaiting the ServerHello.
5510 * 3) The renegotiation is incomplete because the client received
5511 * a non-handshake, non-application data message while awaiting
5512 * the ServerHello.
5513 * In each of these case, looping will be the proper action:
5514 * - For 1), the next iteration will read a new record and check
5515 * if it's application data.
5516 * - For 2), the loop condition isn't satisfied as application data
5517 * is present, hence continue is the same as break
5518 * - For 3), the loop condition is satisfied and read_record
5519 * will re-deliver the message that was held back by the client
5520 * when expecting the ServerHello.
5521 */
5522 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005523 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005524#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005526 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005527 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005528 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005529 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005532 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005534 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005535 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5540 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005543 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005544 }
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5549 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 }
5551
5552 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005553
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005554 /* We're going to return something now, cancel timer,
5555 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005557 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005558
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005560 /* If we requested renego but received AppData, resend HelloRequest.
5561 * Do it now, after setting in_offt, to avoid taking this branch
5562 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005564 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005566 {
Hanno Becker786300f2020-02-05 10:46:40 +00005567 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005568 {
Hanno Becker786300f2020-02-05 10:46:40 +00005569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5570 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005571 return( ret );
5572 }
5573 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005575#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 }
5577
5578 n = ( len < ssl->in_msglen )
5579 ? len : ssl->in_msglen;
5580
5581 memcpy( buf, ssl->in_offt, n );
5582 ssl->in_msglen -= n;
5583
5584 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005585 {
5586 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005587 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005588 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005591 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005592 /* more data available */
5593 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Paul Bakker23986e52011-04-24 08:57:21 +00005598 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599}
5600
5601/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005602 * Send application data to be encrypted by the SSL layer, taking care of max
5603 * fragment length and buffer size.
5604 *
5605 * According to RFC 5246 Section 6.2.1:
5606 *
5607 * Zero-length fragments of Application data MAY be sent as they are
5608 * potentially useful as a traffic analysis countermeasure.
5609 *
5610 * Therefore, it is possible that the input message length is 0 and the
5611 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005613static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005614 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005615{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005616 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5617 const size_t max_len = (size_t) ret;
5618
5619 if( ret < 0 )
5620 {
5621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5622 return( ret );
5623 }
5624
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005625 if( len > max_len )
5626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005628 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005631 "maximum fragment length: %d > %d",
5632 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005634 }
5635 else
5636#endif
5637 len = max_len;
5638 }
Paul Bakker887bd502011-06-08 13:10:54 +00005639
Paul Bakker5121ce52009-01-03 21:22:43 +00005640 if( ssl->out_left != 0 )
5641 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005642 /*
5643 * The user has previously tried to send the data and
5644 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5645 * written. In this case, we expect the high-level write function
5646 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 return( ret );
5652 }
5653 }
Paul Bakker887bd502011-06-08 13:10:54 +00005654 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005655 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005656 /*
5657 * The user is trying to send a message the first time, so we need to
5658 * copy the data into the internal buffers and setup the data structure
5659 * to keep track of partial writes
5660 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005661 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005663 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005664
Hanno Becker67bc7c32018-08-06 11:33:50 +01005665 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005668 return( ret );
5669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 }
5671
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005672 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005673}
5674
5675/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005676 * Write application data, doing 1/n-1 splitting if necessary.
5677 *
5678 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005679 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005680 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005683static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005684 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005685{
Janos Follath865b3eb2019-12-16 11:46:15 +00005686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005687
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005688 if( ssl->conf->cbc_record_splitting ==
5689 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005690 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5692 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5693 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005694 {
5695 return( ssl_write_real( ssl, buf, len ) );
5696 }
5697
5698 if( ssl->split_done == 0 )
5699 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005700 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005701 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005702 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005703 }
5704
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005705 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5706 return( ret );
5707 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005708
5709 return( ret + 1 );
5710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005712
5713/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005714 * Write application data (public-facing wrapper)
5715 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005716int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005717{
Janos Follath865b3eb2019-12-16 11:46:15 +00005718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005719
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005721
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005722 if( ssl == NULL || ssl->conf == NULL )
5723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5724
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005725#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005726 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5727 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005728 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005729 return( ret );
5730 }
5731#endif
5732
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005733 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005734 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005735 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005736 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005738 return( ret );
5739 }
5740 }
5741
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005742#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005743 ret = ssl_write_split( ssl, buf, len );
5744#else
5745 ret = ssl_write_real( ssl, buf, len );
5746#endif
5747
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005749
5750 return( ret );
5751}
5752
5753/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005754 * Notify the peer that the connection is being closed
5755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757{
Janos Follath865b3eb2019-12-16 11:46:15 +00005758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005759
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005760 if( ssl == NULL || ssl->conf == NULL )
5761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005764
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005765 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5771 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5772 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775 return( ret );
5776 }
5777 }
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005780
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005781 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782}
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005785{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005786 if( transform == NULL )
5787 return;
5788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005790 deflateEnd( &transform->ctx_deflate );
5791 inflateEnd( &transform->ctx_inflate );
5792#endif
5793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5795 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005796
Hanno Beckerd56ed242018-01-03 15:32:51 +00005797#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 mbedtls_md_free( &transform->md_ctx_enc );
5799 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005800#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005801
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005802 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005803}
5804
Hanno Becker0271f962018-08-16 13:23:47 +01005805#if defined(MBEDTLS_SSL_PROTO_DTLS)
5806
Hanno Becker533ab5f2020-02-05 10:49:13 +00005807void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005808{
5809 unsigned offset;
5810 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5811
5812 if( hs == NULL )
5813 return;
5814
Hanno Becker283f5ef2018-08-24 09:34:47 +01005815 ssl_free_buffered_record( ssl );
5816
Hanno Becker0271f962018-08-16 13:23:47 +01005817 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005818 ssl_buffering_free_slot( ssl, offset );
5819}
5820
5821static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5822 uint8_t slot )
5823{
5824 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5825 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005826
5827 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5828 return;
5829
Hanno Beckere605b192018-08-21 15:59:07 +01005830 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005831 {
Hanno Beckere605b192018-08-21 15:59:07 +01005832 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005833 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005834 mbedtls_free( hs_buf->data );
5835 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005836 }
5837}
5838
5839#endif /* MBEDTLS_SSL_PROTO_DTLS */
5840
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005841/*
5842 * Convert version numbers to/from wire format
5843 * and, for DTLS, to/from TLS equivalent.
5844 *
5845 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005846 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005847 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5848 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005851 unsigned char ver[2] )
5852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853#if defined(MBEDTLS_SSL_PROTO_DTLS)
5854 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005857 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5858
5859 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5860 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5861 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005862 else
5863#else
5864 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005865#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005866 {
5867 ver[0] = (unsigned char) major;
5868 ver[1] = (unsigned char) minor;
5869 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005870}
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005873 const unsigned char ver[2] )
5874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#if defined(MBEDTLS_SSL_PROTO_DTLS)
5876 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005877 {
5878 *major = 255 - ver[0] + 2;
5879 *minor = 255 - ver[1] + 1;
5880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005882 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5883 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005884 else
5885#else
5886 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005887#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005888 {
5889 *major = ver[0];
5890 *minor = ver[1];
5891 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005892}
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894#endif /* MBEDTLS_SSL_TLS_C */