blob: fdffc4defbd8001248c6f68836f7ee5ae8ad222b [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;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001577 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001578
1579 /*
1580 * The next two sizes are the minimum and maximum values of
1581 * in_msglen over all padlen values.
1582 *
1583 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001584 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001585 *
1586 * Note that max_len + maclen is never more than the buffer
1587 * length, as we previously did in_msglen -= maclen too.
1588 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001589 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001590 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1591
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001592 memset( tmp, 0, sizeof( tmp ) );
1593
1594 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02001595 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02001596#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
1597 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001598 case MBEDTLS_MD_MD5:
1599 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02001600 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02001601 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001602 extra_run =
1603 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
1604 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02001605 break;
1606#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02001607#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02001608 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02001609 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01001610 extra_run =
1611 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
1612 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02001613 break;
1614#endif
1615 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02001616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02001617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1618 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001621
Hanno Beckercab87e62019-04-29 13:52:53 +01001622 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1623 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001624 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
1625 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001626 /* Make sure we access everything even when padlen > 0. This
1627 * makes the synchronisation requirements for just-in-time
1628 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001629 ssl_read_memory( data + rec->data_len, padlen );
1630 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001631
1632 /* Call mbedtls_md_process at least once due to cache attacks
1633 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001634 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001635 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001636
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001637 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001638
1639 /* Make sure we access all the memory that could contain the MAC,
1640 * before we check it in the next code block. This makes the
1641 * synchronisation requirements for just-in-time Prime+Probe
1642 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001643 ssl_read_memory( data + min_len,
1644 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645 }
1646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1648 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001654#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001655 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
1656 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001659 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1660 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_SSL_DEBUG_ALL)
1663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001664#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001665 correct = 0;
1666 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001667 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001669
1670 /*
1671 * Finally check the correct flag
1672 */
1673 if( correct == 0 )
1674 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001675#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001676
1677 /* Make extra sure authentication was performed, exactly once */
1678 if( auth_done != 1 )
1679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Hanno Beckerccc13d02020-05-04 12:30:04 +01001684#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1685 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1686 {
1687 /* Remove inner padding and infer true content type. */
1688 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1689 &rec->type );
1690
1691 if( ret != 0 )
1692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1693 }
1694#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1695
Hanno Beckera0e20d02019-05-15 14:03:01 +01001696#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001697 if( rec->cid_len != 0 )
1698 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001699 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1700 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001701 if( ret != 0 )
1702 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1703 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001704#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
1708 return( 0 );
1709}
1710
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001711#undef MAC_NONE
1712#undef MAC_PLAINTEXT
1713#undef MAC_CIPHERTEXT
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001716/*
1717 * Compression/decompression functions
1718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001720{
Janos Follath865b3eb2019-12-16 11:46:15 +00001721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001722 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001723 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001724 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001725 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001726#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1727 size_t out_buf_len = ssl->out_buf_len;
1728#else
1729 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1730#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001733
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001734 if( len_pre == 0 )
1735 return( 0 );
1736
Paul Bakker2770fbd2012-07-03 13:30:23 +00001737 memcpy( msg_pre, ssl->out_msg, len_pre );
1738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740 ssl->out_msglen ) );
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743 ssl->out_msg, ssl->out_msglen );
1744
Paul Bakker48916f92012-09-16 19:57:18 +00001745 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1746 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1747 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001748 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751 if( ret != Z_OK )
1752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1754 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 }
1756
Darryl Greenb33cc762019-11-28 14:29:44 +00001757 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001758 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761 ssl->out_msglen ) );
1762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 ssl->out_msg, ssl->out_msglen );
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001767
1768 return( 0 );
1769}
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772{
Janos Follath865b3eb2019-12-16 11:46:15 +00001773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001774 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001775 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001777 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001778#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1779 size_t in_buf_len = ssl->in_buf_len;
1780#else
1781 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1782#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001786 if( len_pre == 0 )
1787 return( 0 );
1788
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789 memcpy( msg_pre, ssl->in_msg, len_pre );
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001792 ssl->in_msglen ) );
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 ssl->in_msg, ssl->in_msglen );
1796
Paul Bakker48916f92012-09-16 19:57:18 +00001797 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1798 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1799 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001800 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801
Paul Bakker48916f92012-09-16 19:57:18 +00001802 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001803 if( ret != Z_OK )
1804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1806 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807 }
1808
Darryl Greenb33cc762019-11-28 14:29:44 +00001809 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001810 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 ssl->in_msglen ) );
1814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816 ssl->in_msg, ssl->in_msglen );
1817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819
1820 return( 0 );
1821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823
Paul Bakker5121ce52009-01-03 21:22:43 +00001824/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001825 * Fill the input message buffer by appending data to it.
1826 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001827 *
1828 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1829 * available (from this read and/or a previous one). Otherwise, an error code
1830 * is returned (possibly EOF or WANT_READ).
1831 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001832 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1833 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1834 * since we always read a whole datagram at once.
1835 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001836 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001837 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001840{
Janos Follath865b3eb2019-12-16 11:46:15 +00001841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001842 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001843#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1844 size_t in_buf_len = ssl->in_buf_len;
1845#else
1846 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001851 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001854 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001856 }
1857
Darryl Greenb33cc762019-11-28 14:29:44 +00001858 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001862 }
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001865 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001867 uint32_t timeout;
1868
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001869 /* Just to be sure */
1870 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
1871 {
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
1873 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
1874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1875 }
1876
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001877 /*
1878 * The point is, we need to always read a full datagram at once, so we
1879 * sometimes read more then requested, and handle the additional data.
1880 * It could be the rest of the current record (while fetching the
1881 * header) and/or some other records in the same datagram.
1882 */
1883
1884 /*
1885 * Move to the next record in the already read datagram if applicable
1886 */
1887 if( ssl->next_record_offset != 0 )
1888 {
1889 if( ssl->in_left < ssl->next_record_offset )
1890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1892 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001893 }
1894
1895 ssl->in_left -= ssl->next_record_offset;
1896
1897 if( ssl->in_left != 0 )
1898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001900 ssl->next_record_offset ) );
1901 memmove( ssl->in_hdr,
1902 ssl->in_hdr + ssl->next_record_offset,
1903 ssl->in_left );
1904 }
1905
1906 ssl->next_record_offset = 0;
1907 }
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001911
1912 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001913 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001914 */
1915 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001918 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001919 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001920
1921 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001922 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001923 * are not at the beginning of a new record, the caller did something
1924 * wrong.
1925 */
1926 if( ssl->in_left != 0 )
1927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001930 }
1931
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001932 /*
1933 * Don't even try to read if time's out already.
1934 * This avoids by-passing the timer when repeatedly receiving messages
1935 * that will end up being dropped.
1936 */
Hanno Becker7876d122020-02-05 10:39:31 +00001937 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001938 {
1939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001940 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001941 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001942 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001943 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001944 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001947 timeout = ssl->handshake->retransmit_timeout;
1948 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001949 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001952
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001953 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001954 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1955 timeout );
1956 else
1957 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001960
1961 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001963 }
1964
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001965 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001968 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001971 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001972 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001975 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001976 }
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001981 return( ret );
1982 }
1983
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001984 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001985 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001987 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001989 {
Hanno Becker786300f2020-02-05 10:46:40 +00001990 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001991 {
Hanno Becker786300f2020-02-05 10:46:40 +00001992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1993 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001994 return( ret );
1995 }
1996
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001997 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001998 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002000 }
2001
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 if( ret < 0 )
2003 return( ret );
2004
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002005 ssl->in_left = ret;
2006 }
2007 else
2008#endif
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002011 ssl->in_left, nb_want ) );
2012
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002013 while( ssl->in_left < nb_want )
2014 {
2015 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002016
Hanno Becker7876d122020-02-05 10:39:31 +00002017 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002018 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2019 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002020 {
2021 if( ssl->f_recv_timeout != NULL )
2022 {
2023 ret = ssl->f_recv_timeout( ssl->p_bio,
2024 ssl->in_hdr + ssl->in_left, len,
2025 ssl->conf->read_timeout );
2026 }
2027 else
2028 {
2029 ret = ssl->f_recv( ssl->p_bio,
2030 ssl->in_hdr + ssl->in_left, len );
2031 }
2032 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002035 ssl->in_left, nb_want ) );
2036 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002037
2038 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002040
2041 if( ret < 0 )
2042 return( ret );
2043
mohammad160352aecb92018-03-28 23:41:40 -07002044 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002045 {
Darryl Green11999bb2018-03-13 15:22:58 +00002046 MBEDTLS_SSL_DEBUG_MSG( 1,
2047 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002048 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2050 }
2051
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002052 ssl->in_left += ret;
2053 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
2055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
2058 return( 0 );
2059}
2060
2061/*
2062 * Flush any data not yet written
2063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065{
Janos Follath865b3eb2019-12-16 11:46:15 +00002066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002067 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002071 if( ssl->f_send == NULL )
2072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002074 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002076 }
2077
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002078 /* Avoid incrementing counter if data is flushed */
2079 if( ssl->out_left == 0 )
2080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002082 return( 0 );
2083 }
2084
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 while( ssl->out_left > 0 )
2086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01002088 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
Hanno Becker2b1e3542018-08-06 11:19:13 +01002090 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002091 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
2095 if( ret <= 0 )
2096 return( ret );
2097
mohammad160352aecb92018-03-28 23:41:40 -07002098 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002099 {
Darryl Green11999bb2018-03-13 15:22:58 +00002100 MBEDTLS_SSL_DEBUG_MSG( 1,
2101 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002102 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2104 }
2105
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 ssl->out_left -= ret;
2107 }
2108
Hanno Becker2b1e3542018-08-06 11:19:13 +01002109#if defined(MBEDTLS_SSL_PROTO_DTLS)
2110 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002111 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002112 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002113 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002114 else
2115#endif
2116 {
2117 ssl->out_hdr = ssl->out_buf + 8;
2118 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002119 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123 return( 0 );
2124}
2125
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002126/*
2127 * Functions to handle the DTLS retransmission state machine
2128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002130/*
2131 * Append current handshake message to current outgoing flight
2132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2137 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2138 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002139
2140 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002141 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002145 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002146 }
2147
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002148 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == 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", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_free( msg );
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
2155 /* Copy current handshake message with headers */
2156 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2157 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002158 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002159 msg->next = NULL;
2160
2161 /* Append to the current flight */
2162 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002163 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002164 else
2165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002167 while( cur->next != NULL )
2168 cur = cur->next;
2169 cur->next = msg;
2170 }
2171
Hanno Becker3b235902018-08-06 09:54:53 +01002172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002173 return( 0 );
2174}
2175
2176/*
2177 * Free the current flight of handshake messages
2178 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002179void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_ssl_flight_item *cur = flight;
2182 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002183
2184 while( cur != NULL )
2185 {
2186 next = cur->next;
2187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_free( cur->p );
2189 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002190
2191 cur = next;
2192 }
2193}
2194
2195/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002196 * Swap transform_out and out_ctr with the alternative ones
2197 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002198static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002201 unsigned char tmp_out_ctr[8];
2202
2203 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002206 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207 }
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002210
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002211 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002212 tmp_transform = ssl->transform_out;
2213 ssl->transform_out = ssl->handshake->alt_transform_out;
2214 ssl->handshake->alt_transform_out = tmp_transform;
2215
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002216 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002217 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2218 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002219 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002220
2221 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002222 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2225 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002226 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002227 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2228 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2231 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002232 }
2233 }
2234#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002235
2236 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002237}
2238
2239/*
2240 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002241 */
2242int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2243{
2244 int ret = 0;
2245
2246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2247
2248 ret = mbedtls_ssl_flight_transmit( ssl );
2249
2250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2251
2252 return( ret );
2253}
2254
2255/*
2256 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002257 *
2258 * Need to remember the current message in case flush_output returns
2259 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002260 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002261 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002262int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002263{
Janos Follath865b3eb2019-12-16 11:46:15 +00002264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002268 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002270
2271 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002272 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002273 ret = ssl_swap_epochs( ssl );
2274 if( ret != 0 )
2275 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002278 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002279
2280 while( ssl->handshake->cur_msg != NULL )
2281 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002282 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002283 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002284
Hanno Beckere1dcb032018-08-17 16:47:58 +01002285 int const is_finished =
2286 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2287 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2288
Hanno Becker04da1892018-08-14 13:22:10 +01002289 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2290 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2291
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002292 /* Swap epochs before sending Finished: we can't do it after
2293 * sending ChangeCipherSpec, in case write returns WANT_READ.
2294 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002295 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002296 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002298 ret = ssl_swap_epochs( ssl );
2299 if( ret != 0 )
2300 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002301 }
2302
Hanno Becker67bc7c32018-08-06 11:33:50 +01002303 ret = ssl_get_remaining_payload_in_datagram( ssl );
2304 if( ret < 0 )
2305 return( ret );
2306 max_frag_len = (size_t) ret;
2307
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002308 /* CCS is copied as is, while HS messages may need fragmentation */
2309 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2310 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002311 if( max_frag_len == 0 )
2312 {
2313 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2314 return( ret );
2315
2316 continue;
2317 }
2318
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002319 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002320 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002321 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002322
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002323 /* Update position inside current message */
2324 ssl->handshake->cur_msg_p += cur->len;
2325 }
2326 else
2327 {
2328 const unsigned char * const p = ssl->handshake->cur_msg_p;
2329 const size_t hs_len = cur->len - 12;
2330 const size_t frag_off = p - ( cur->p + 12 );
2331 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002332 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002333
Hanno Beckere1dcb032018-08-17 16:47:58 +01002334 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002335 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002336 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002337 {
2338 ret = ssl_swap_epochs( ssl );
2339 if( ret != 0 )
2340 return( ret );
2341 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002342
Hanno Becker67bc7c32018-08-06 11:33:50 +01002343 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2344 return( ret );
2345
2346 continue;
2347 }
2348 max_hs_frag_len = max_frag_len - 12;
2349
2350 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2351 max_hs_frag_len : rem_len;
2352
2353 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002354 {
2355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002356 (unsigned) cur_hs_frag_len,
2357 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002358 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002359
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002360 /* Messages are stored with handshake headers as if not fragmented,
2361 * copy beginning of headers then fill fragmentation fields.
2362 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2363 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002364
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002365 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2366 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2367 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2368
Hanno Becker67bc7c32018-08-06 11:33:50 +01002369 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2370 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2371 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002372
2373 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2374
Hanno Becker3f7b9732018-08-28 09:53:25 +01002375 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002376 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2377 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002378 ssl->out_msgtype = cur->type;
2379
2380 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002381 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002382 }
2383
2384 /* If done with the current message move to the next one if any */
2385 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2386 {
2387 if( cur->next != NULL )
2388 {
2389 ssl->handshake->cur_msg = cur->next;
2390 ssl->handshake->cur_msg_p = cur->next->p + 12;
2391 }
2392 else
2393 {
2394 ssl->handshake->cur_msg = NULL;
2395 ssl->handshake->cur_msg_p = NULL;
2396 }
2397 }
2398
2399 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002400 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002403 return( ret );
2404 }
2405 }
2406
Hanno Becker67bc7c32018-08-06 11:33:50 +01002407 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2408 return( ret );
2409
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002410 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002413 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002416 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002417 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002418
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002420
2421 return( 0 );
2422}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002423
2424/*
2425 * To be called when the last message of an incoming flight is received.
2426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002428{
2429 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002430 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002431 ssl->handshake->flight = NULL;
2432 ssl->handshake->cur_msg = NULL;
2433
2434 /* The next incoming flight will start with this msg_seq */
2435 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2436
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002437 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002438 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002439
Hanno Becker0271f962018-08-16 13:23:47 +01002440 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002441 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002442
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002443 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002444 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2447 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002450 }
2451 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002453}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002454
2455/*
2456 * To be called when the last message of an outgoing flight is send.
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002459{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002460 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002461 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2464 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002467 }
2468 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002474 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002476
2477/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002478 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002479 *
2480 * - fill in handshake headers
2481 * - update handshake checksum
2482 * - DTLS: save message for resending
2483 * - then pass to the record layer
2484 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002485 * DTLS: except for HelloRequest, messages are only queued, and will only be
2486 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002487 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002488 * Inputs:
2489 * - ssl->out_msglen: 4 + actual handshake message len
2490 * (4 is the size of handshake headers for TLS)
2491 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2492 * - ssl->out_msg + 4: the handshake message body
2493 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002494 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002495 * - ssl->out_msglen: the length of the record contents
2496 * (including handshake headers but excluding record headers)
2497 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002498 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002499int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002500{
Janos Follath865b3eb2019-12-16 11:46:15 +00002501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002502 const size_t hs_len = ssl->out_msglen - 4;
2503 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2506
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002507 /*
2508 * Sanity checks
2509 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002510 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002511 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2512 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002513 /* In SSLv3, the client might send a NoCertificate alert. */
2514#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2515 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2516 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2517 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2518#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2519 {
2520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2522 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002525 /* Whenever we send anything different from a
2526 * HelloRequest we should be in a handshake - double check. */
2527 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2528 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002529 ssl->handshake == NULL )
2530 {
2531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002536 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002537 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002539 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002542 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002543#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002544
Hanno Beckerb50a2532018-08-06 11:52:54 +01002545 /* Double-check that we did not exceed the bounds
2546 * of the outgoing record buffer.
2547 * This should never fail as the various message
2548 * writing functions must obey the bounds of the
2549 * outgoing record buffer, but better be safe.
2550 *
2551 * Note: We deliberately do not check for the MTU or MFL here.
2552 */
2553 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2554 {
2555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
2556 "size %u, maximum %u",
2557 (unsigned) ssl->out_msglen,
2558 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2560 }
2561
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002562 /*
2563 * Fill handshake headers
2564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002567 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2568 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2569 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002571 /*
2572 * DTLS has additional fields in the Handshake layer,
2573 * between the length field and the actual payload:
2574 * uint16 message_seq;
2575 * uint24 fragment_offset;
2576 * uint24 fragment_length;
2577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002580 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002581 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002582 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002583 {
2584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2585 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002586 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10002587 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2589 }
2590
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002591 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002592 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002593
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002594 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002595 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002596 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002597 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2598 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2599 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002600 }
2601 else
2602 {
2603 ssl->out_msg[4] = 0;
2604 ssl->out_msg[5] = 0;
2605 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002606
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002607 /* Handshake hashes are computed without fragmentation,
2608 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002609 memset( ssl->out_msg + 6, 0x00, 3 );
2610 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002613
Hanno Becker0207e532018-08-28 10:28:28 +01002614 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002615 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2616 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
2618
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002619 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002622 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2623 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002624 {
2625 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002628 return( ret );
2629 }
2630 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002631 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002632#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002633 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002634 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002635 {
2636 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2637 return( ret );
2638 }
2639 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002640
2641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2642
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002643 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002644}
2645
2646/*
2647 * Record layer functions
2648 */
2649
2650/*
2651 * Write current record.
2652 *
2653 * Uses:
2654 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2655 * - ssl->out_msglen: length of the record content (excl headers)
2656 * - ssl->out_msg: record content
2657 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002658int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002659{
2660 int ret, done = 0;
2661 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002662 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002663
2664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002667 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002669 {
2670 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673 return( ret );
2674 }
2675
2676 len = ssl->out_msglen;
2677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2681 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 ret = mbedtls_ssl_hw_record_write( ssl );
2686 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2689 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002690 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002691
2692 if( ret == 0 )
2693 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002696 if( !done )
2697 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002698 unsigned i;
2699 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002700#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2701 size_t out_buf_len = ssl->out_buf_len;
2702#else
2703 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2704#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002705 /* Skip writing the record content type to after the encryption,
2706 * as it may change when using the CID extension. */
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002709 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002710
Hanno Becker19859472018-08-06 09:40:20 +01002711 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002712 ssl->out_len[0] = (unsigned char)( len >> 8 );
2713 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002714
Paul Bakker48916f92012-09-16 19:57:18 +00002715 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002716 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002717 mbedtls_record rec;
2718
2719 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002720 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002721 rec.data_len = ssl->out_msglen;
2722 rec.data_offset = ssl->out_msg - rec.buf;
2723
2724 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2725 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2726 ssl->conf->transport, rec.ver );
2727 rec.type = ssl->out_msgtype;
2728
Hanno Beckera0e20d02019-05-15 14:03:01 +01002729#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002730 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002731 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002733
Hanno Beckera18d1322018-01-03 14:27:32 +00002734 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002735 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002738 return( ret );
2739 }
2740
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002741 if( rec.data_offset != 0 )
2742 {
2743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2745 }
2746
Hanno Becker6430faf2019-05-08 11:57:13 +01002747 /* Update the record content type and CID. */
2748 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002749#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002750 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002751#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002752 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002753 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2754 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002755 }
2756
Hanno Becker5903de42019-05-03 14:46:38 +01002757 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002758
2759#if defined(MBEDTLS_SSL_PROTO_DTLS)
2760 /* In case of DTLS, double-check that we don't exceed
2761 * the remaining space in the datagram. */
2762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2763 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002764 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002765 if( ret < 0 )
2766 return( ret );
2767
2768 if( protected_record_size > (size_t) ret )
2769 {
2770 /* Should never happen */
2771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2772 }
2773 }
2774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002775
Hanno Becker6430faf2019-05-08 11:57:13 +01002776 /* Now write the potentially updated record content type. */
2777 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002780 "version = [%d:%d], msglen = %d",
2781 ssl->out_hdr[0], ssl->out_hdr[1],
2782 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002785 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002786
2787 ssl->out_left += protected_record_size;
2788 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002789 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002790
Hanno Beckerdd772292020-02-05 10:38:31 +00002791 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002792 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2793 break;
2794
2795 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002796 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002797 {
2798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2799 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2800 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
2802
Hanno Becker67bc7c32018-08-06 11:33:50 +01002803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002804 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2805 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002806 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002807 size_t remaining;
2808 ret = ssl_get_remaining_payload_in_datagram( ssl );
2809 if( ret < 0 )
2810 {
2811 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2812 ret );
2813 return( ret );
2814 }
2815
2816 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002817 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002818 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002819 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002820 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002821 else
2822 {
Hanno Becker513815a2018-08-20 11:56:09 +01002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002824 }
2825 }
2826#endif /* MBEDTLS_SSL_PROTO_DTLS */
2827
2828 if( ( flush == SSL_FORCE_FLUSH ) &&
2829 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 return( ret );
2833 }
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 return( 0 );
2838}
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002841
2842static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2843{
2844 if( ssl->in_msglen < ssl->in_hslen ||
2845 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2846 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2847 {
2848 return( 1 );
2849 }
2850 return( 0 );
2851}
Hanno Becker44650b72018-08-16 12:51:11 +01002852
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002853static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002854{
2855 return( ( ssl->in_msg[9] << 16 ) |
2856 ( ssl->in_msg[10] << 8 ) |
2857 ssl->in_msg[11] );
2858}
2859
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002860static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002861{
2862 return( ( ssl->in_msg[6] << 16 ) |
2863 ( ssl->in_msg[7] << 8 ) |
2864 ssl->in_msg[8] );
2865}
2866
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002867static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002868{
2869 uint32_t msg_len, frag_off, frag_len;
2870
2871 msg_len = ssl_get_hs_total_len( ssl );
2872 frag_off = ssl_get_hs_frag_off( ssl );
2873 frag_len = ssl_get_hs_frag_len( ssl );
2874
2875 if( frag_off > msg_len )
2876 return( -1 );
2877
2878 if( frag_len > msg_len - frag_off )
2879 return( -1 );
2880
2881 if( frag_len + 12 > ssl->in_msglen )
2882 return( -1 );
2883
2884 return( 0 );
2885}
2886
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002887/*
2888 * Mark bits in bitmask (used for DTLS HS reassembly)
2889 */
2890static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2891{
2892 unsigned int start_bits, end_bits;
2893
2894 start_bits = 8 - ( offset % 8 );
2895 if( start_bits != 8 )
2896 {
2897 size_t first_byte_idx = offset / 8;
2898
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002899 /* Special case */
2900 if( len <= start_bits )
2901 {
2902 for( ; len != 0; len-- )
2903 mask[first_byte_idx] |= 1 << ( start_bits - len );
2904
2905 /* Avoid potential issues with offset or len becoming invalid */
2906 return;
2907 }
2908
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002909 offset += start_bits; /* Now offset % 8 == 0 */
2910 len -= start_bits;
2911
2912 for( ; start_bits != 0; start_bits-- )
2913 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2914 }
2915
2916 end_bits = len % 8;
2917 if( end_bits != 0 )
2918 {
2919 size_t last_byte_idx = ( offset + len ) / 8;
2920
2921 len -= end_bits; /* Now len % 8 == 0 */
2922
2923 for( ; end_bits != 0; end_bits-- )
2924 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2925 }
2926
2927 memset( mask + offset / 8, 0xFF, len / 8 );
2928}
2929
2930/*
2931 * Check that bitmask is full
2932 */
2933static int ssl_bitmask_check( unsigned char *mask, size_t len )
2934{
2935 size_t i;
2936
2937 for( i = 0; i < len / 8; i++ )
2938 if( mask[i] != 0xFF )
2939 return( -1 );
2940
2941 for( i = 0; i < len % 8; i++ )
2942 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2943 return( -1 );
2944
2945 return( 0 );
2946}
2947
Hanno Becker56e205e2018-08-16 09:06:12 +01002948/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002949static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002950 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002951{
Hanno Becker56e205e2018-08-16 09:06:12 +01002952 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002953
Hanno Becker56e205e2018-08-16 09:06:12 +01002954 alloc_len = 12; /* Handshake header */
2955 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002956
Hanno Beckerd07df862018-08-16 09:14:58 +01002957 if( add_bitmap )
2958 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002959
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002960 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002961}
Hanno Becker56e205e2018-08-16 09:06:12 +01002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002964
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002965static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002966{
2967 return( ( ssl->in_msg[1] << 16 ) |
2968 ( ssl->in_msg[2] << 8 ) |
2969 ssl->in_msg[3] );
2970}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002971
Simon Butcher99000142016-10-13 17:21:01 +01002972int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002973{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002977 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002979 }
2980
Hanno Becker12555c62018-08-16 12:47:53 +01002981 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002984 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002985 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002989 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002991 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002992
Hanno Becker44650b72018-08-16 12:51:11 +01002993 if( ssl_check_hs_header( ssl ) != 0 )
2994 {
2995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2996 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2997 }
2998
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002999 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003000 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3001 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3002 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3003 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003004 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003005 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3006 {
3007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3008 recv_msg_seq,
3009 ssl->handshake->in_msg_seq ) );
3010 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3011 }
3012
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003013 /* Retransmit only on last message from previous flight, to avoid
3014 * too many retransmissions.
3015 * Besides, No sane server ever retransmits HelloVerifyRequest */
3016 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003020 "message_seq = %d, start_of_flight = %d",
3021 recv_msg_seq,
3022 ssl->handshake->in_flight_start_seq ) );
3023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003027 return( ret );
3028 }
3029 }
3030 else
3031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003033 "message_seq = %d, expected = %d",
3034 recv_msg_seq,
3035 ssl->handshake->in_msg_seq ) );
3036 }
3037
Hanno Becker90333da2017-10-10 11:27:13 +01003038 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003039 }
3040 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003041
Hanno Becker6d97ef52018-08-16 13:09:04 +01003042 /* Message reassembly is handled alongside buffering of future
3043 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003044 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003045 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003046 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003049 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003050 }
3051 }
3052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003054 /* With TLS we don't handle fragmentation (for now) */
3055 if( ssl->in_msglen < ssl->in_hslen )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3058 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003059 }
3060
Simon Butcher99000142016-10-13 17:21:01 +01003061 return( 0 );
3062}
3063
3064void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3065{
Hanno Becker0271f962018-08-16 13:23:47 +01003066 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003067
Hanno Becker0271f962018-08-16 13:23:47 +01003068 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003069 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003070 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003071 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003072
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003073 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003075 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003076 ssl->handshake != NULL )
3077 {
Hanno Becker0271f962018-08-16 13:23:47 +01003078 unsigned offset;
3079 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003080
Hanno Becker0271f962018-08-16 13:23:47 +01003081 /* Increment handshake sequence number */
3082 hs->in_msg_seq++;
3083
3084 /*
3085 * Clear up handshake buffering and reassembly structure.
3086 */
3087
3088 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003089 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003090
3091 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003092 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3093 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003094 offset++, hs_buf++ )
3095 {
3096 *hs_buf = *(hs_buf + 1);
3097 }
3098
3099 /* Create a fresh last entry */
3100 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003101 }
3102#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003103}
3104
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003105/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003106 * DTLS anti-replay: RFC 6347 4.1.2.6
3107 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003108 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3109 * Bit n is set iff record number in_window_top - n has been seen.
3110 *
3111 * Usually, in_window_top is the last record number seen and the lsb of
3112 * in_window is set. The only exception is the initial state (record number 0
3113 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003116void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003117{
3118 ssl->in_window_top = 0;
3119 ssl->in_window = 0;
3120}
3121
3122static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3123{
3124 return( ( (uint64_t) buf[0] << 40 ) |
3125 ( (uint64_t) buf[1] << 32 ) |
3126 ( (uint64_t) buf[2] << 24 ) |
3127 ( (uint64_t) buf[3] << 16 ) |
3128 ( (uint64_t) buf[4] << 8 ) |
3129 ( (uint64_t) buf[5] ) );
3130}
3131
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003132static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3133{
Janos Follath865b3eb2019-12-16 11:46:15 +00003134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003135 unsigned char *original_in_ctr;
3136
3137 // save original in_ctr
3138 original_in_ctr = ssl->in_ctr;
3139
3140 // use counter from record
3141 ssl->in_ctr = record_in_ctr;
3142
3143 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3144
3145 // restore the counter
3146 ssl->in_ctr = original_in_ctr;
3147
3148 return ret;
3149}
3150
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003151/*
3152 * Return 0 if sequence number is acceptable, -1 otherwise
3153 */
Hanno Becker0183d692019-07-12 08:50:37 +01003154int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003155{
3156 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3157 uint64_t bit;
3158
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003159 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003160 return( 0 );
3161
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003162 if( rec_seqnum > ssl->in_window_top )
3163 return( 0 );
3164
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003165 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003166
3167 if( bit >= 64 )
3168 return( -1 );
3169
3170 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3171 return( -1 );
3172
3173 return( 0 );
3174}
3175
3176/*
3177 * Update replay window on new validated record
3178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003180{
3181 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3182
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003183 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003184 return;
3185
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003186 if( rec_seqnum > ssl->in_window_top )
3187 {
3188 /* Update window_top and the contents of the window */
3189 uint64_t shift = rec_seqnum - ssl->in_window_top;
3190
3191 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003192 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003194 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003195 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003196 ssl->in_window |= 1;
3197 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003198
3199 ssl->in_window_top = rec_seqnum;
3200 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003201 else
3202 {
3203 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003204 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003205
3206 if( bit < 64 ) /* Always true, but be extra sure */
3207 ssl->in_window |= (uint64_t) 1 << bit;
3208 }
3209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003211
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003212#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003213/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003214 * Without any SSL context, check if a datagram looks like a ClientHello with
3215 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003216 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003217 *
3218 * - if cookie is valid, return 0
3219 * - if ClientHello looks superficially valid but cookie is not,
3220 * fill obuf and set olen, then
3221 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3222 * - otherwise return a specific error code
3223 */
3224static int ssl_check_dtls_clihlo_cookie(
3225 mbedtls_ssl_cookie_write_t *f_cookie_write,
3226 mbedtls_ssl_cookie_check_t *f_cookie_check,
3227 void *p_cookie,
3228 const unsigned char *cli_id, size_t cli_id_len,
3229 const unsigned char *in, size_t in_len,
3230 unsigned char *obuf, size_t buf_len, size_t *olen )
3231{
3232 size_t sid_len, cookie_len;
3233 unsigned char *p;
3234
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003235 /*
3236 * Structure of ClientHello with record and handshake headers,
3237 * and expected values. We don't need to check a lot, more checks will be
3238 * done when actually parsing the ClientHello - skipping those checks
3239 * avoids code duplication and does not make cookie forging any easier.
3240 *
3241 * 0-0 ContentType type; copied, must be handshake
3242 * 1-2 ProtocolVersion version; copied
3243 * 3-4 uint16 epoch; copied, must be 0
3244 * 5-10 uint48 sequence_number; copied
3245 * 11-12 uint16 length; (ignored)
3246 *
3247 * 13-13 HandshakeType msg_type; (ignored)
3248 * 14-16 uint24 length; (ignored)
3249 * 17-18 uint16 message_seq; copied
3250 * 19-21 uint24 fragment_offset; copied, must be 0
3251 * 22-24 uint24 fragment_length; (ignored)
3252 *
3253 * 25-26 ProtocolVersion client_version; (ignored)
3254 * 27-58 Random random; (ignored)
3255 * 59-xx SessionID session_id; 1 byte len + sid_len content
3256 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3257 * ...
3258 *
3259 * Minimum length is 61 bytes.
3260 */
3261 if( in_len < 61 ||
3262 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3263 in[3] != 0 || in[4] != 0 ||
3264 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3265 {
3266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3267 }
3268
3269 sid_len = in[59];
3270 if( sid_len > in_len - 61 )
3271 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3272
3273 cookie_len = in[60 + sid_len];
3274 if( cookie_len > in_len - 60 )
3275 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3276
3277 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3278 cli_id, cli_id_len ) == 0 )
3279 {
3280 /* Valid cookie */
3281 return( 0 );
3282 }
3283
3284 /*
3285 * If we get here, we've got an invalid cookie, let's prepare HVR.
3286 *
3287 * 0-0 ContentType type; copied
3288 * 1-2 ProtocolVersion version; copied
3289 * 3-4 uint16 epoch; copied
3290 * 5-10 uint48 sequence_number; copied
3291 * 11-12 uint16 length; olen - 13
3292 *
3293 * 13-13 HandshakeType msg_type; hello_verify_request
3294 * 14-16 uint24 length; olen - 25
3295 * 17-18 uint16 message_seq; copied
3296 * 19-21 uint24 fragment_offset; copied
3297 * 22-24 uint24 fragment_length; olen - 25
3298 *
3299 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3300 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3301 *
3302 * Minimum length is 28.
3303 */
3304 if( buf_len < 28 )
3305 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3306
3307 /* Copy most fields and adapt others */
3308 memcpy( obuf, in, 25 );
3309 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3310 obuf[25] = 0xfe;
3311 obuf[26] = 0xff;
3312
3313 /* Generate and write actual cookie */
3314 p = obuf + 28;
3315 if( f_cookie_write( p_cookie,
3316 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3317 {
3318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3319 }
3320
3321 *olen = p - obuf;
3322
3323 /* Go back and fill length fields */
3324 obuf[27] = (unsigned char)( *olen - 28 );
3325
3326 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3327 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3328 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3329
3330 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3331 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3332
3333 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3334}
3335
3336/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003337 * Handle possible client reconnect with the same UDP quadruplet
3338 * (RFC 6347 Section 4.2.8).
3339 *
3340 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3341 * that looks like a ClientHello.
3342 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003343 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003344 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003345 * - if the input looks like a ClientHello with a valid cookie,
3346 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003347 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003348 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003349 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003350 * This function is called (through ssl_check_client_reconnect()) when an
3351 * unexpected record is found in ssl_get_next_record(), which will discard the
3352 * record if we return 0, and bubble up the return value otherwise (this
3353 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3354 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003355 */
3356static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3357{
Janos Follath865b3eb2019-12-16 11:46:15 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003359 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003360
Hanno Becker2fddd372019-07-10 14:37:41 +01003361 if( ssl->conf->f_cookie_write == NULL ||
3362 ssl->conf->f_cookie_check == NULL )
3363 {
3364 /* If we can't use cookies to verify reachability of the peer,
3365 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3367 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003368 return( 0 );
3369 }
3370
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003371 ret = ssl_check_dtls_clihlo_cookie(
3372 ssl->conf->f_cookie_write,
3373 ssl->conf->f_cookie_check,
3374 ssl->conf->p_cookie,
3375 ssl->cli_id, ssl->cli_id_len,
3376 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003377 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003378
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003379 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3380
3381 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003382 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003383 int send_ret;
3384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3385 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3386 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003387 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003388 * If the error is permanent we'll catch it later,
3389 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003390 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3391 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3392 (void) send_ret;
3393
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003394 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003395 }
3396
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003397 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003398 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003400 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003401 {
3402 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3403 return( ret );
3404 }
3405
3406 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003407 }
3408
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003409 return( ret );
3410}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003411#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003412
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003413static int ssl_check_record_type( uint8_t record_type )
3414{
3415 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3416 record_type != MBEDTLS_SSL_MSG_ALERT &&
3417 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3418 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3419 {
3420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3421 }
3422
3423 return( 0 );
3424}
3425
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003426/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003427 * ContentType type;
3428 * ProtocolVersion version;
3429 * uint16 epoch; // DTLS only
3430 * uint48 sequence_number; // DTLS only
3431 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003432 *
3433 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003434 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003435 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3436 *
3437 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003438 * 1. proceed with the record if this function returns 0
3439 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3440 * 3. return CLIENT_RECONNECT if this function return that value
3441 * 4. drop the whole datagram if this function returns anything else.
3442 * Point 2 is needed when the peer is resending, and we have already received
3443 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003444 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003445static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003446 unsigned char *buf,
3447 size_t len,
3448 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003450 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
Hanno Beckere5e7e782019-07-11 12:29:35 +01003452 size_t const rec_hdr_type_offset = 0;
3453 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003454
Hanno Beckere5e7e782019-07-11 12:29:35 +01003455 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3456 rec_hdr_type_len;
3457 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Hanno Beckere5e7e782019-07-11 12:29:35 +01003459 size_t const rec_hdr_ctr_len = 8;
3460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003461 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003462 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3463 rec_hdr_version_len;
3464
Hanno Beckera0e20d02019-05-15 14:03:01 +01003465#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003466 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3467 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003468 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3470#endif /* MBEDTLS_SSL_PROTO_DTLS */
3471
3472 size_t rec_hdr_len_offset; /* To be determined */
3473 size_t const rec_hdr_len_len = 2;
3474
3475 /*
3476 * Check minimum lengths for record header.
3477 */
3478
3479#if defined(MBEDTLS_SSL_PROTO_DTLS)
3480 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3481 {
3482 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3483 }
3484 else
3485#endif /* MBEDTLS_SSL_PROTO_DTLS */
3486 {
3487 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3488 }
3489
3490 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3491 {
3492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3493 (unsigned) len,
3494 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3495 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3496 }
3497
3498 /*
3499 * Parse and validate record content type
3500 */
3501
3502 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003503
3504 /* Check record content type */
3505#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3506 rec->cid_len = 0;
3507
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003509 ssl->conf->cid_len != 0 &&
3510 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003511 {
3512 /* Shift pointers to account for record header including CID
3513 * struct {
3514 * ContentType special_type = tls12_cid;
3515 * ProtocolVersion version;
3516 * uint16 epoch;
3517 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003518 * opaque cid[cid_length]; // Additional field compared to
3519 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003520 * uint16 length;
3521 * opaque enc_content[DTLSCiphertext.length];
3522 * } DTLSCiphertext;
3523 */
3524
3525 /* So far, we only support static CID lengths
3526 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003527 rec_hdr_cid_len = ssl->conf->cid_len;
3528 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003529
Hanno Beckere5e7e782019-07-11 12:29:35 +01003530 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003531 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3533 (unsigned) len,
3534 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003535 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003536 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003537
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003538 /* configured CID len is guaranteed at most 255, see
3539 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3540 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003541 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003542 }
3543 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003545 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003546 if( ssl_check_record_type( rec->type ) )
3547 {
Hanno Becker54229812019-07-12 14:40:00 +01003548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3549 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3551 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003552 }
3553
Hanno Beckere5e7e782019-07-11 12:29:35 +01003554 /*
3555 * Parse and validate record version
3556 */
3557
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003558 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3559 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003560 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3561 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003562 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003563
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003564 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3567 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 }
3569
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003570 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3573 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 }
3575
Hanno Beckere5e7e782019-07-11 12:29:35 +01003576 /*
3577 * Parse/Copy record sequence number.
3578 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003579
Hanno Beckere5e7e782019-07-11 12:29:35 +01003580#if defined(MBEDTLS_SSL_PROTO_DTLS)
3581 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003582 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003583 /* Copy explicit record sequence number from input buffer. */
3584 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3585 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003586 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003587 else
3588#endif /* MBEDTLS_SSL_PROTO_DTLS */
3589 {
3590 /* Copy implicit record sequence number from SSL context structure. */
3591 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3592 }
Paul Bakker40e46942009-01-03 21:51:57 +00003593
Hanno Beckere5e7e782019-07-11 12:29:35 +01003594 /*
3595 * Parse record length.
3596 */
3597
Hanno Beckere5e7e782019-07-11 12:29:35 +01003598 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003599 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3600 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003601 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01003604 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01003605 rec->type,
3606 major_ver, minor_ver, rec->data_len ) );
3607
3608 rec->buf = buf;
3609 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003610
Hanno Beckerd417cc92019-07-26 08:20:27 +01003611 if( rec->data_len == 0 )
3612 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003614 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003615 * DTLS-related tests.
3616 * Check epoch before checking length constraint because
3617 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3618 * message gets duplicated before the corresponding Finished message,
3619 * the second ChangeCipherSpec should be discarded because it belongs
3620 * to an old epoch, but not because its length is shorter than
3621 * the minimum record length for packets using the new record transform.
3622 * Note that these two kinds of failures are handled differently,
3623 * as an unexpected record is silently skipped but an invalid
3624 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003625 */
3626#if defined(MBEDTLS_SSL_PROTO_DTLS)
3627 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3628 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003629 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003630
Hanno Becker955a5c92019-07-10 17:12:07 +01003631 /* Check that the datagram is large enough to contain a record
3632 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003633 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003634 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3636 (unsigned) len,
3637 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003638 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3639 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003640
Hanno Becker37cfe732019-07-10 17:20:01 +01003641 /* Records from other, non-matching epochs are silently discarded.
3642 * (The case of same-port Client reconnects must be considered in
3643 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003644 if( rec_epoch != ssl->in_epoch )
3645 {
3646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3647 "expected %d, received %d",
3648 ssl->in_epoch, rec_epoch ) );
3649
Hanno Becker552f7472019-07-19 10:59:12 +01003650 /* Records from the next epoch are considered for buffering
3651 * (concretely: early Finished messages). */
3652 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003653 {
Hanno Becker552f7472019-07-19 10:59:12 +01003654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3655 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003656 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003657
Hanno Becker2fddd372019-07-10 14:37:41 +01003658 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003659 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003660#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003661 /* For records from the correct epoch, check whether their
3662 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003663 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3664 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003665 {
3666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3667 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3668 }
3669#endif
3670 }
3671#endif /* MBEDTLS_SSL_PROTO_DTLS */
3672
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003673 return( 0 );
3674}
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
Paul Bakker5121ce52009-01-03 21:22:43 +00003676
Hanno Becker2fddd372019-07-10 14:37:41 +01003677#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3678static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3679{
3680 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3681
3682 /*
3683 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3684 * access the first byte of record content (handshake type), as we
3685 * have an active transform (possibly iv_len != 0), so use the
3686 * fact that the record header len is 13 instead.
3687 */
3688 if( rec_epoch == 0 &&
3689 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3690 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3691 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3692 ssl->in_left > 13 &&
3693 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3694 {
3695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3696 "from the same port" ) );
3697 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 }
3699
3700 return( 0 );
3701}
Hanno Becker2fddd372019-07-10 14:37:41 +01003702#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003704/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003705 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003706 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003707static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3708 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003709{
3710 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003713 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3716 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 ret = mbedtls_ssl_hw_record_read( ssl );
3721 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3724 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003725 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003726
3727 if( ret == 0 )
3728 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003731 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003733 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003734
Hanno Beckera18d1322018-01-03 14:27:32 +00003735 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003736 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003739
Hanno Beckera0e20d02019-05-15 14:03:01 +01003740#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003741 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3742 ssl->conf->ignore_unexpected_cid
3743 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3744 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003746 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003747 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003748#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003749
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 return( ret );
3751 }
3752
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003753 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003754 {
3755 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003756 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003757 }
3758
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003759 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003760 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003761
Hanno Beckera0e20d02019-05-15 14:03:01 +01003762#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003763 /* We have already checked the record content type
3764 * in ssl_parse_record_header(), failing or silently
3765 * dropping the record in the case of an unknown type.
3766 *
3767 * Since with the use of CIDs, the record content type
3768 * might change during decryption, re-check the record
3769 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003770 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003771 {
3772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3774 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003775#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003776
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003777 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003778 {
3779#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3780 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003781 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003782 {
3783 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3785 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3786 }
3787#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3788
3789 ssl->nb_zero++;
3790
3791 /*
3792 * Three or more empty messages may be a DoS attack
3793 * (excessive CPU consumption).
3794 */
3795 if( ssl->nb_zero > 3 )
3796 {
3797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003798 "messages, possible DoS attack" ) );
3799 /* Treat the records as if they were not properly authenticated,
3800 * thereby failing the connection if we see more than allowed
3801 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003802 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3803 }
3804 }
3805 else
3806 ssl->nb_zero = 0;
3807
3808#if defined(MBEDTLS_SSL_PROTO_DTLS)
3809 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3810 {
3811 ; /* in_ctr read from peer, not maintained internally */
3812 }
3813 else
3814#endif
3815 {
3816 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003817 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003818 if( ++ssl->in_ctr[i - 1] != 0 )
3819 break;
3820
3821 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003822 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3825 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3826 }
3827 }
3828
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 }
3830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003835 }
3836#endif
3837
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003838 /* Check actual (decrypted) record content length against
3839 * configured maximum. */
3840 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3841 {
3842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3843 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3844 }
3845
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003846 return( 0 );
3847}
3848
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003849/*
3850 * Read a record.
3851 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003852 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3853 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3854 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003855 */
Hanno Becker1097b342018-08-15 14:09:41 +01003856
3857/* Helper functions for mbedtls_ssl_read_record(). */
3858static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003859static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3860static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003861
Hanno Becker327c93b2018-08-15 13:56:18 +01003862int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003863 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003864{
Janos Follath865b3eb2019-12-16 11:46:15 +00003865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003868
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003869 if( ssl->keep_current_message == 0 )
3870 {
3871 do {
Simon Butcher99000142016-10-13 17:21:01 +01003872
Hanno Becker26994592018-08-15 14:14:59 +01003873 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003874 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003875 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003876
Hanno Beckere74d5562018-08-15 14:26:08 +01003877 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003878 {
Hanno Becker40f50842018-08-15 14:48:01 +01003879#if defined(MBEDTLS_SSL_PROTO_DTLS)
3880 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003881
Hanno Becker40f50842018-08-15 14:48:01 +01003882 /* We only check for buffered messages if the
3883 * current datagram is fully consumed. */
3884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003885 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003886 {
Hanno Becker40f50842018-08-15 14:48:01 +01003887 if( ssl_load_buffered_message( ssl ) == 0 )
3888 have_buffered = 1;
3889 }
3890
3891 if( have_buffered == 0 )
3892#endif /* MBEDTLS_SSL_PROTO_DTLS */
3893 {
3894 ret = ssl_get_next_record( ssl );
3895 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3896 continue;
3897
3898 if( ret != 0 )
3899 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003900 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003901 return( ret );
3902 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003903 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003904 }
3905
3906 ret = mbedtls_ssl_handle_message_type( ssl );
3907
Hanno Becker40f50842018-08-15 14:48:01 +01003908#if defined(MBEDTLS_SSL_PROTO_DTLS)
3909 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3910 {
3911 /* Buffer future message */
3912 ret = ssl_buffer_message( ssl );
3913 if( ret != 0 )
3914 return( ret );
3915
3916 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3917 }
3918#endif /* MBEDTLS_SSL_PROTO_DTLS */
3919
Hanno Becker90333da2017-10-10 11:27:13 +01003920 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3921 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003922
3923 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003924 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003925 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003926 return( ret );
3927 }
3928
Hanno Becker327c93b2018-08-15 13:56:18 +01003929 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003930 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003931 {
3932 mbedtls_ssl_update_handshake_status( ssl );
3933 }
Simon Butcher99000142016-10-13 17:21:01 +01003934 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003935 else
Simon Butcher99000142016-10-13 17:21:01 +01003936 {
Hanno Becker02f59072018-08-15 14:00:24 +01003937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003938 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003939 }
3940
3941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3942
3943 return( 0 );
3944}
3945
Hanno Becker40f50842018-08-15 14:48:01 +01003946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003947static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003948{
Hanno Becker40f50842018-08-15 14:48:01 +01003949 if( ssl->in_left > ssl->next_record_offset )
3950 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003951
Hanno Becker40f50842018-08-15 14:48:01 +01003952 return( 0 );
3953}
3954
3955static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3956{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003957 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003958 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003959 int ret = 0;
3960
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003961 if( hs == NULL )
3962 return( -1 );
3963
Hanno Beckere00ae372018-08-20 09:39:42 +01003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3965
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003966 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3967 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3968 {
3969 /* Check if we have seen a ChangeCipherSpec before.
3970 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003971 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3974 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003975 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003976 }
3977
Hanno Becker39b8bc92018-08-28 17:17:13 +01003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003979 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3980 ssl->in_msglen = 1;
3981 ssl->in_msg[0] = 1;
3982
3983 /* As long as they are equal, the exact value doesn't matter. */
3984 ssl->in_left = 0;
3985 ssl->next_record_offset = 0;
3986
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003987 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003988 goto exit;
3989 }
Hanno Becker37f95322018-08-16 13:55:32 +01003990
Hanno Beckerb8f50142018-08-28 10:01:34 +01003991#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003992 /* Debug only */
3993 {
3994 unsigned offset;
3995 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3996 {
3997 hs_buf = &hs->buffering.hs[offset];
3998 if( hs_buf->is_valid == 1 )
3999 {
4000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4001 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004002 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004003 }
4004 }
4005 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004006#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004007
4008 /* Check if we have buffered and/or fully reassembled the
4009 * next handshake message. */
4010 hs_buf = &hs->buffering.hs[0];
4011 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4012 {
4013 /* Synthesize a record containing the buffered HS message. */
4014 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4015 ( hs_buf->data[2] << 8 ) |
4016 hs_buf->data[3];
4017
4018 /* Double-check that we haven't accidentally buffered
4019 * a message that doesn't fit into the input buffer. */
4020 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4021 {
4022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4024 }
4025
4026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4027 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4028 hs_buf->data, msg_len + 12 );
4029
4030 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4031 ssl->in_hslen = msg_len + 12;
4032 ssl->in_msglen = msg_len + 12;
4033 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4034
4035 ret = 0;
4036 goto exit;
4037 }
4038 else
4039 {
4040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4041 hs->in_msg_seq ) );
4042 }
4043
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004044 ret = -1;
4045
4046exit:
4047
4048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4049 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004050}
4051
Hanno Beckera02b0b42018-08-21 17:20:27 +01004052static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4053 size_t desired )
4054{
4055 int offset;
4056 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4058 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004059
Hanno Becker01315ea2018-08-21 17:22:17 +01004060 /* Get rid of future records epoch first, if such exist. */
4061 ssl_free_buffered_record( ssl );
4062
4063 /* Check if we have enough space available now. */
4064 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4065 hs->buffering.total_bytes_buffered ) )
4066 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004068 return( 0 );
4069 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004070
Hanno Becker4f432ad2018-08-28 10:02:32 +01004071 /* We don't have enough space to buffer the next expected handshake
4072 * message. Remove buffers used for future messages to gain space,
4073 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004074 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4075 offset >= 0; offset-- )
4076 {
4077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4078 offset ) );
4079
Hanno Beckerb309b922018-08-23 13:18:05 +01004080 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004081
4082 /* Check if we have enough space available now. */
4083 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4084 hs->buffering.total_bytes_buffered ) )
4085 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004087 return( 0 );
4088 }
4089 }
4090
4091 return( -1 );
4092}
4093
Hanno Becker40f50842018-08-15 14:48:01 +01004094static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4095{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004096 int ret = 0;
4097 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4098
4099 if( hs == NULL )
4100 return( 0 );
4101
4102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4103
4104 switch( ssl->in_msgtype )
4105 {
4106 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004108
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004109 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004110 break;
4111
4112 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004113 {
4114 unsigned recv_msg_seq_offset;
4115 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4116 mbedtls_ssl_hs_buffer *hs_buf;
4117 size_t msg_len = ssl->in_hslen - 12;
4118
4119 /* We should never receive an old handshake
4120 * message - double-check nonetheless. */
4121 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4125 }
4126
4127 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4128 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4129 {
4130 /* Silently ignore -- message too far in the future */
4131 MBEDTLS_SSL_DEBUG_MSG( 2,
4132 ( "Ignore future HS message with sequence number %u, "
4133 "buffering window %u - %u",
4134 recv_msg_seq, ssl->handshake->in_msg_seq,
4135 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4136
4137 goto exit;
4138 }
4139
4140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4141 recv_msg_seq, recv_msg_seq_offset ) );
4142
4143 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4144
4145 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004146 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004147 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004148 size_t reassembly_buf_sz;
4149
Hanno Becker37f95322018-08-16 13:55:32 +01004150 hs_buf->is_fragmented =
4151 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4152
4153 /* We copy the message back into the input buffer
4154 * after reassembly, so check that it's not too large.
4155 * This is an implementation-specific limitation
4156 * and not one from the standard, hence it is not
4157 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004158 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004159 {
4160 /* Ignore message */
4161 goto exit;
4162 }
4163
Hanno Beckere0b150f2018-08-21 15:51:03 +01004164 /* Check if we have enough space to buffer the message. */
4165 if( hs->buffering.total_bytes_buffered >
4166 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4167 {
4168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4170 }
4171
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004172 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4173 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004174
4175 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4176 hs->buffering.total_bytes_buffered ) )
4177 {
4178 if( recv_msg_seq_offset > 0 )
4179 {
4180 /* If we can't buffer a future message because
4181 * of space limitations -- ignore. */
4182 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",
4183 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4184 (unsigned) hs->buffering.total_bytes_buffered ) );
4185 goto exit;
4186 }
Hanno Beckere1801392018-08-21 16:51:05 +01004187 else
4188 {
4189 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",
4190 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4191 (unsigned) hs->buffering.total_bytes_buffered ) );
4192 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004193
Hanno Beckera02b0b42018-08-21 17:20:27 +01004194 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004195 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004196 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",
4197 (unsigned) msg_len,
4198 (unsigned) reassembly_buf_sz,
4199 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004200 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004201 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4202 goto exit;
4203 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004204 }
4205
4206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4207 msg_len ) );
4208
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004209 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4210 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004211 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004212 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004213 goto exit;
4214 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004215 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004216
4217 /* Prepare final header: copy msg_type, length and message_seq,
4218 * then add standardised fragment_offset and fragment_length */
4219 memcpy( hs_buf->data, ssl->in_msg, 6 );
4220 memset( hs_buf->data + 6, 0, 3 );
4221 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4222
4223 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004224
4225 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004226 }
4227 else
4228 {
4229 /* Make sure msg_type and length are consistent */
4230 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4231 {
4232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4233 /* Ignore */
4234 goto exit;
4235 }
4236 }
4237
Hanno Becker4422bbb2018-08-20 09:40:19 +01004238 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004239 {
4240 size_t frag_len, frag_off;
4241 unsigned char * const msg = hs_buf->data + 12;
4242
4243 /*
4244 * Check and copy current fragment
4245 */
4246
4247 /* Validation of header fields already done in
4248 * mbedtls_ssl_prepare_handshake_record(). */
4249 frag_off = ssl_get_hs_frag_off( ssl );
4250 frag_len = ssl_get_hs_frag_len( ssl );
4251
4252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4253 frag_off, frag_len ) );
4254 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4255
4256 if( hs_buf->is_fragmented )
4257 {
4258 unsigned char * const bitmask = msg + msg_len;
4259 ssl_bitmask_set( bitmask, frag_off, frag_len );
4260 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4261 msg_len ) == 0 );
4262 }
4263 else
4264 {
4265 hs_buf->is_complete = 1;
4266 }
4267
4268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4269 hs_buf->is_complete ? "" : "not yet " ) );
4270 }
4271
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004272 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004273 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004274
4275 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004276 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004277 break;
4278 }
4279
4280exit:
4281
4282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4283 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004284}
4285#endif /* MBEDTLS_SSL_PROTO_DTLS */
4286
Hanno Becker1097b342018-08-15 14:09:41 +01004287static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004288{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004289 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004290 * Consume last content-layer message and potentially
4291 * update in_msglen which keeps track of the contents'
4292 * consumption state.
4293 *
4294 * (1) Handshake messages:
4295 * Remove last handshake message, move content
4296 * and adapt in_msglen.
4297 *
4298 * (2) Alert messages:
4299 * Consume whole record content, in_msglen = 0.
4300 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004301 * (3) Change cipher spec:
4302 * Consume whole record content, in_msglen = 0.
4303 *
4304 * (4) Application data:
4305 * Don't do anything - the record layer provides
4306 * the application data as a stream transport
4307 * and consumes through mbedtls_ssl_read only.
4308 *
4309 */
4310
4311 /* Case (1): Handshake messages */
4312 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004313 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004314 /* Hard assertion to be sure that no application data
4315 * is in flight, as corrupting ssl->in_msglen during
4316 * ssl->in_offt != NULL is fatal. */
4317 if( ssl->in_offt != NULL )
4318 {
4319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4321 }
4322
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004323 /*
4324 * Get next Handshake message in the current record
4325 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326
Hanno Becker4a810fb2017-05-24 16:27:30 +01004327 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004328 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004329 * current handshake content: If DTLS handshake
4330 * fragmentation is used, that's the fragment
4331 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004332 * size here is faulty and should be changed at
4333 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004334 * (2) While it doesn't seem to cause problems, one
4335 * has to be very careful not to assume that in_hslen
4336 * is always <= in_msglen in a sensible communication.
4337 * Again, it's wrong for DTLS handshake fragmentation.
4338 * The following check is therefore mandatory, and
4339 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004340 * Additionally, ssl->in_hslen might be arbitrarily out of
4341 * bounds after handling a DTLS message with an unexpected
4342 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004343 */
4344 if( ssl->in_hslen < ssl->in_msglen )
4345 {
4346 ssl->in_msglen -= ssl->in_hslen;
4347 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4348 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004349
Hanno Becker4a810fb2017-05-24 16:27:30 +01004350 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4351 ssl->in_msg, ssl->in_msglen );
4352 }
4353 else
4354 {
4355 ssl->in_msglen = 0;
4356 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004357
Hanno Becker4a810fb2017-05-24 16:27:30 +01004358 ssl->in_hslen = 0;
4359 }
4360 /* Case (4): Application data */
4361 else if( ssl->in_offt != NULL )
4362 {
4363 return( 0 );
4364 }
4365 /* Everything else (CCS & Alerts) */
4366 else
4367 {
4368 ssl->in_msglen = 0;
4369 }
4370
Hanno Becker1097b342018-08-15 14:09:41 +01004371 return( 0 );
4372}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004373
Hanno Beckere74d5562018-08-15 14:26:08 +01004374static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4375{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004376 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004377 return( 1 );
4378
4379 return( 0 );
4380}
4381
Hanno Becker5f066e72018-08-16 14:56:31 +01004382#if defined(MBEDTLS_SSL_PROTO_DTLS)
4383
4384static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4385{
4386 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4387 if( hs == NULL )
4388 return;
4389
Hanno Becker01315ea2018-08-21 17:22:17 +01004390 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004391 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004392 hs->buffering.total_bytes_buffered -=
4393 hs->buffering.future_record.len;
4394
4395 mbedtls_free( hs->buffering.future_record.data );
4396 hs->buffering.future_record.data = NULL;
4397 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004398}
4399
4400static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4401{
4402 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4403 unsigned char * rec;
4404 size_t rec_len;
4405 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004406#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4407 size_t in_buf_len = ssl->in_buf_len;
4408#else
4409 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4410#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004411 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4412 return( 0 );
4413
4414 if( hs == NULL )
4415 return( 0 );
4416
Hanno Becker5f066e72018-08-16 14:56:31 +01004417 rec = hs->buffering.future_record.data;
4418 rec_len = hs->buffering.future_record.len;
4419 rec_epoch = hs->buffering.future_record.epoch;
4420
4421 if( rec == NULL )
4422 return( 0 );
4423
Hanno Becker4cb782d2018-08-20 11:19:05 +01004424 /* Only consider loading future records if the
4425 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004426 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004427 return( 0 );
4428
Hanno Becker5f066e72018-08-16 14:56:31 +01004429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4430
4431 if( rec_epoch != ssl->in_epoch )
4432 {
4433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4434 goto exit;
4435 }
4436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4438
4439 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004440 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004441 {
4442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4444 }
4445
4446 memcpy( ssl->in_hdr, rec, rec_len );
4447 ssl->in_left = rec_len;
4448 ssl->next_record_offset = 0;
4449
4450 ssl_free_buffered_record( ssl );
4451
4452exit:
4453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4454 return( 0 );
4455}
4456
Hanno Becker519f15d2019-07-11 12:43:20 +01004457static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4458 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004459{
4460 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004461
4462 /* Don't buffer future records outside handshakes. */
4463 if( hs == NULL )
4464 return( 0 );
4465
4466 /* Only buffer handshake records (we are only interested
4467 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004468 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004469 return( 0 );
4470
4471 /* Don't buffer more than one future epoch record. */
4472 if( hs->buffering.future_record.data != NULL )
4473 return( 0 );
4474
Hanno Becker01315ea2018-08-21 17:22:17 +01004475 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004476 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004477 hs->buffering.total_bytes_buffered ) )
4478 {
4479 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 +01004480 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01004481 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004482 return( 0 );
4483 }
4484
Hanno Becker5f066e72018-08-16 14:56:31 +01004485 /* Buffer record */
4486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4487 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004488 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004489
4490 /* ssl_parse_record_header() only considers records
4491 * of the next epoch as candidates for buffering. */
4492 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004493 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004494
4495 hs->buffering.future_record.data =
4496 mbedtls_calloc( 1, hs->buffering.future_record.len );
4497 if( hs->buffering.future_record.data == NULL )
4498 {
4499 /* If we run out of RAM trying to buffer a
4500 * record from the next epoch, just ignore. */
4501 return( 0 );
4502 }
4503
Hanno Becker519f15d2019-07-11 12:43:20 +01004504 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004505
Hanno Becker519f15d2019-07-11 12:43:20 +01004506 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004507 return( 0 );
4508}
4509
4510#endif /* MBEDTLS_SSL_PROTO_DTLS */
4511
Hanno Beckere74d5562018-08-15 14:26:08 +01004512static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004513{
Janos Follath865b3eb2019-12-16 11:46:15 +00004514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004515 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004516
Hanno Becker5f066e72018-08-16 14:56:31 +01004517#if defined(MBEDTLS_SSL_PROTO_DTLS)
4518 /* We might have buffered a future record; if so,
4519 * and if the epoch matches now, load it.
4520 * On success, this call will set ssl->in_left to
4521 * the length of the buffered record, so that
4522 * the calls to ssl_fetch_input() below will
4523 * essentially be no-ops. */
4524 ret = ssl_load_buffered_record( ssl );
4525 if( ret != 0 )
4526 return( ret );
4527#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004528
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004529 /* Ensure that we have enough space available for the default form
4530 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4531 * with no space for CIDs counted in). */
4532 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4533 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004536 return( ret );
4537 }
4538
Hanno Beckere5e7e782019-07-11 12:29:35 +01004539 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4540 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004544 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004545 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4546 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004547 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004548 if( ret != 0 )
4549 return( ret );
4550
4551 /* Fall through to handling of unexpected records */
4552 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4553 }
4554
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004555 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4556 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004557#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004558 /* Reset in pointers to default state for TLS/DTLS records,
4559 * assuming no CID and no offset between record content and
4560 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004561 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004562
Hanno Becker7ae20e02019-07-12 08:33:49 +01004563 /* Setup internal message pointers from record structure. */
4564 ssl->in_msgtype = rec.type;
4565#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4566 ssl->in_len = ssl->in_cid + rec.cid_len;
4567#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4568 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4569 ssl->in_msglen = rec.data_len;
4570
Hanno Becker2fddd372019-07-10 14:37:41 +01004571 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004572 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004573 if( ret != 0 )
4574 return( ret );
4575#endif
4576
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004577 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004578 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004579
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4581 "(header)" ) );
4582 }
4583 else
4584 {
4585 /* Skip invalid record and the rest of the datagram */
4586 ssl->next_record_offset = 0;
4587 ssl->in_left = 0;
4588
4589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4590 "(header)" ) );
4591 }
4592
4593 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004594 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004595 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004596 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004597#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004598 {
4599 return( ret );
4600 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004601 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004605 {
Hanno Beckera8814792019-07-10 15:01:45 +01004606 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004607 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004608 if( ssl->next_record_offset < ssl->in_left )
4609 {
4610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4611 }
4612 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004613 else
4614#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004615 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004616 /*
4617 * Fetch record contents from underlying transport.
4618 */
Hanno Beckera3175662019-07-11 12:50:29 +01004619 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004620 if( ret != 0 )
4621 {
4622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4623 return( ret );
4624 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004625
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004626 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004627 }
4628
4629 /*
4630 * Decrypt record contents.
4631 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004632
Hanno Beckerfdf66042019-07-11 13:07:45 +01004633 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004637 {
4638 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004639 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004640 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004641 /* Except when waiting for Finished as a bad mac here
4642 * probably means something went wrong in the handshake
4643 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4644 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4645 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4646 {
4647#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4648 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4649 {
4650 mbedtls_ssl_send_alert_message( ssl,
4651 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4652 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4653 }
4654#endif
4655 return( ret );
4656 }
4657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004659 if( ssl->conf->badmac_limit != 0 &&
4660 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4663 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004664 }
4665#endif
4666
Hanno Becker4a810fb2017-05-24 16:27:30 +01004667 /* As above, invalid records cause
4668 * dismissal of the whole datagram. */
4669
4670 ssl->next_record_offset = 0;
4671 ssl->in_left = 0;
4672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004674 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004675 }
4676
4677 return( ret );
4678 }
4679 else
4680#endif
4681 {
4682 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4684 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 mbedtls_ssl_send_alert_message( ssl,
4687 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4688 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004689 }
4690#endif
4691 return( ret );
4692 }
4693 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004694
Hanno Becker44d89b22019-07-12 09:40:44 +01004695
4696 /* Reset in pointers to default state for TLS/DTLS records,
4697 * assuming no CID and no offset between record content and
4698 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004699 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004700#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4701 ssl->in_len = ssl->in_cid + rec.cid_len;
4702#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004703 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004704
Hanno Becker8685c822019-07-12 09:37:30 +01004705 /* The record content type may change during decryption,
4706 * so re-read it. */
4707 ssl->in_msgtype = rec.type;
4708 /* Also update the input buffer, because unfortunately
4709 * the server-side ssl_parse_client_hello() reparses the
4710 * record header when receiving a ClientHello initiating
4711 * a renegotiation. */
4712 ssl->in_hdr[0] = rec.type;
4713 ssl->in_msg = rec.buf + rec.data_offset;
4714 ssl->in_msglen = rec.data_len;
4715 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4716 ssl->in_len[1] = (unsigned char)( rec.data_len );
4717
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004718#if defined(MBEDTLS_ZLIB_SUPPORT)
4719 if( ssl->transform_in != NULL &&
4720 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4721 {
4722 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4723 {
4724 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4725 return( ret );
4726 }
4727
4728 /* Check actual (decompress) record content length against
4729 * configured maximum. */
4730 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4731 {
4732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4733 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4734 }
4735 }
4736#endif /* MBEDTLS_ZLIB_SUPPORT */
4737
Simon Butcher99000142016-10-13 17:21:01 +01004738 return( 0 );
4739}
4740
4741int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4742{
Janos Follath865b3eb2019-12-16 11:46:15 +00004743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004744
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004745 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004746 * Handle particular types of records
4747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 {
Simon Butcher99000142016-10-13 17:21:01 +01004750 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4751 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004752 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 }
4755
Hanno Beckere678eaa2018-08-21 14:57:46 +01004756 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004757 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004758 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004759 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
4761 ssl->in_msglen ) );
4762 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004763 }
4764
Hanno Beckere678eaa2018-08-21 14:57:46 +01004765 if( ssl->in_msg[0] != 1 )
4766 {
4767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4768 ssl->in_msg[0] ) );
4769 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4770 }
4771
4772#if defined(MBEDTLS_SSL_PROTO_DTLS)
4773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4774 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4775 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4776 {
4777 if( ssl->handshake == NULL )
4778 {
4779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4780 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4781 }
4782
4783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4784 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4785 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004786#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004787 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004791 if( ssl->in_msglen != 2 )
4792 {
4793 /* Note: Standard allows for more than one 2 byte alert
4794 to be packed in a single message, but Mbed TLS doesn't
4795 currently support this. */
4796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4797 ssl->in_msglen ) );
4798 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4799 }
4800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 ssl->in_msg[0], ssl->in_msg[1] ) );
4803
4804 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004805 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004810 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 }
4813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4815 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4818 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004820
4821#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4822 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4823 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4824 {
Hanno Becker90333da2017-10-10 11:27:13 +01004825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004826 /* Will be handled when trying to parse ServerHello */
4827 return( 0 );
4828 }
4829#endif
4830
4831#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4832 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4833 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4834 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4835 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4836 {
4837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4838 /* Will be handled in mbedtls_ssl_parse_certificate() */
4839 return( 0 );
4840 }
4841#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4842
4843 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004844 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
Hanno Beckerc76c6192017-06-06 10:03:17 +01004847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004848 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004849 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004850 /* Drop unexpected ApplicationData records,
4851 * except at the beginning of renegotiations */
4852 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4853 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4854#if defined(MBEDTLS_SSL_RENEGOTIATION)
4855 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4856 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004857#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004858 )
4859 {
4860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4861 return( MBEDTLS_ERR_SSL_NON_FATAL );
4862 }
4863
4864 if( ssl->handshake != NULL &&
4865 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4866 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004867 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004868 }
4869 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004870#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004871
Paul Bakker5121ce52009-01-03 21:22:43 +00004872 return( 0 );
4873}
4874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004876{
irwir6c0da642019-09-26 21:07:41 +03004877 return( mbedtls_ssl_send_alert_message( ssl,
4878 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4879 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004880}
4881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004883 unsigned char level,
4884 unsigned char message )
4885{
Janos Follath865b3eb2019-12-16 11:46:15 +00004886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004887
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004888 if( ssl == NULL || ssl->conf == NULL )
4889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004895 ssl->out_msglen = 2;
4896 ssl->out_msg[0] = level;
4897 ssl->out_msg[1] = message;
4898
Hanno Becker67bc7c32018-08-06 11:33:50 +01004899 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004902 return( ret );
4903 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004905
4906 return( 0 );
4907}
4908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004910{
Janos Follath865b3eb2019-12-16 11:46:15 +00004911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004916 ssl->out_msglen = 1;
4917 ssl->out_msg[0] = 1;
4918
Paul Bakker5121ce52009-01-03 21:22:43 +00004919 ssl->state++;
4920
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004921 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004922 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004924 return( ret );
4925 }
4926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004928
4929 return( 0 );
4930}
4931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004933{
Janos Follath865b3eb2019-12-16 11:46:15 +00004934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004937
Hanno Becker327c93b2018-08-15 13:56:18 +01004938 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 return( ret );
4942 }
4943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004947 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4948 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004950 }
4951
Hanno Beckere678eaa2018-08-21 14:57:46 +01004952 /* CCS records are only accepted if they have length 1 and content '1',
4953 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004954
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004955 /*
4956 * Switch to our negotiated transform and session parameters for inbound
4957 * data.
4958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004960 ssl->transform_in = ssl->transform_negotiate;
4961 ssl->session_in = ssl->session_negotiate;
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004967 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004968#endif
4969
4970 /* Increment epoch */
4971 if( ++ssl->in_epoch == 0 )
4972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004974 /* This is highly unlikely to happen for legitimate reasons, so
4975 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004977 }
4978 }
4979 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004981 memset( ssl->in_ctr, 0, 8 );
4982
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004983 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4986 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004991 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4992 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004994 }
4995 }
4996#endif
4997
Paul Bakker5121ce52009-01-03 21:22:43 +00004998 ssl->state++;
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001
5002 return( 0 );
5003}
5004
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005005/* Once ssl->out_hdr as the address of the beginning of the
5006 * next outgoing record is set, deduce the other pointers.
5007 *
5008 * Note: For TLS, we save the implicit record sequence number
5009 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5010 * and the caller has to make sure there's space for this.
5011 */
5012
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005013static size_t ssl_transform_get_explicit_iv_len(
5014 mbedtls_ssl_transform const *transform )
5015{
5016 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5017 return( 0 );
5018
5019 return( transform->ivlen - transform->fixed_ivlen );
5020}
5021
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005022void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5023 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005024{
5025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5027 {
5028 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005029#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005030 ssl->out_cid = ssl->out_ctr + 8;
5031 ssl->out_len = ssl->out_cid;
5032 if( transform != NULL )
5033 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005034#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005035 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005036#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005037 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005038 }
5039 else
5040#endif
5041 {
5042 ssl->out_ctr = ssl->out_hdr - 8;
5043 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005044#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005045 ssl->out_cid = ssl->out_len;
5046#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005047 ssl->out_iv = ssl->out_hdr + 5;
5048 }
5049
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005050 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005051 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005052 if( transform != NULL )
5053 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005054}
5055
5056/* Once ssl->in_hdr as the address of the beginning of the
5057 * next incoming record is set, deduce the other pointers.
5058 *
5059 * Note: For TLS, we save the implicit record sequence number
5060 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5061 * and the caller has to make sure there's space for this.
5062 */
5063
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005064void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005065{
Hanno Becker79594fd2019-05-08 09:38:41 +01005066 /* This function sets the pointers to match the case
5067 * of unprotected TLS/DTLS records, with both ssl->in_iv
5068 * and ssl->in_msg pointing to the beginning of the record
5069 * content.
5070 *
5071 * When decrypting a protected record, ssl->in_msg
5072 * will be shifted to point to the beginning of the
5073 * record plaintext.
5074 */
5075
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005076#if defined(MBEDTLS_SSL_PROTO_DTLS)
5077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5078 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005079 /* This sets the header pointers to match records
5080 * without CID. When we receive a record containing
5081 * a CID, the fields are shifted accordingly in
5082 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005083 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005084#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005085 ssl->in_cid = ssl->in_ctr + 8;
5086 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005087#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005088 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005089#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005090 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005091 }
5092 else
5093#endif
5094 {
5095 ssl->in_ctr = ssl->in_hdr - 8;
5096 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005097#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005098 ssl->in_cid = ssl->in_len;
5099#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005100 ssl->in_iv = ssl->in_hdr + 5;
5101 }
5102
Hanno Becker79594fd2019-05-08 09:38:41 +01005103 /* This will be adjusted at record decryption time. */
5104 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005105}
5106
Paul Bakker5121ce52009-01-03 21:22:43 +00005107/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005108 * Setup an SSL context
5109 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005110
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005111void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005112{
5113 /* Set the incoming and outgoing record pointers. */
5114#if defined(MBEDTLS_SSL_PROTO_DTLS)
5115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5116 {
5117 ssl->out_hdr = ssl->out_buf;
5118 ssl->in_hdr = ssl->in_buf;
5119 }
5120 else
5121#endif /* MBEDTLS_SSL_PROTO_DTLS */
5122 {
5123 ssl->out_hdr = ssl->out_buf + 8;
5124 ssl->in_hdr = ssl->in_buf + 8;
5125 }
5126
5127 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005128 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5129 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005130}
5131
Paul Bakker5121ce52009-01-03 21:22:43 +00005132/*
5133 * SSL get accessors
5134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005136{
5137 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5138}
5139
Hanno Becker8b170a02017-10-10 11:51:19 +01005140int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5141{
5142 /*
5143 * Case A: We're currently holding back
5144 * a message for further processing.
5145 */
5146
5147 if( ssl->keep_current_message == 1 )
5148 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005150 return( 1 );
5151 }
5152
5153 /*
5154 * Case B: Further records are pending in the current datagram.
5155 */
5156
5157#if defined(MBEDTLS_SSL_PROTO_DTLS)
5158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5159 ssl->in_left > ssl->next_record_offset )
5160 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005162 return( 1 );
5163 }
5164#endif /* MBEDTLS_SSL_PROTO_DTLS */
5165
5166 /*
5167 * Case C: A handshake message is being processed.
5168 */
5169
Hanno Becker8b170a02017-10-10 11:51:19 +01005170 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5171 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005173 return( 1 );
5174 }
5175
5176 /*
5177 * Case D: An application data message is being processed
5178 */
5179 if( ssl->in_offt != NULL )
5180 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005182 return( 1 );
5183 }
5184
5185 /*
5186 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005187 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005188 * we implement support for multiple alerts in single records.
5189 */
5190
5191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5192 return( 0 );
5193}
5194
Paul Bakker43ca69c2011-01-15 17:35:19 +00005195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005197{
Hanno Becker3136ede2018-08-17 15:28:19 +01005198 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005200 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005201
Hanno Becker5903de42019-05-03 14:46:38 +01005202 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5203
Hanno Becker78640902018-08-13 16:35:15 +01005204 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005205 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207#if defined(MBEDTLS_ZLIB_SUPPORT)
5208 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5209 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005210#endif
5211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 case MBEDTLS_MODE_GCM:
5215 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005216 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005218 transform_expansion = transform->minlen;
5219 break;
5220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005222
5223 block_size = mbedtls_cipher_get_block_size(
5224 &transform->cipher_ctx_enc );
5225
Hanno Becker3136ede2018-08-17 15:28:19 +01005226 /* Expansion due to the addition of the MAC. */
5227 transform_expansion += transform->maclen;
5228
5229 /* Expansion due to the addition of CBC padding;
5230 * Theoretically up to 256 bytes, but we never use
5231 * more than the block size of the underlying cipher. */
5232 transform_expansion += block_size;
5233
5234 /* For TLS 1.1 or higher, an explicit IV is added
5235 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005236#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5237 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005238 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005239#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005240
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005241 break;
5242
5243 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005246 }
5247
Hanno Beckera0e20d02019-05-15 14:03:01 +01005248#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005249 if( transform->out_cid_len != 0 )
5250 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005251#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005252
Hanno Becker5903de42019-05-03 14:46:38 +01005253 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005254}
5255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005257/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005258 * Check record counters and renegotiate if they're above the limit.
5259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005261{
Hanno Beckerdd772292020-02-05 10:38:31 +00005262 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005263 int in_ctr_cmp;
5264 int out_ctr_cmp;
5265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5267 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005268 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005269 {
5270 return( 0 );
5271 }
5272
Andres AG2196c7f2016-12-15 17:01:16 +00005273 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5274 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005275 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005276 ssl->conf->renego_period + ep_len, 8 - ep_len );
5277
5278 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005279 {
5280 return( 0 );
5281 }
5282
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005287
5288/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 * Receive application data decrypted from the SSL layer
5290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005292{
Janos Follath865b3eb2019-12-16 11:46:15 +00005293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005294 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005295
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005296 if( ssl == NULL || ssl->conf == NULL )
5297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005305 return( ret );
5306
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005307 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005309 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005310 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005311 return( ret );
5312 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005313 }
5314#endif
5315
Hanno Becker4a810fb2017-05-24 16:27:30 +01005316 /*
5317 * Check if renegotiation is necessary and/or handshake is
5318 * in process. If yes, perform/continue, and fall through
5319 * if an unexpected packet is received while the client
5320 * is waiting for the ServerHello.
5321 *
5322 * (There is no equivalent to the last condition on
5323 * the server-side as it is not treated as within
5324 * a handshake while waiting for the ClientHello
5325 * after a renegotiation request.)
5326 */
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005329 ret = ssl_check_ctr_renegotiate( ssl );
5330 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5331 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005334 return( ret );
5335 }
5336#endif
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005341 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5342 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 return( ret );
5346 }
5347 }
5348
Hanno Beckere41158b2017-10-23 13:30:32 +01005349 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005350 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005351 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005352 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005353 if( ssl->f_get_timer != NULL &&
5354 ssl->f_get_timer( ssl->p_timer ) == -1 )
5355 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005356 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005357 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005358
Hanno Becker327c93b2018-08-15 13:56:18 +01005359 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005360 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005361 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5362 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005363
Hanno Becker4a810fb2017-05-24 16:27:30 +01005364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005366 }
5367
5368 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005370 {
5371 /*
5372 * OpenSSL sends empty messages to randomize the IV
5373 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005374 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005377 return( 0 );
5378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005380 return( ret );
5381 }
5382 }
5383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005387
Hanno Becker4a810fb2017-05-24 16:27:30 +01005388 /*
5389 * - For client-side, expect SERVER_HELLO_REQUEST.
5390 * - For server-side, expect CLIENT_HELLO.
5391 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5392 */
5393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005397 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005400
5401 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005404 {
5405 continue;
5406 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005409 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005410#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005411
Hanno Becker4a810fb2017-05-24 16:27:30 +01005412#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005417
5418 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005421 {
5422 continue;
5423 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005426 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005427#endif /* MBEDTLS_SSL_SRV_C */
5428
Hanno Becker21df7f92017-10-17 11:03:26 +01005429#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005431 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5432 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5433 ssl->conf->allow_legacy_renegotiation ==
5434 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5435 {
5436 /*
5437 * Accept renegotiation request
5438 */
Paul Bakker48916f92012-09-16 19:57:18 +00005439
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005440 /* DTLS clients need to know renego is server-initiated */
5441#if defined(MBEDTLS_SSL_PROTO_DTLS)
5442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5443 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5444 {
5445 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5446 }
5447#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005448 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005449 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5450 ret != 0 )
5451 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5453 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005454 return( ret );
5455 }
5456 }
5457 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005458#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005459 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 /*
5461 * Refuse renegotiation
5462 */
5463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#if defined(MBEDTLS_SSL_PROTO_SSL3)
5467 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005468 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005469 /* SSLv3 does not have a "no_renegotiation" warning, so
5470 we send a fatal alert and abort the connection. */
5471 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5472 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5473 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005474 }
5475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5477#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5478 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5479 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5482 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5483 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005484 {
5485 return( ret );
5486 }
Paul Bakker48916f92012-09-16 19:57:18 +00005487 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005488 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5490 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005494 }
Paul Bakker48916f92012-09-16 19:57:18 +00005495 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005496
Hanno Becker90333da2017-10-10 11:27:13 +01005497 /* At this point, we don't know whether the renegotiation has been
5498 * completed or not. The cases to consider are the following:
5499 * 1) The renegotiation is complete. In this case, no new record
5500 * has been read yet.
5501 * 2) The renegotiation is incomplete because the client received
5502 * an application data record while awaiting the ServerHello.
5503 * 3) The renegotiation is incomplete because the client received
5504 * a non-handshake, non-application data message while awaiting
5505 * the ServerHello.
5506 * In each of these case, looping will be the proper action:
5507 * - For 1), the next iteration will read a new record and check
5508 * if it's application data.
5509 * - For 2), the loop condition isn't satisfied as application data
5510 * is present, hence continue is the same as break
5511 * - For 3), the loop condition is satisfied and read_record
5512 * will re-deliver the message that was held back by the client
5513 * when expecting the ServerHello.
5514 */
5515 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005516 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005517#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005519 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005520 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005521 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005522 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005525 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005527 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005528 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5533 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005536 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005537 }
5538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5542 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543 }
5544
5545 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005546
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005547 /* We're going to return something now, cancel timer,
5548 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005550 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005551
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005553 /* If we requested renego but received AppData, resend HelloRequest.
5554 * Do it now, after setting in_offt, to avoid taking this branch
5555 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005559 {
Hanno Becker786300f2020-02-05 10:46:40 +00005560 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005561 {
Hanno Becker786300f2020-02-05 10:46:40 +00005562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5563 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005564 return( ret );
5565 }
5566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005568#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005569 }
5570
5571 n = ( len < ssl->in_msglen )
5572 ? len : ssl->in_msglen;
5573
5574 memcpy( buf, ssl->in_offt, n );
5575 ssl->in_msglen -= n;
5576
5577 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005578 {
5579 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005580 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005581 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005583 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005584 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 /* more data available */
5586 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Paul Bakker23986e52011-04-24 08:57:21 +00005591 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005592}
5593
5594/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005595 * Send application data to be encrypted by the SSL layer, taking care of max
5596 * fragment length and buffer size.
5597 *
5598 * According to RFC 5246 Section 6.2.1:
5599 *
5600 * Zero-length fragments of Application data MAY be sent as they are
5601 * potentially useful as a traffic analysis countermeasure.
5602 *
5603 * Therefore, it is possible that the input message length is 0 and the
5604 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005605 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005606static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005607 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005609 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5610 const size_t max_len = (size_t) ret;
5611
5612 if( ret < 0 )
5613 {
5614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5615 return( ret );
5616 }
5617
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005618 if( len > max_len )
5619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005624 "maximum fragment length: %d > %d",
5625 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005627 }
5628 else
5629#endif
5630 len = max_len;
5631 }
Paul Bakker887bd502011-06-08 13:10:54 +00005632
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 if( ssl->out_left != 0 )
5634 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005635 /*
5636 * The user has previously tried to send the data and
5637 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5638 * written. In this case, we expect the high-level write function
5639 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 return( ret );
5645 }
5646 }
Paul Bakker887bd502011-06-08 13:10:54 +00005647 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005648 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005649 /*
5650 * The user is trying to send a message the first time, so we need to
5651 * copy the data into the internal buffers and setup the data structure
5652 * to keep track of partial writes
5653 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005654 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005656 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005657
Hanno Becker67bc7c32018-08-06 11:33:50 +01005658 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005661 return( ret );
5662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 }
5664
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005665 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666}
5667
5668/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005669 * Write application data, doing 1/n-1 splitting if necessary.
5670 *
5671 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005672 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005673 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005676static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005677 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005678{
Janos Follath865b3eb2019-12-16 11:46:15 +00005679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005680
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005681 if( ssl->conf->cbc_record_splitting ==
5682 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005683 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5685 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5686 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005687 {
5688 return( ssl_write_real( ssl, buf, len ) );
5689 }
5690
5691 if( ssl->split_done == 0 )
5692 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005693 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005694 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005695 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005696 }
5697
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005698 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5699 return( ret );
5700 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005701
5702 return( ret + 1 );
5703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005705
5706/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005707 * Write application data (public-facing wrapper)
5708 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005709int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005710{
Janos Follath865b3eb2019-12-16 11:46:15 +00005711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005712
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005714
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005715 if( ssl == NULL || ssl->conf == NULL )
5716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5717
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005718#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005719 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5720 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005721 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005722 return( ret );
5723 }
5724#endif
5725
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005726 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005727 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005728 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005729 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005731 return( ret );
5732 }
5733 }
5734
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005735#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005736 ret = ssl_write_split( ssl, buf, len );
5737#else
5738 ret = ssl_write_real( ssl, buf, len );
5739#endif
5740
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005742
5743 return( ret );
5744}
5745
5746/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005747 * Notify the peer that the connection is being closed
5748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005750{
Janos Follath865b3eb2019-12-16 11:46:15 +00005751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005752
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005753 if( ssl == NULL || ssl->conf == NULL )
5754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005757
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005758 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5764 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5765 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768 return( ret );
5769 }
5770 }
5771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005773
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005774 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775}
5776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005778{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005779 if( transform == NULL )
5780 return;
5781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005783 deflateEnd( &transform->ctx_deflate );
5784 inflateEnd( &transform->ctx_inflate );
5785#endif
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5788 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005789
Hanno Beckerd56ed242018-01-03 15:32:51 +00005790#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 mbedtls_md_free( &transform->md_ctx_enc );
5792 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005793#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005794
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005795 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005796}
5797
Hanno Becker0271f962018-08-16 13:23:47 +01005798#if defined(MBEDTLS_SSL_PROTO_DTLS)
5799
Hanno Becker533ab5f2020-02-05 10:49:13 +00005800void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005801{
5802 unsigned offset;
5803 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5804
5805 if( hs == NULL )
5806 return;
5807
Hanno Becker283f5ef2018-08-24 09:34:47 +01005808 ssl_free_buffered_record( ssl );
5809
Hanno Becker0271f962018-08-16 13:23:47 +01005810 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005811 ssl_buffering_free_slot( ssl, offset );
5812}
5813
5814static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5815 uint8_t slot )
5816{
5817 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5818 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005819
5820 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5821 return;
5822
Hanno Beckere605b192018-08-21 15:59:07 +01005823 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005824 {
Hanno Beckere605b192018-08-21 15:59:07 +01005825 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005826 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005827 mbedtls_free( hs_buf->data );
5828 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005829 }
5830}
5831
5832#endif /* MBEDTLS_SSL_PROTO_DTLS */
5833
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005834/*
5835 * Convert version numbers to/from wire format
5836 * and, for DTLS, to/from TLS equivalent.
5837 *
5838 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005839 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005840 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5841 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005844 unsigned char ver[2] )
5845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846#if defined(MBEDTLS_SSL_PROTO_DTLS)
5847 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005850 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5851
5852 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5853 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5854 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005855 else
5856#else
5857 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005858#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005859 {
5860 ver[0] = (unsigned char) major;
5861 ver[1] = (unsigned char) minor;
5862 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005863}
5864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005866 const unsigned char ver[2] )
5867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#if defined(MBEDTLS_SSL_PROTO_DTLS)
5869 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005870 {
5871 *major = 255 - ver[0] + 2;
5872 *minor = 255 - ver[1] + 1;
5873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005875 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5876 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005877 else
5878#else
5879 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005880#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005881 {
5882 *major = ver[0];
5883 *minor = ver[1];
5884 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005885}
5886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887#endif /* MBEDTLS_SSL_TLS_C */