blob: 127276486bcb1b71eaa0a83fa0277d8c724edc32 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SSL 3.0 specification was drafted by Netscape in 1996,
48 * and became an IETF standard in 1999.
49 *
50 * http://wp.netscape.com/eng/ssl3/
51 * http://www.ietf.org/rfc/rfc2246.txt
52 * http://www.ietf.org/rfc/rfc4346.txt
53 */
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000062
SimonBd5800b72016-04-26 07:43:27 +010063#if defined(MBEDTLS_PLATFORM_C)
64#include "mbedtls/platform.h"
65#else
66#include <stdlib.h>
67#define mbedtls_calloc calloc
68#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010069#endif
70
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/debug.h"
72#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020073#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050074#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020075
Rich Evans00ab4702015-02-06 13:43:58 +000076#include <string.h>
77
Janos Follath23bdca02016-10-07 14:47:14 +010078#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020080#endif
81
Hanno Becker2a43f6f2018-08-10 11:12:52 +010082static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010083static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010084
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010085/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010090 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010091#else
92 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010093#endif
94 return( 0 );
95}
96
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097/*
98 * Start a timer.
99 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200103 if( ssl->f_set_timer == NULL )
104 return;
105
106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
107 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200108}
109
110/*
111 * Return -1 is timer is expired, 0 if it isn't.
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200114{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200115 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200116 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200117
118 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119 {
120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200121 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200122 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200123
124 return( 0 );
125}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200126
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100127static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
128 mbedtls_ssl_transform *transform );
129static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
130 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100131
132#define SSL_DONT_FORCE_FLUSH 0
133#define SSL_FORCE_FLUSH 1
134
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
Hanno Beckerd5847772018-08-28 10:09:23 +0100137/* Forward declarations for functions related to message buffering. */
138static void ssl_buffering_free( mbedtls_ssl_context *ssl );
139static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
140 uint8_t slot );
141static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
142static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
143static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
144static int ssl_buffer_message( mbedtls_ssl_context *ssl );
145static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100146static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100147
Hanno Beckera67dee22018-08-22 10:05:20 +0100148static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100149static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100150{
Hanno Becker11682cc2018-08-22 14:41:02 +0100151 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100152
153 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100154 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100155
156 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
157}
158
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
160{
Hanno Becker11682cc2018-08-22 14:41:02 +0100161 size_t const bytes_written = ssl->out_left;
162 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100163
164 /* Double-check that the write-index hasn't gone
165 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100166 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100167 {
168 /* Should never happen... */
169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
170 }
171
172 return( (int) ( mtu - bytes_written ) );
173}
174
175static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
176{
177 int ret;
178 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400179 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100180
181#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
182 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
183
184 if( max_len > mfl )
185 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100186
187 /* By the standard (RFC 6066 Sect. 4), the MFL extension
188 * only limits the maximum record payload size, so in theory
189 * we would be allowed to pack multiple records of payload size
190 * MFL into a single datagram. However, this would mean that there's
191 * no way to explicitly communicate MTU restrictions to the peer.
192 *
193 * The following reduction of max_len makes sure that we never
194 * write datagrams larger than MFL + Record Expansion Overhead.
195 */
196 if( max_len <= ssl->out_left )
197 return( 0 );
198
199 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100200#endif
201
202 ret = ssl_get_remaining_space_in_datagram( ssl );
203 if( ret < 0 )
204 return( ret );
205 remaining = (size_t) ret;
206
207 ret = mbedtls_ssl_get_record_expansion( ssl );
208 if( ret < 0 )
209 return( ret );
210 expansion = (size_t) ret;
211
212 if( remaining <= expansion )
213 return( 0 );
214
215 remaining -= expansion;
216 if( remaining >= max_len )
217 remaining = max_len;
218
219 return( (int) remaining );
220}
221
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200222/*
223 * Double the retransmit timeout value, within the allowed range,
224 * returning -1 if the maximum value has already been reached.
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200227{
228 uint32_t new_timeout;
229
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200230 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200231 return( -1 );
232
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200233 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
234 * in the following way: after the initial transmission and a first
235 * retransmission, back off to a temporary estimated MTU of 508 bytes.
236 * This value is guaranteed to be deliverable (if not guaranteed to be
237 * delivered) of any compliant IPv4 (and IPv6) network, and should work
238 * on most non-IP stacks too. */
239 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400240 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200241 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
243 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200244
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200245 new_timeout = 2 * ssl->handshake->retransmit_timeout;
246
247 /* Avoid arithmetic overflow and range overflow */
248 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200249 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200250 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200251 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252 }
253
254 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256 ssl->handshake->retransmit_timeout ) );
257
258 return( 0 );
259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200263 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200265 ssl->handshake->retransmit_timeout ) );
266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200270/*
271 * Convert max_fragment_length codes to length.
272 * RFC 6066 says:
273 * enum{
274 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
275 * } MaxFragmentLength;
276 * and we add 0 -> extension unused
277 */
Angus Grattond8213d02016-05-25 20:56:48 +1000278static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200279{
Angus Grattond8213d02016-05-25 20:56:48 +1000280 switch( mfl )
281 {
282 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
283 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
284 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
285 return 512;
286 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
287 return 1024;
288 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
289 return 2048;
290 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
291 return 4096;
292 default:
293 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
294 }
295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200297
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200298#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_ssl_session_free( dst );
302 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
吴敬辉eb490aa2021-11-29 10:46:35 +0800304#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
305 dst->ticket = NULL;
306#endif
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200309 if( src->peer_cert != NULL )
310 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200311 int ret;
312
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200313 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200314 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200320 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200323 dst->peer_cert = NULL;
324 return( ret );
325 }
326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200328
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200329#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200330 if( src->ticket != NULL )
331 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200332 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200333 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200334 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 memcpy( dst->ticket, src->ticket, src->ticket_len );
337 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200338#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200339
340 return( 0 );
341}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200342#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
345int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200346 const unsigned char *key_enc, const unsigned char *key_dec,
347 size_t keylen,
348 const unsigned char *iv_enc, const unsigned char *iv_dec,
349 size_t ivlen,
350 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200351 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
353int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
354int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
355int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
356int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
357#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359/*
360 * Key material generation
361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200363static int ssl3_prf( const unsigned char *secret, size_t slen,
364 const char *label,
365 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000366 unsigned char *dstbuf, size_t dlen )
367{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100368 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000369 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200370 mbedtls_md5_context md5;
371 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000372 unsigned char padding[16];
373 unsigned char sha1sum[20];
374 ((void)label);
375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200376 mbedtls_md5_init( &md5 );
377 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200378
Paul Bakker5f70b252012-09-13 14:23:06 +0000379 /*
380 * SSLv3:
381 * block =
382 * MD5( secret + SHA1( 'A' + secret + random ) ) +
383 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
384 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
385 * ...
386 */
387 for( i = 0; i < dlen / 16; i++ )
388 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200389 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000390
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100400 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000401
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100406 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100408 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100409 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000410 }
411
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100412exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200413 mbedtls_md5_free( &md5 );
414 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000415
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500416 mbedtls_platform_zeroize( padding, sizeof( padding ) );
417 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100419 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200424static int tls1_prf( const unsigned char *secret, size_t slen,
425 const char *label,
426 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000427 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000428{
Paul Bakker23986e52011-04-24 08:57:21 +0000429 size_t nb, hs;
430 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200431 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 unsigned char tmp[128];
433 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 const mbedtls_md_info_t *md_info;
435 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100436 int ret;
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443 hs = ( slen + 1 ) / 2;
444 S1 = secret;
445 S2 = secret + slen - hs;
446
447 nb = strlen( label );
448 memcpy( tmp + 20, label, nb );
449 memcpy( tmp + 20 + nb, random, rlen );
450 nb += rlen;
451
452 /*
453 * First compute P_md5(secret,label+random)[0..dlen]
454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100456 {
457 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
458 goto exit;
459 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100462 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100463
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100464 ret = mbedtls_md_hmac_starts( &md_ctx, S1, hs );
465 if( ret != 0 )
466 goto exit;
467 ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
468 if( ret != 0 )
469 goto exit;
470 ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
471 if( ret != 0 )
472 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 for( i = 0; i < dlen; i += 16 )
475 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100476 ret = mbedtls_md_hmac_reset ( &md_ctx );
477 if( ret != 0 )
478 goto exit;
479 ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
480 if( ret != 0 )
481 goto exit;
482 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
483 if( ret != 0 )
484 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100486 ret = mbedtls_md_hmac_reset ( &md_ctx );
487 if( ret != 0 )
488 goto exit;
489 ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
490 if( ret != 0 )
491 goto exit;
492 ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
493 if( ret != 0 )
494 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
497
498 for( j = 0; j < k; j++ )
499 dstbuf[i + j] = h_i[j];
500 }
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 /*
505 * XOR out with P_sha1(secret,label+random)[0..dlen]
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100508 {
509 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
510 goto exit;
511 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100514 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100515
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100516 ret = mbedtls_md_hmac_starts( &md_ctx, S2, hs );
517 if( ret != 0 )
518 goto exit;
519 ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
520 if( ret != 0 )
521 goto exit;
522 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
523 if( ret != 0 )
524 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 for( i = 0; i < dlen; i += 20 )
527 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100528 ret = mbedtls_md_hmac_reset ( &md_ctx );
529 if( ret != 0 )
530 goto exit;
531 ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
532 if( ret != 0 )
533 goto exit;
534 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
535 if( ret != 0 )
536 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100538 ret = mbedtls_md_hmac_reset ( &md_ctx );
539 if( ret != 0 )
540 goto exit;
541 ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
542 if( ret != 0 )
543 goto exit;
544 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
545 if( ret != 0 )
546 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
549
550 for( j = 0; j < k; j++ )
551 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
552 }
553
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100554exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500557 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
558 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 return( 0 );
561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
565static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100566 const unsigned char *secret, size_t slen,
567 const char *label,
568 const unsigned char *random, size_t rlen,
569 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000570{
571 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100572 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000573 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
575 const mbedtls_md_info_t *md_info;
576 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577 int ret;
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100585
586 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000588
589 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100590 memcpy( tmp + md_len, label, nb );
591 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592 nb += rlen;
593
594 /*
595 * Compute P_<hash>(secret, label + random)[0..dlen]
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100598 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100599
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100600 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
601 if( ret != 0 )
602 goto exit;
603 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
604 if( ret != 0 )
605 goto exit;
606 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
607 if( ret != 0 )
608 goto exit;
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100609
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100610 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100612 ret = mbedtls_md_hmac_reset ( &md_ctx );
613 if( ret != 0 )
614 goto exit;
615 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
616 if( ret != 0 )
617 goto exit;
618 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
619 if( ret != 0 )
620 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100622 ret = mbedtls_md_hmac_reset ( &md_ctx );
623 if( ret != 0 )
624 goto exit;
625 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
626 if( ret != 0 )
627 goto exit;
628 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
629 if( ret != 0 )
630 goto exit;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000631
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633
634 for( j = 0; j < k; j++ )
635 dstbuf[i + j] = h_i[j];
636 }
637
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100638exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500641 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
642 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000643
644 return( 0 );
645}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100648static int tls_prf_sha256( const unsigned char *secret, size_t slen,
649 const char *label,
650 const unsigned char *random, size_t rlen,
651 unsigned char *dstbuf, size_t dlen )
652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100654 label, random, rlen, dstbuf, dlen ) );
655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200659static int tls_prf_sha384( const unsigned char *secret, size_t slen,
660 const char *label,
661 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 unsigned char *dstbuf, size_t dlen )
663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100665 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#endif /* MBEDTLS_SHA512_C */
668#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
673 defined(MBEDTLS_SSL_PROTO_TLS1_1)
674static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675#endif
Paul Bakker380da532012-04-18 16:10:25 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_PROTO_SSL3)
678static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
679static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200680#endif
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300683static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#endif
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
688#if defined(MBEDTLS_SHA256_C)
689static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300690static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SHA512_C)
695static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300696static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000702{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200703 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 unsigned char keyblk[256];
706 unsigned char *key1;
707 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100708 unsigned char *mac_enc;
709 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000710 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200711 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 const mbedtls_cipher_info_t *cipher_info;
713 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_ssl_session *session = ssl->session_negotiate;
716 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
717 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100722 if( cipher_info == NULL )
723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100725 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100727 }
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100730 if( md_info == NULL )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100733 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100735 }
736
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000738 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_SSL_PROTO_SSL3)
741 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000742 {
Paul Bakker48916f92012-09-16 19:57:18 +0000743 handshake->tls_prf = ssl3_prf;
744 handshake->calc_verify = ssl_calc_verify_ssl;
745 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000746 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200747 else
748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
750 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000751 {
Paul Bakker48916f92012-09-16 19:57:18 +0000752 handshake->tls_prf = tls1_prf;
753 handshake->calc_verify = ssl_calc_verify_tls;
754 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000755 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200756 else
757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
759#if defined(MBEDTLS_SHA512_C)
760 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
761 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000762 {
Paul Bakker48916f92012-09-16 19:57:18 +0000763 handshake->tls_prf = tls_prf_sha384;
764 handshake->calc_verify = ssl_calc_verify_tls_sha384;
765 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000766 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_SHA256_C)
770 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000771 {
Paul Bakker48916f92012-09-16 19:57:18 +0000772 handshake->tls_prf = tls_prf_sha256;
773 handshake->calc_verify = ssl_calc_verify_tls_sha256;
774 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200776 else
777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200782 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783
784 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 * SSLv3:
786 * master =
787 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
788 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
789 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200790 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000792 * master = PRF( premaster, "master secret", randbytes )[0..47]
793 */
Paul Bakker0a597072012-09-25 21:55:46 +0000794 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000797 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
800 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200801 {
802 unsigned char session_hash[48];
803 size_t hash_len;
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200806
807 ssl->handshake->calc_verify( ssl, session_hash );
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
810 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200813 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200815 {
816 hash_len = 48;
817 }
818 else
819#endif
820 hash_len = 32;
821 }
822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200824 hash_len = 36;
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200827
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100828 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
829 "extended master secret",
830 session_hash, hash_len,
831 session->master, 48 );
832 if( ret != 0 )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100835 return( ret );
836 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200837
838 }
839 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200840#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100841 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
842 "master secret",
843 handshake->randbytes, 64,
844 session->master, 48 );
845 if( ret != 0 )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100848 return( ret );
849 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200850
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500851 mbedtls_platform_zeroize( handshake->premaster,
852 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 }
854 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
857 /*
858 * Swap the client and server random values.
859 */
Paul Bakker48916f92012-09-16 19:57:18 +0000860 memcpy( tmp, handshake->randbytes, 64 );
861 memcpy( handshake->randbytes, tmp + 32, 32 );
862 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500863 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
865 /*
866 * SSLv3:
867 * key block =
868 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
869 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
870 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
871 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
872 * ...
873 *
874 * TLSv1:
875 * key block = PRF( master, "key expansion", randbytes )
876 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100877 ret = handshake->tls_prf( session->master, 48, "key expansion",
878 handshake->randbytes, 64, keyblk, 256 );
879 if( ret != 0 )
880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100882 return( ret );
883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
886 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
887 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
888 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
889 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500891 mbedtls_platform_zeroize( handshake->randbytes,
892 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
894 /*
895 * Determine the appropriate key, IV and MAC length.
896 */
Paul Bakker68884e32013-01-07 18:20:04 +0100897
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200898 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200901 cipher_info->mode == MBEDTLS_MODE_CCM ||
902 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200904 size_t taglen, explicit_ivlen;
905
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200909 /* All modes haves 96-bit IVs;
910 * GCM and CCM has 4 implicit and 8 explicit bytes
911 * ChachaPoly has all 12 bytes implicit
912 */
Paul Bakker68884e32013-01-07 18:20:04 +0100913 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200914 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
915 transform->fixed_ivlen = 12;
916 else
917 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200918
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200919 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
920 taglen = transform->ciphersuite_info->flags &
921 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
922
923
924 /* Minimum length of encrypted record */
925 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
926 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100927 }
928 else
929 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200930 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
932 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200935 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100936 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200938 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000939 mac_key_len = mbedtls_md_get_size( md_info );
940 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200943 /*
944 * If HMAC is to be truncated, we shall keep the leftmost bytes,
945 * (rfc 6066 page 13 or rfc 2104 section 4),
946 * so we only need to adjust the length here.
947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000951
952#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
953 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000954 * HMAC implementation which also truncates the key
955 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000956 mac_key_len = transform->maclen;
957#endif
958 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200960
961 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100962 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200964 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200966 transform->minlen = transform->maclen;
967 else
Paul Bakker68884e32013-01-07 18:20:04 +0100968 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 /*
970 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100971 * 1. if EtM is in use: one block plus MAC
972 * otherwise: * first multiple of blocklen greater than maclen
973 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
976 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100977 {
978 transform->minlen = transform->maclen
979 + cipher_info->block_size;
980 }
981 else
982#endif
983 {
984 transform->minlen = transform->maclen
985 + cipher_info->block_size
986 - transform->maclen % cipher_info->block_size;
987 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
990 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
991 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200992 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100993 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
996 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
997 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200998 {
999 transform->minlen += transform->ivlen;
1000 }
1001 else
1002#endif
1003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001006 }
Paul Bakker68884e32013-01-07 18:20:04 +01001007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 }
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001011 transform->keylen, transform->minlen, transform->ivlen,
1012 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 /*
1015 * Finally setup the cipher contexts, IVs and MAC secrets.
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001018 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key1 = keyblk + mac_key_len * 2;
1021 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001024 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001026 /*
1027 * This is not used in TLS v1.1.
1028 */
Paul Bakker48916f92012-09-16 19:57:18 +00001029 iv_copy_len = ( transform->fixed_ivlen ) ?
1030 transform->fixed_ivlen : transform->ivlen;
1031 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1032 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001033 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 }
1035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_CLI_C */
1037#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001038 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001040 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1041 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Hanno Becker81c7b182017-11-09 18:39:33 +00001043 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001044 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001046 /*
1047 * This is not used in TLS v1.1.
1048 */
Paul Bakker48916f92012-09-16 19:57:18 +00001049 iv_copy_len = ( transform->fixed_ivlen ) ?
1050 transform->fixed_ivlen : transform->ivlen;
1051 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1052 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001053 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_SSL3)
1063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001064 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001065 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001069 }
1070
Hanno Becker81c7b182017-11-09 18:39:33 +00001071 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1072 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001073 }
1074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1076#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1077 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1078 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001079 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001080 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1081 For AEAD-based ciphersuites, there is nothing to do here. */
1082 if( mac_key_len != 0 )
1083 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001084 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc,
1085 mac_enc, mac_key_len );
1086 if( ret != 0 )
1087 return( ret );
1088 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec,
1089 mac_dec, mac_key_len );
1090 if( ret != 0 )
1091 return( ret );
Gilles Peskine039fd122018-03-19 19:06:08 +01001092 }
Paul Bakker68884e32013-01-07 18:20:04 +01001093 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001094 else
1095#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001099 }
Paul Bakker68884e32013-01-07 18:20:04 +01001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1102 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001107 transform->iv_enc, transform->iv_dec,
1108 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001109 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001110 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1113 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001114 }
1115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001117
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001118#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1119 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001120 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001121 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1122 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001123 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001124 iv_copy_len );
1125 }
1126#endif
1127
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001128 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 return( ret );
1133 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001134
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001135 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001136 cipher_info ) ) != 0 )
1137 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001139 return( ret );
1140 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001143 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147 return( ret );
1148 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001151 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001155 return( ret );
1156 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_CIPHER_MODE_CBC)
1159 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1162 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001165 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001166 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1169 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001172 return( ret );
1173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001177 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001180 // Initialize compression
1181 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001183 {
Paul Bakker16770332013-10-11 09:59:44 +02001184 if( ssl->compress_buf == NULL )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001187 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001188 if( ssl->compress_buf == NULL )
1189 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001191 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001192 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001193 }
1194 }
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001197
Paul Bakker48916f92012-09-16 19:57:18 +00001198 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1199 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001200
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001201 if( deflateInit( &transform->ctx_deflate,
1202 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001203 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1206 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001207 }
1208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
1213 return( 0 );
1214}
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_SSL_PROTO_SSL3)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001217void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001219 mbedtls_md5_context md5;
1220 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 unsigned char pad_1[48];
1222 unsigned char pad_2[48];
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001226 mbedtls_md5_init( &md5 );
1227 mbedtls_sha1_init( &sha1 );
1228
1229 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1230 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Paul Bakker380da532012-04-18 16:10:25 +00001232 memset( pad_1, 0x36, 48 );
1233 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001235 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1236 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1237 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001239 mbedtls_md5_starts_ret( &md5 );
1240 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1241 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1242 mbedtls_md5_update_ret( &md5, hash, 16 );
1243 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001245 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1246 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1247 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001248
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001249 mbedtls_sha1_starts_ret( &sha1 );
1250 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1251 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1252 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1253 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001257
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001258 mbedtls_md5_free( &md5 );
1259 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001260
Paul Bakker380da532012-04-18 16:10:25 +00001261 return;
1262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001266void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001267{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001268 mbedtls_md5_context md5;
1269 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001272
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001273 mbedtls_md5_init( &md5 );
1274 mbedtls_sha1_init( &sha1 );
1275
1276 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1277 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001278
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001279 mbedtls_md5_finish_ret( &md5, hash );
1280 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001284
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001285 mbedtls_md5_free( &md5 );
1286 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001287
Paul Bakker380da532012-04-18 16:10:25 +00001288 return;
1289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1293#if defined(MBEDTLS_SHA256_C)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001294void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001295{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001296 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001297
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001298 mbedtls_sha256_init( &sha256 );
1299
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001301
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001302 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001303 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001307
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001308 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001309
Paul Bakker380da532012-04-18 16:10:25 +00001310 return;
1311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_SHA512_C)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001315void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001316{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001317 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001318
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001319 mbedtls_sha512_init( &sha512 );
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001322
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001323 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001324 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001329 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001330
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return;
1332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_SHA512_C */
1334#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1337int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001338{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001339 unsigned char *p = ssl->handshake->premaster;
1340 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001341 const unsigned char *psk = ssl->conf->psk;
1342 size_t psk_len = ssl->conf->psk_len;
1343
1344 /* If the psk callback was called, use its result */
1345 if( ssl->handshake->psk != NULL )
1346 {
1347 psk = ssl->handshake->psk;
1348 psk_len = ssl->handshake->psk_len;
1349 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001350
1351 /*
1352 * PMS = struct {
1353 * opaque other_secret<0..2^16-1>;
1354 * opaque psk<0..2^16-1>;
1355 * };
1356 * with "other_secret" depending on the particular key exchange
1357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1359 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001360 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001361 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001363
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001364 *(p++) = (unsigned char)( psk_len >> 8 );
1365 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001366
1367 if( end < p || (size_t)( end - p ) < psk_len )
1368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1369
1370 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001371 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001372 }
1373 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1375#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1376 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001377 {
1378 /*
1379 * other_secret already set by the ClientKeyExchange message,
1380 * and is 48 bytes long
1381 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001382 if( end - p < 2 )
1383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1384
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001385 *p++ = 0;
1386 *p++ = 48;
1387 p += 48;
1388 }
1389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1391#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1392 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001393 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001394 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001395 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001396
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001397 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001399 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001400 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001403 return( ret );
1404 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001405 *(p++) = (unsigned char)( len >> 8 );
1406 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001407 p += len;
1408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001410 }
1411 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1413#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1414 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001415 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001416 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001417 size_t zlen;
1418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001420 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001421 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001424 return( ret );
1425 }
1426
1427 *(p++) = (unsigned char)( zlen >> 8 );
1428 *(p++) = (unsigned char)( zlen );
1429 p += zlen;
1430
Janos Follath3fbdada2018-08-15 10:26:53 +01001431 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1432 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001433 }
1434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001439 }
1440
1441 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001442 if( end - p < 2 )
1443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001444
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001445 *(p++) = (unsigned char)( psk_len >> 8 );
1446 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001447
1448 if( end < p || (size_t)( end - p ) < psk_len )
1449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1450
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001451 memcpy( p, psk, psk_len );
1452 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001453
1454 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1455
1456 return( 0 );
1457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001461/*
1462 * SSLv3.0 MAC functions
1463 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001464#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001465static int ssl_mac( mbedtls_md_context_t *md_ctx,
1466 const unsigned char *secret,
1467 const unsigned char *buf, size_t len,
1468 const unsigned char *ctr, int type,
1469 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001470{
1471 unsigned char header[11];
1472 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001473 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1475 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001476 int ret;
Paul Bakker68884e32013-01-07 18:20:04 +01001477
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001478 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001480 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001481 else
Paul Bakker68884e32013-01-07 18:20:04 +01001482 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001483
1484 memcpy( header, ctr, 8 );
1485 header[ 8] = (unsigned char) type;
1486 header[ 9] = (unsigned char)( len >> 8 );
1487 header[10] = (unsigned char)( len );
1488
Paul Bakker68884e32013-01-07 18:20:04 +01001489 memset( padding, 0x36, padlen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001490 ret = mbedtls_md_starts( md_ctx );
1491 if( ret != 0 )
1492 return( ret );
1493 ret = mbedtls_md_update( md_ctx, secret, md_size );
1494 if( ret != 0 )
1495 return( ret );
1496 ret = mbedtls_md_update( md_ctx, padding, padlen );
1497 if( ret != 0 )
1498 return( ret );
1499 ret = mbedtls_md_update( md_ctx, header, 11 );
1500 if( ret != 0 )
1501 return( ret );
1502 ret = mbedtls_md_update( md_ctx, buf, len );
1503 if( ret != 0 )
1504 return( ret );
1505 ret = mbedtls_md_finish( md_ctx, out );
1506 if( ret != 0 )
1507 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Paul Bakker68884e32013-01-07 18:20:04 +01001509 memset( padding, 0x5C, padlen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001510 ret = mbedtls_md_starts( md_ctx );
1511 if( ret != 0 )
1512 return( ret );
1513 ret = mbedtls_md_update( md_ctx, secret, md_size );
1514 if( ret != 0 )
1515 return( ret );
1516 ret = mbedtls_md_update( md_ctx, padding, padlen );
1517 if( ret != 0 )
1518 return( ret );
1519 ret = mbedtls_md_update( md_ctx, out, md_size );
1520 if( ret != 0 )
1521 return( ret );
1522 ret = mbedtls_md_finish( md_ctx, out );
1523 if( ret != 0 )
1524 return( ret );
1525
1526 return( 0 );
Paul Bakker5f70b252012-09-13 14:23:06 +00001527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001531 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001532#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001533#endif
1534
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001535/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001541 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001545 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001549 }
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001554 ssl->out_msg, ssl->out_msglen );
1555
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001557 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001559#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( mode == MBEDTLS_MODE_STREAM ||
1561 ( mode == MBEDTLS_MODE_CBC
1562#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1563 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001564#endif
1565 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_PROTO_SSL3)
1568 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001569 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001570 unsigned char mac[SSL_MAC_MAX_BYTES];
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001571 int ret;
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001572
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001573 ret = ssl_mac( &ssl->transform_out->md_ctx_enc,
1574 ssl->transform_out->mac_enc,
1575 ssl->out_msg, ssl->out_msglen,
1576 ssl->out_ctr, ssl->out_msgtype,
1577 mac );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001578
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001579 if( ret == 0 )
1580 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Gilles Peskine957e3832021-12-10 21:33:21 +01001581 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001582 if( ret != 0 )
1583 {
1584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_mac", ret );
1585 return( ret );
1586 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001587 }
1588 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1591 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1592 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001593 {
Hanno Becker992b6872017-11-09 18:57:39 +00001594 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001595 int ret;
Hanno Becker992b6872017-11-09 18:57:39 +00001596
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001597 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1598 if( ret != 0 )
1599 goto hmac_failed_etm_disabled;
1600 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1601 if( ret != 0 )
1602 goto hmac_failed_etm_disabled;
1603 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1604 if( ret != 0 )
1605 goto hmac_failed_etm_disabled;
1606 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
1607 ssl->out_msg, ssl->out_msglen );
1608 ret = mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
1609 if( ret != 0 )
1610 goto hmac_failed_etm_disabled;
1611 ret = mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1612 if( ret != 0 )
1613 goto hmac_failed_etm_disabled;
Hanno Becker992b6872017-11-09 18:57:39 +00001614
1615 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001616
1617 hmac_failed_etm_disabled:
Gilles Peskine957e3832021-12-10 21:33:21 +01001618 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001619 if( ret != 0 )
1620 {
1621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_hmac_xxx", ret );
1622 return( ret );
1623 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 }
1625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 }
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 ssl->out_msg + ssl->out_msglen,
1634 ssl->transform_out->maclen );
1635
1636 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001638 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001639#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 /*
1642 * Encrypt
1643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1645 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001647 int ret;
1648 size_t olen = 0;
1649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 "including %d bytes of padding",
1652 ssl->out_msglen, 0 ) );
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001655 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001656 ssl->transform_out->ivlen,
1657 ssl->out_msg, ssl->out_msglen,
1658 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001661 return( ret );
1662 }
1663
1664 if( ssl->out_msglen != olen )
1665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1667 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 }
Paul Bakker68884e32013-01-07 18:20:04 +01001670 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001672#if defined(MBEDTLS_GCM_C) || \
1673 defined(MBEDTLS_CCM_C) || \
1674 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001676 mode == MBEDTLS_MODE_CCM ||
1677 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001678 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001679 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001680 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001681 unsigned char *enc_msg;
1682 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001683 unsigned char iv[12];
1684 mbedtls_ssl_transform *transform = ssl->transform_out;
1685 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001687 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001688
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001689 /*
1690 * Prepare additional authenticated data
1691 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001692 memcpy( add_data, ssl->out_ctr, 8 );
1693 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001695 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001696 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1697 add_data[12] = ssl->out_msglen & 0xFF;
1698
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001699 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001700
Paul Bakker68884e32013-01-07 18:20:04 +01001701 /*
1702 * Generate IV
1703 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001704 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1705 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001706 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001707 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1708 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1709 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1710
1711 }
1712 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001714 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001715 unsigned char i;
1716
1717 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1718
1719 for( i = 0; i < 8; i++ )
1720 iv[i+4] ^= ssl->out_ctr[i];
1721 }
1722 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001723 {
1724 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001727 }
1728
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001729 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1730 iv, transform->ivlen );
1731 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1732 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001733
Paul Bakker68884e32013-01-07 18:20:04 +01001734 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001735 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001736 */
1737 enc_msg = ssl->out_msg;
1738 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001739 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001742 "including 0 bytes of padding",
1743 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001744
Paul Bakker68884e32013-01-07 18:20:04 +01001745 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001746 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001747 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001748 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1749 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001750 add_data, 13,
1751 enc_msg, enc_msglen,
1752 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001753 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001756 return( ret );
1757 }
1758
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001759 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001763 }
1764
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001765 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001766 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001772#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001775 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001776 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001777 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001778
Paul Bakker48916f92012-09-16 19:57:18 +00001779 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1780 ssl->transform_out->ivlen;
1781 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 padlen = 0;
1783
1784 for( i = 0; i <= padlen; i++ )
1785 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1786
1787 ssl->out_msglen += padlen + 1;
1788
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001789 enc_msglen = ssl->out_msglen;
1790 enc_msg = ssl->out_msg;
1791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001793 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001794 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1795 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001798 {
1799 /*
1800 * Generate IV
1801 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001802 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001803 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001804 if( ret != 0 )
1805 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001806
Paul Bakker92be97b2013-01-02 17:30:03 +01001807 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001808 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001809
1810 /*
1811 * Fix pointer positions and message length with added IV
1812 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001813 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001814 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001815 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001820 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001821 ssl->out_msglen, ssl->transform_out->ivlen,
1822 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001825 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001826 ssl->transform_out->ivlen,
1827 enc_msg, enc_msglen,
1828 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001831 return( ret );
1832 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001833
Paul Bakkercca5b812013-08-31 17:40:26 +02001834 if( enc_msglen != olen )
1835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001838 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1841 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001842 {
1843 /*
1844 * Save IV in SSL3 and TLS1
1845 */
1846 memcpy( ssl->transform_out->iv_enc,
1847 ssl->transform_out->cipher_ctx_enc.iv,
1848 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001850#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001854 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001855 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1856
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001857 /*
1858 * MAC(MAC_write_key, seq_num +
1859 * TLSCipherText.type +
1860 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001861 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001862 * IV + // except for TLS 1.0
1863 * ENC(content + padding + padding_length));
1864 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001865 unsigned char pseudo_hdr[13];
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001868
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001869 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1870 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001871 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1872 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001875
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001876 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1877 if( ret != 0 )
1878 goto hmac_failed_etm_enabled;
1879 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
1880 ssl->out_iv, ssl->out_msglen );
1881 if( ret != 0 )
1882 goto hmac_failed_etm_enabled;
1883 ret = mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
1884 if( ret != 0 )
1885 goto hmac_failed_etm_enabled;
1886 ret = mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1887 if( ret != 0 )
1888 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001889
Hanno Becker3d8c9072018-01-05 16:24:22 +00001890 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1891 ssl->transform_out->maclen );
1892
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001893 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 auth_done++;
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001895
1896 hmac_failed_etm_enabled:
Gilles Peskine957e3832021-12-10 21:33:21 +01001897 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001898 if( ret != 0 )
1899 {
1900 MBEDTLS_SSL_DEBUG_RET( 1, "HMAC calculation failed", ret );
1901 return( ret );
1902 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001903 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001906 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001907#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001913 /* Make extra sure authentication was performed, exactly once */
1914 if( auth_done != 1 )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001918 }
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
1922 return( 0 );
1923}
1924
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001925#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001926/*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001927 * Constant-flow conditional memcpy:
1928 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1929 * - otherwise, a no-op,
1930 * but with execution flow independent of the values of c1 and c2.
1931 *
1932 * Use only bit operations to avoid branches that could be used by some
1933 * compilers on some platforms to translate comparison operators.
1934 */
Manuel Pégourié-Gonnard94fd8dc2020-07-28 11:56:05 +02001935static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1936 const unsigned char *src,
1937 size_t len,
1938 size_t c1, size_t c2 )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001939{
1940 /* diff = 0 if c1 == c2, non-zero otherwise */
1941 const size_t diff = c1 ^ c2;
1942
1943 /* MSVC has a warning about unary minus on unsigned integer types,
1944 * but this is well-defined and precisely what we want to do here. */
1945#if defined(_MSC_VER)
1946#pragma warning( push )
1947#pragma warning( disable : 4146 )
1948#endif
1949
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001950 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001951 const size_t diff_msb = ( diff | -diff );
1952
1953 /* diff1 = c1 != c2 */
1954 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1955
1956 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001957 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001958
1959#if defined(_MSC_VER)
1960#pragma warning( pop )
1961#endif
1962
1963 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnard253b0de2020-07-29 10:04:36 +02001964 size_t i;
1965 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001966 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1967}
1968
1969/*
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001970 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnarde1b1fff2020-07-29 12:54:04 +02001971 *
1972 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1973 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001974 */
1975int mbedtls_ssl_cf_hmac(
1976 mbedtls_md_context_t *ctx,
1977 const unsigned char *add_data, size_t add_data_len,
1978 const unsigned char *data, size_t data_len_secret,
1979 size_t min_data_len, size_t max_data_len,
1980 unsigned char *output )
1981{
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001982 /*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001983 * This function breaks the HMAC abstraction and uses the md_clone()
1984 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001985 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001986 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001987 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001988 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001989 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001990 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1991 * minlen, then cloning the context, and for each byte up to maxlen
1992 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001993 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001994 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001995 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001996 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001997 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1998 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001999 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnarda6c13172020-07-28 11:45:02 +02002000 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002001 const unsigned char * const okey = ikey + block_size;
2002 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002003
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002004 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
2005 mbedtls_md_context_t aux;
2006 size_t offset;
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002007 int ret;
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002008
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002009 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002010
2011#define MD_CHK( func_call ) \
2012 do { \
2013 ret = (func_call); \
2014 if( ret != 0 ) \
2015 goto cleanup; \
2016 } while( 0 )
2017
2018 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002019
2020 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
2021 * so we can start directly with the message */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002022 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
2023 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002024
2025 /* For each possible length, compute the hash up to that point */
2026 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002027 {
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002028 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
2029 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002030 /* Keep only the correct inner_hash in the output buffer */
2031 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
2032 offset, data_len_secret );
2033
2034 if( offset < max_data_len )
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002035 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002036 }
2037
Manuel Pégourié-Gonnard7f0d1932021-05-19 10:44:43 +02002038 /* The context needs to finish() before it starts() again */
2039 MD_CHK( mbedtls_md_finish( ctx, aux_out ) );
2040
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002041 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002042 MD_CHK( mbedtls_md_starts( ctx ) );
2043 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
2044 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
2045 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002046
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002047 /* Done, get ready for next time */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002048 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002049
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002050#undef MD_CHK
2051
2052cleanup:
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002053 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002054 return( ret );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002055}
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002056
2057/*
2058 * Constant-flow memcpy from variable position in buffer.
2059 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardab9ec322020-08-25 11:43:10 +02002060 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002061 */
2062void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
2063 const unsigned char *src_base,
2064 size_t offset_secret,
2065 size_t offset_min, size_t offset_max,
2066 size_t len )
2067{
Manuel Pégourié-Gonnardab9ec322020-08-25 11:43:10 +02002068 size_t offset;
2069
2070 for( offset = offset_min; offset <= offset_max; offset++ )
2071 {
2072 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
2073 offset, offset_secret );
2074 }
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002075}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02002076#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002081 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002082#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002083 size_t padlen = 0, correct = 1;
2084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002088 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002092 }
2093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002095
Paul Bakker48916f92012-09-16 19:57:18 +00002096 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002099 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2104 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002105 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002106 int ret;
2107 size_t olen = 0;
2108
Paul Bakker68884e32013-01-07 18:20:04 +01002109 padlen = 0;
2110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002112 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002113 ssl->transform_in->ivlen,
2114 ssl->in_msg, ssl->in_msglen,
2115 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002118 return( ret );
2119 }
2120
2121 if( ssl->in_msglen != olen )
2122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
Paul Bakker68884e32013-01-07 18:20:04 +01002127 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002129#if defined(MBEDTLS_GCM_C) || \
2130 defined(MBEDTLS_CCM_C) || \
2131 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002133 mode == MBEDTLS_MODE_CCM ||
2134 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002135 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002136 int ret;
2137 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002138 unsigned char *dec_msg;
2139 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002140 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141 unsigned char iv[12];
2142 mbedtls_ssl_transform *transform = ssl->transform_in;
2143 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002145 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002146
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002147 /*
2148 * Compute and update sizes
2149 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002150 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002153 "+ taglen (%d)", ssl->in_msglen,
2154 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002156 }
2157 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2158
Paul Bakker68884e32013-01-07 18:20:04 +01002159 dec_msg = ssl->in_msg;
2160 dec_msg_result = ssl->in_msg;
2161 ssl->in_msglen = dec_msglen;
2162
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002163 /*
2164 * Prepare additional authenticated data
2165 */
Paul Bakker68884e32013-01-07 18:20:04 +01002166 memcpy( add_data, ssl->in_ctr, 8 );
2167 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002169 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002170 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2171 add_data[12] = ssl->in_msglen & 0xFF;
2172
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002173 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002174
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002175 /*
2176 * Prepare IV
2177 */
2178 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2179 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002180 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002181 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2182 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002183
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 }
2185 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2186 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002187 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002188 unsigned char i;
2189
2190 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2191
2192 for( i = 0; i < 8; i++ )
2193 iv[i+4] ^= ssl->in_ctr[i];
2194 }
2195 else
2196 {
2197 /* Reminder if we ever add an AEAD mode with a different size */
2198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2200 }
2201
2202 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002204
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002205 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002206 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002209 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002210 add_data, 13,
2211 dec_msg, dec_msglen,
2212 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002213 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2218 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002219
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002220 return( ret );
2221 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002222 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002223
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002224 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002228 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002229 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002232#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 {
Paul Bakker45829992013-01-03 14:52:21 +01002235 /*
2236 * Decrypt and check the padding
2237 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002238 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002239 unsigned char *dec_msg;
2240 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002241 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002242 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002243 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002244
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 /*
Paul Bakker45829992013-01-03 14:52:21 +01002246 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2249 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002250 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002251#endif
Paul Bakker45829992013-01-03 14:52:21 +01002252
2253 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2254 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002257 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2258 ssl->transform_in->ivlen,
2259 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002261 }
2262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263 dec_msglen = ssl->in_msglen;
2264 dec_msg = ssl->in_msg;
2265 dec_msg_result = ssl->in_msg;
2266
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002267 /*
2268 * Authenticate before decrypt if enabled
2269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2271 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002272 {
Hanno Becker992b6872017-11-09 18:57:39 +00002273 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002274 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002278 dec_msglen -= ssl->transform_in->maclen;
2279 ssl->in_msglen -= ssl->transform_in->maclen;
2280
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002281 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2282 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2283 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2284 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002287
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002288 ret = mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2289 if( ret != 0 )
2290 goto hmac_failed_etm_enabled;
2291 ret = mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
2292 ssl->in_iv, ssl->in_msglen );
2293 if( ret != 0 )
2294 goto hmac_failed_etm_enabled;
2295 ret = mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
2296 if( ret != 0 )
2297 goto hmac_failed_etm_enabled;
2298 ret = mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
2299 if( ret != 0 )
2300 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002304 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002305 ssl->transform_in->maclen );
2306
Hanno Becker992b6872017-11-09 18:57:39 +00002307 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2308 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311
Gilles Peskine957e3832021-12-10 21:33:21 +01002312 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
2313 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002315 auth_done++;
Gilles Peskine957e3832021-12-10 21:33:21 +01002316
2317 hmac_failed_etm_enabled:
2318 mbedtls_platform_zeroize( mac_expect, ssl->transform_in->maclen );
2319 if( ret != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002320 {
2321 if( ret != MBEDTLS_ERR_SSL_INVALID_MAC )
2322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_hmac_xxx", ret );
Gilles Peskine957e3832021-12-10 21:33:21 +01002323 return( ret );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002324 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002327
2328 /*
2329 * Check length sanity
2330 */
2331 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002334 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002339 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002340 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002343 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002344 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002345 dec_msglen -= ssl->transform_in->ivlen;
2346 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002347
Paul Bakker48916f92012-09-16 19:57:18 +00002348 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002349 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002354 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002355 ssl->transform_in->ivlen,
2356 dec_msg, dec_msglen,
2357 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002360 return( ret );
2361 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002362
Paul Bakkercca5b812013-08-31 17:40:26 +02002363 if( dec_msglen != olen )
2364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002367 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2370 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002371 {
2372 /*
2373 * Save IV in SSL3 and TLS1
2374 */
2375 memcpy( ssl->transform_in->iv_dec,
2376 ssl->transform_in->cipher_ctx_dec.iv,
2377 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002382
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002383 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002384 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_DEBUG_ALL)
2387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002388 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002389#endif
Paul Bakker45829992013-01-03 14:52:21 +01002390 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002391 correct = 0;
2392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_SSL_PROTO_SSL3)
2395 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 {
Paul Bakker48916f92012-09-16 19:57:18 +00002397 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#if defined(MBEDTLS_SSL_DEBUG_ALL)
2400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002402 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002403#endif
Paul Bakker45829992013-01-03 14:52:21 +01002404 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406 }
2407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2409#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2410 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2411 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
2413 /*
Paul Bakker45829992013-01-03 14:52:21 +01002414 * TLSv1+: always check the padding up to the first failure
2415 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002417 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002418 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002419 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002420
Paul Bakker956c9e02013-12-19 14:42:28 +01002421 /*
2422 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002423 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002424 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002425 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002426 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002427 *
2428 * In both cases we reset padding_idx to a safe value (0) to
2429 * prevent out-of-buffer reads.
2430 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002431 correct &= ( padlen <= ssl->in_msglen );
2432 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002433 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002434
2435 padding_idx *= correct;
2436
Angus Grattonb512bc12018-06-19 15:57:50 +10002437 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002438 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002439 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002440 pad_count += real_count *
2441 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2442 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002443
2444 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002447 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002449#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002450 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2454 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002458 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002459
2460 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002462 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002463#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002469#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
2474 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002475 * Authenticate if not done yet.
2476 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002478#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002479 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002480 {
Hanno Becker992b6872017-11-09 18:57:39 +00002481 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002482 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Gilles Peskine957e3832021-12-10 21:33:21 +01002483 int ret = 0;
Paul Bakker1e5369c2013-12-19 16:40:57 +01002484
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002485 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002487 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2488 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_PROTO_SSL3)
2491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002492 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002493 ret = ssl_mac( &ssl->transform_in->md_ctx_dec,
2494 ssl->transform_in->mac_dec,
2495 ssl->in_msg, ssl->in_msglen,
2496 ssl->in_ctr, ssl->in_msgtype,
2497 mac_expect );
2498 if( ret != 0 )
2499 {
2500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_mac", ret );
2501 return( ret );
2502 }
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002503 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2504 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002505 }
2506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2510 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002511 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002512 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002513
2514 /*
2515 * The next two sizes are the minimum and maximum values of
2516 * in_msglen over all padlen values.
2517 *
2518 * They're independent of padlen, since we previously did
2519 * in_msglen -= padlen.
2520 *
2521 * Note that max_len + maclen is never more than the buffer
2522 * length, as we previously did in_msglen -= maclen too.
2523 */
2524 const size_t max_len = ssl->in_msglen + padlen;
2525 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2526
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002527 memcpy( add_data + 0, ssl->in_ctr, 8 );
2528 memcpy( add_data + 8, ssl->in_hdr, 3 );
2529 memcpy( add_data + 11, ssl->in_len, 2 );
2530
2531 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2532 add_data, sizeof( add_data ),
2533 ssl->in_msg, ssl->in_msglen,
2534 min_len, max_len,
2535 mac_expect );
2536 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002537 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
Gilles Peskine957e3832021-12-10 21:33:21 +01002539 goto hmac_failed_etm_disabled;
Gilles Peskine20b44082018-05-29 14:06:49 +02002540 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002541
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002542 mbedtls_ssl_cf_memcpy_offset( mac_peer, ssl->in_msg,
2543 ssl->in_msglen,
2544 min_len, max_len,
2545 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002546 }
2547 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2549 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002555#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002556 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002557 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002560 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002561 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#if defined(MBEDTLS_SSL_DEBUG_ALL)
2564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002565#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002566 correct = 0;
2567 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002568 auth_done++;
Gilles Peskine957e3832021-12-10 21:33:21 +01002569
2570 hmac_failed_etm_disabled:
2571 mbedtls_platform_zeroize( mac_peer, ssl->transform_in->maclen );
2572 mbedtls_platform_zeroize( mac_expect, ssl->transform_in->maclen );
2573 if( ret != 0 )
2574 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002575 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002576
2577 /*
2578 * Finally check the correct flag
2579 */
2580 if( correct == 0 )
2581 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002582#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002583
2584 /* Make extra sure authentication was performed, exactly once */
2585 if( auth_done != 1 )
2586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
2591 if( ssl->in_msglen == 0 )
2592 {
Angus Gratton34817922018-06-19 15:58:22 +10002593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2594 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2595 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2596 {
2597 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2600 }
2601#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2602
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 ssl->nb_zero++;
2604
2605 /*
2606 * Three or more empty messages may be a DoS attack
2607 * (excessive CPU consumption).
2608 */
2609 if( ssl->nb_zero > 3 )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
2615 }
2616 else
2617 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002621 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002622 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002623 }
2624 else
2625#endif
2626 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002628 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2629 if( ++ssl->in_ctr[i - 1] != 0 )
2630 break;
2631
2632 /* The loop goes to its end iff the counter is wrapping */
2633 if( i == ssl_ep_len( ssl ) )
2634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2636 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002637 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002638 }
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 return( 0 );
2643}
2644
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002645#undef MAC_NONE
2646#undef MAC_PLAINTEXT
2647#undef MAC_CIPHERTEXT
2648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002650/*
2651 * Compression/decompression functions
2652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002654{
2655 int ret;
2656 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002657 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002658 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002659 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002662
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002663 if( len_pre == 0 )
2664 return( 0 );
2665
Paul Bakker2770fbd2012-07-03 13:30:23 +00002666 memcpy( msg_pre, ssl->out_msg, len_pre );
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002669 ssl->out_msglen ) );
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002672 ssl->out_msg, ssl->out_msglen );
2673
Paul Bakker48916f92012-09-16 19:57:18 +00002674 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2675 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2676 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002677 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002678
Paul Bakker48916f92012-09-16 19:57:18 +00002679 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002680 if( ret != Z_OK )
2681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2683 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002684 }
2685
Angus Grattond8213d02016-05-25 20:56:48 +10002686 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002687 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690 ssl->out_msglen ) );
2691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693 ssl->out_msg, ssl->out_msglen );
2694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002696
2697 return( 0 );
2698}
2699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002701{
2702 int ret;
2703 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002704 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002706 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002710 if( len_pre == 0 )
2711 return( 0 );
2712
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713 memcpy( msg_pre, ssl->in_msg, len_pre );
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002716 ssl->in_msglen ) );
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002719 ssl->in_msg, ssl->in_msglen );
2720
Paul Bakker48916f92012-09-16 19:57:18 +00002721 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2722 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2723 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002724 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002725 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002726
Paul Bakker48916f92012-09-16 19:57:18 +00002727 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728 if( ret != Z_OK )
2729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2731 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732 }
2733
Angus Grattond8213d02016-05-25 20:56:48 +10002734 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002735 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002738 ssl->in_msglen ) );
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741 ssl->in_msg, ssl->in_msglen );
2742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002744
2745 return( 0 );
2746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2750static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_SSL_PROTO_DTLS)
2753static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002754{
2755 /* If renegotiation is not enforced, retransmit until we would reach max
2756 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002757 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002758 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002759 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002760 unsigned char doublings = 1;
2761
2762 while( ratio != 0 )
2763 {
2764 ++doublings;
2765 ratio >>= 1;
2766 }
2767
2768 if( ++ssl->renego_records_seen > doublings )
2769 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002771 return( 0 );
2772 }
2773 }
2774
2775 return( ssl_write_hello_request( ssl ) );
2776}
2777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002779
Paul Bakker5121ce52009-01-03 21:22:43 +00002780/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002781 * Fill the input message buffer by appending data to it.
2782 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002783 *
2784 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2785 * available (from this read and/or a previous one). Otherwise, an error code
2786 * is returned (possibly EOF or WANT_READ).
2787 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002788 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2789 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2790 * since we always read a whole datagram at once.
2791 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002792 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002793 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796{
Paul Bakker23986e52011-04-24 08:57:21 +00002797 int ret;
2798 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002802 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002805 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002807 }
2808
Angus Grattond8213d02016-05-25 20:56:48 +10002809 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002813 }
2814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002816 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002818 uint32_t timeout;
2819
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002820 /* Just to be sure */
2821 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2822 {
2823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2824 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2826 }
2827
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002828 /*
2829 * The point is, we need to always read a full datagram at once, so we
2830 * sometimes read more then requested, and handle the additional data.
2831 * It could be the rest of the current record (while fetching the
2832 * header) and/or some other records in the same datagram.
2833 */
2834
2835 /*
2836 * Move to the next record in the already read datagram if applicable
2837 */
2838 if( ssl->next_record_offset != 0 )
2839 {
2840 if( ssl->in_left < ssl->next_record_offset )
2841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002844 }
2845
2846 ssl->in_left -= ssl->next_record_offset;
2847
2848 if( ssl->in_left != 0 )
2849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002851 ssl->next_record_offset ) );
2852 memmove( ssl->in_hdr,
2853 ssl->in_hdr + ssl->next_record_offset,
2854 ssl->in_left );
2855 }
2856
2857 ssl->next_record_offset = 0;
2858 }
2859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002862
2863 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002864 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002865 */
2866 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002869 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002870 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002871
2872 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002873 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002874 * are not at the beginning of a new record, the caller did something
2875 * wrong.
2876 */
2877 if( ssl->in_left != 0 )
2878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002881 }
2882
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002883 /*
2884 * Don't even try to read if time's out already.
2885 * This avoids by-passing the timer when repeatedly receiving messages
2886 * that will end up being dropped.
2887 */
2888 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002889 {
2890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002891 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002892 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002893 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002894 {
Angus Grattond8213d02016-05-25 20:56:48 +10002895 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002898 timeout = ssl->handshake->retransmit_timeout;
2899 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002900 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002903
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002904 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002905 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2906 timeout );
2907 else
2908 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002911
2912 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002914 }
2915
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002916 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002919 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002922 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002923 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002926 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002927 }
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002932 return( ret );
2933 }
2934
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002935 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002936 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002938 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002940 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002941 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002944 return( ret );
2945 }
2946
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002947 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002950 }
2951
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 if( ret < 0 )
2953 return( ret );
2954
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002955 ssl->in_left = ret;
2956 }
2957 else
2958#endif
2959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002961 ssl->in_left, nb_want ) );
2962
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002963 while( ssl->in_left < nb_want )
2964 {
2965 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002966
2967 if( ssl_check_timer( ssl ) != 0 )
2968 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2969 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002970 {
2971 if( ssl->f_recv_timeout != NULL )
2972 {
2973 ret = ssl->f_recv_timeout( ssl->p_bio,
2974 ssl->in_hdr + ssl->in_left, len,
2975 ssl->conf->read_timeout );
2976 }
2977 else
2978 {
2979 ret = ssl->f_recv( ssl->p_bio,
2980 ssl->in_hdr + ssl->in_left, len );
2981 }
2982 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002985 ssl->in_left, nb_want ) );
2986 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002987
2988 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002990
2991 if( ret < 0 )
2992 return( ret );
2993
makise-homura8c3fa632020-08-24 18:33:54 +03002994 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002995 {
Darryl Green11999bb2018-03-13 15:22:58 +00002996 MBEDTLS_SSL_DEBUG_MSG( 1,
2997 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002998 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3000 }
3001
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003002 ssl->in_left += ret;
3003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 }
3005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
3008 return( 0 );
3009}
3010
3011/*
3012 * Flush any data not yet written
3013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003015{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003016 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003017 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003021 if( ssl->f_send == NULL )
3022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003024 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003026 }
3027
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003028 /* Avoid incrementing counter if data is flushed */
3029 if( ssl->out_left == 0 )
3030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003032 return( 0 );
3033 }
3034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 while( ssl->out_left > 0 )
3036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3038 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Hanno Becker2b1e3542018-08-06 11:19:13 +01003040 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003041 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
3045 if( ret <= 0 )
3046 return( ret );
3047
makise-homura8c3fa632020-08-24 18:33:54 +03003048 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003049 {
Darryl Green11999bb2018-03-13 15:22:58 +00003050 MBEDTLS_SSL_DEBUG_MSG( 1,
3051 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003052 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3054 }
3055
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 ssl->out_left -= ret;
3057 }
3058
Hanno Becker2b1e3542018-08-06 11:19:13 +01003059#if defined(MBEDTLS_SSL_PROTO_DTLS)
3060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003061 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003062 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003063 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003064 else
3065#endif
3066 {
3067 ssl->out_hdr = ssl->out_buf + 8;
3068 }
3069 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
3073 return( 0 );
3074}
3075
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003076/*
3077 * Functions to handle the DTLS retransmission state machine
3078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080/*
3081 * Append current handshake message to current outgoing flight
3082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3087 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3088 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003089
3090 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003091 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003092 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003095 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003096 }
3097
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003098 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003102 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003103 }
3104
3105 /* Copy current handshake message with headers */
3106 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3107 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003108 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003109 msg->next = NULL;
3110
3111 /* Append to the current flight */
3112 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003113 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114 else
3115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117 while( cur->next != NULL )
3118 cur = cur->next;
3119 cur->next = msg;
3120 }
3121
Hanno Becker3b235902018-08-06 09:54:53 +01003122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123 return( 0 );
3124}
3125
3126/*
3127 * Free the current flight of handshake messages
3128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 mbedtls_ssl_flight_item *cur = flight;
3132 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003133
3134 while( cur != NULL )
3135 {
3136 next = cur->next;
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 mbedtls_free( cur->p );
3139 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003140
3141 cur = next;
3142 }
3143}
3144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3146static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003147#endif
3148
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003150 * Swap transform_out and out_ctr with the alternative ones
3151 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003152static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003155 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003156#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3157 int ret;
3158#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003159
3160 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003163 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003164 }
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003167
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003168 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003169 tmp_transform = ssl->transform_out;
3170 ssl->transform_out = ssl->handshake->alt_transform_out;
3171 ssl->handshake->alt_transform_out = tmp_transform;
3172
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003173 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003174 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3175 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003176 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003177
3178 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003179 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3182 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3187 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003188 }
3189 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003190#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3191
3192 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193}
3194
3195/*
3196 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003197 */
3198int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3199{
3200 int ret = 0;
3201
3202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3203
3204 ret = mbedtls_ssl_flight_transmit( ssl );
3205
3206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3207
3208 return( ret );
3209}
3210
3211/*
3212 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003213 *
3214 * Need to remember the current message in case flush_output returns
3215 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003216 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003217 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003218int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003219{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003220 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003224 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003226
3227 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003228 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003229 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3230 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003233 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003234
3235 while( ssl->handshake->cur_msg != NULL )
3236 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003237 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003238 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003239
Hanno Beckere1dcb032018-08-17 16:47:58 +01003240 int const is_finished =
3241 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3242 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3243
Hanno Becker04da1892018-08-14 13:22:10 +01003244 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3245 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3246
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003247 /* Swap epochs before sending Finished: we can't do it after
3248 * sending ChangeCipherSpec, in case write returns WANT_READ.
3249 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003250 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003251 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003253 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3254 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003255 }
3256
Hanno Becker67bc7c32018-08-06 11:33:50 +01003257 ret = ssl_get_remaining_payload_in_datagram( ssl );
3258 if( ret < 0 )
3259 return( ret );
3260 max_frag_len = (size_t) ret;
3261
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003262 /* CCS is copied as is, while HS messages may need fragmentation */
3263 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3264 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003265 if( max_frag_len == 0 )
3266 {
3267 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3268 return( ret );
3269
3270 continue;
3271 }
3272
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003273 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003274 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003275 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003276
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003277 /* Update position inside current message */
3278 ssl->handshake->cur_msg_p += cur->len;
3279 }
3280 else
3281 {
3282 const unsigned char * const p = ssl->handshake->cur_msg_p;
3283 const size_t hs_len = cur->len - 12;
3284 const size_t frag_off = p - ( cur->p + 12 );
3285 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003286 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003287
Hanno Beckere1dcb032018-08-17 16:47:58 +01003288 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003289 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003290 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003291 {
3292 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3293 return( ret );
3294 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003295
Hanno Becker67bc7c32018-08-06 11:33:50 +01003296 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3297 return( ret );
3298
3299 continue;
3300 }
3301 max_hs_frag_len = max_frag_len - 12;
3302
3303 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3304 max_hs_frag_len : rem_len;
3305
3306 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003307 {
3308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003309 (unsigned) cur_hs_frag_len,
3310 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003311 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003312
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003313 /* Messages are stored with handshake headers as if not fragmented,
3314 * copy beginning of headers then fill fragmentation fields.
3315 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3316 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003317
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003318 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3319 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3320 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3321
Hanno Becker67bc7c32018-08-06 11:33:50 +01003322 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3323 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3324 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003325
3326 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3327
Hanno Becker3f7b9732018-08-28 09:53:25 +01003328 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003329 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3330 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003331 ssl->out_msgtype = cur->type;
3332
3333 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003334 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003335 }
3336
3337 /* If done with the current message move to the next one if any */
3338 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3339 {
3340 if( cur->next != NULL )
3341 {
3342 ssl->handshake->cur_msg = cur->next;
3343 ssl->handshake->cur_msg_p = cur->next->p + 12;
3344 }
3345 else
3346 {
3347 ssl->handshake->cur_msg = NULL;
3348 ssl->handshake->cur_msg_p = NULL;
3349 }
3350 }
3351
3352 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003353 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003356 return( ret );
3357 }
3358 }
3359
Hanno Becker67bc7c32018-08-06 11:33:50 +01003360 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3361 return( ret );
3362
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003363 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3365 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003366 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003369 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3370 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003371
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373
3374 return( 0 );
3375}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003376
3377/*
3378 * To be called when the last message of an incoming flight is received.
3379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003381{
3382 /* We won't need to resend that one any more */
3383 ssl_flight_free( ssl->handshake->flight );
3384 ssl->handshake->flight = NULL;
3385 ssl->handshake->cur_msg = NULL;
3386
3387 /* The next incoming flight will start with this msg_seq */
3388 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3389
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003390 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003391 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003392
Hanno Becker0271f962018-08-16 13:23:47 +01003393 /* Clear future message buffering structure. */
3394 ssl_buffering_free( ssl );
3395
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003396 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003397 ssl_set_timer( ssl, 0 );
3398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3400 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003403 }
3404 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003407
3408/*
3409 * To be called when the last message of an outgoing flight is send.
3410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003412{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003413 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003414 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3417 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003420 }
3421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003427 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429
3430/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003431 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003432 *
3433 * - fill in handshake headers
3434 * - update handshake checksum
3435 * - DTLS: save message for resending
3436 * - then pass to the record layer
3437 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003438 * DTLS: except for HelloRequest, messages are only queued, and will only be
3439 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003440 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003441 * Inputs:
3442 * - ssl->out_msglen: 4 + actual handshake message len
3443 * (4 is the size of handshake headers for TLS)
3444 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3445 * - ssl->out_msg + 4: the handshake message body
3446 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003447 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448 * - ssl->out_msglen: the length of the record contents
3449 * (including handshake headers but excluding record headers)
3450 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003452int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003453{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003454 int ret;
3455 const size_t hs_len = ssl->out_msglen - 4;
3456 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3459
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003460 /*
3461 * Sanity checks
3462 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003463 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003464 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3465 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003466 /* In SSLv3, the client might send a NoCertificate alert. */
3467#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3468 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3469 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3470 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3471#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3472 {
3473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3475 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003478 /* Whenever we send anything different from a
3479 * HelloRequest we should be in a handshake - double check. */
3480 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3481 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003482 ssl->handshake == NULL )
3483 {
3484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003490 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003492 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003495 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003496#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003497
Hanno Beckerb50a2532018-08-06 11:52:54 +01003498 /* Double-check that we did not exceed the bounds
3499 * of the outgoing record buffer.
3500 * This should never fail as the various message
3501 * writing functions must obey the bounds of the
3502 * outgoing record buffer, but better be safe.
3503 *
3504 * Note: We deliberately do not check for the MTU or MFL here.
3505 */
3506 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3507 {
3508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3509 "size %u, maximum %u",
3510 (unsigned) ssl->out_msglen,
3511 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3513 }
3514
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003515 /*
3516 * Fill handshake headers
3517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003520 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3521 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3522 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003524 /*
3525 * DTLS has additional fields in the Handshake layer,
3526 * between the length field and the actual payload:
3527 * uint16 message_seq;
3528 * uint24 fragment_offset;
3529 * uint24 fragment_length;
3530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003532 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003533 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003534 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003535 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003536 {
3537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3538 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003539 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003540 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3542 }
3543
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003544 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003545 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003546
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003547 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003548 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003549 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003550 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3551 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3552 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003553 }
3554 else
3555 {
3556 ssl->out_msg[4] = 0;
3557 ssl->out_msg[5] = 0;
3558 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003559
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003560 /* Handshake hashes are computed without fragmentation,
3561 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003562 memset( ssl->out_msg + 6, 0x00, 3 );
3563 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003566
Hanno Becker0207e532018-08-28 10:28:28 +01003567 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003568 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3569 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 }
3571
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003572 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003575 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3576 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 {
3578 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 return( ret );
3582 }
3583 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003584 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003586 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003587 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003588 {
3589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3590 return( ret );
3591 }
3592 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003593
3594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3595
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003596 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003597}
3598
3599/*
3600 * Record layer functions
3601 */
3602
3603/*
3604 * Write current record.
3605 *
3606 * Uses:
3607 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3608 * - ssl->out_msglen: length of the record content (excl headers)
3609 * - ssl->out_msg: record content
3610 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003611int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003612{
3613 int ret, done = 0;
3614 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003615 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003616
3617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003620 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003622 {
3623 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626 return( ret );
3627 }
3628
3629 len = ssl->out_msglen;
3630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3634 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 ret = mbedtls_ssl_hw_record_write( ssl );
3639 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3642 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003643 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003644
3645 if( ret == 0 )
3646 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003649 if( !done )
3650 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003651 unsigned i;
3652 size_t protected_record_size;
3653
Paul Bakker05ef8352012-05-08 09:17:57 +00003654 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003656 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003657
Hanno Becker19859472018-08-06 09:40:20 +01003658 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003659 ssl->out_len[0] = (unsigned char)( len >> 8 );
3660 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003661
Paul Bakker48916f92012-09-16 19:57:18 +00003662 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003663 {
3664 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003667 return( ret );
3668 }
3669
3670 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003671 ssl->out_len[0] = (unsigned char)( len >> 8 );
3672 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 }
3674
Hanno Becker2b1e3542018-08-06 11:19:13 +01003675 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3676
3677#if defined(MBEDTLS_SSL_PROTO_DTLS)
3678 /* In case of DTLS, double-check that we don't exceed
3679 * the remaining space in the datagram. */
3680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3681 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003682 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003683 if( ret < 0 )
3684 return( ret );
3685
3686 if( protected_record_size > (size_t) ret )
3687 {
3688 /* Should never happen */
3689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3690 }
3691 }
3692#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003695 "version = [%d:%d], msglen = %d",
3696 ssl->out_hdr[0], ssl->out_hdr[1],
3697 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003700 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003701
3702 ssl->out_left += protected_record_size;
3703 ssl->out_hdr += protected_record_size;
3704 ssl_update_out_pointers( ssl, ssl->transform_out );
3705
Hanno Becker04484622018-08-06 09:49:38 +01003706 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3707 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3708 break;
3709
3710 /* The loop goes to its end iff the counter is wrapping */
3711 if( i == ssl_ep_len( ssl ) )
3712 {
3713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3714 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717
Hanno Becker67bc7c32018-08-06 11:33:50 +01003718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3720 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003721 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003722 size_t remaining;
3723 ret = ssl_get_remaining_payload_in_datagram( ssl );
3724 if( ret < 0 )
3725 {
3726 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3727 ret );
3728 return( ret );
3729 }
3730
3731 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003733 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003734 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003735 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003736 else
3737 {
Hanno Becker513815a2018-08-20 11:56:09 +01003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003739 }
3740 }
3741#endif /* MBEDTLS_SSL_PROTO_DTLS */
3742
3743 if( ( flush == SSL_FORCE_FLUSH ) &&
3744 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 return( ret );
3748 }
3749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751
3752 return( 0 );
3753}
3754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003756
3757static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3758{
3759 if( ssl->in_msglen < ssl->in_hslen ||
3760 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3761 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3762 {
3763 return( 1 );
3764 }
3765 return( 0 );
3766}
Hanno Becker44650b72018-08-16 12:51:11 +01003767
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003768static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003769{
3770 return( ( ssl->in_msg[9] << 16 ) |
3771 ( ssl->in_msg[10] << 8 ) |
3772 ssl->in_msg[11] );
3773}
3774
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003775static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003776{
3777 return( ( ssl->in_msg[6] << 16 ) |
3778 ( ssl->in_msg[7] << 8 ) |
3779 ssl->in_msg[8] );
3780}
3781
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003782static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003783{
3784 uint32_t msg_len, frag_off, frag_len;
3785
3786 msg_len = ssl_get_hs_total_len( ssl );
3787 frag_off = ssl_get_hs_frag_off( ssl );
3788 frag_len = ssl_get_hs_frag_len( ssl );
3789
3790 if( frag_off > msg_len )
3791 return( -1 );
3792
3793 if( frag_len > msg_len - frag_off )
3794 return( -1 );
3795
3796 if( frag_len + 12 > ssl->in_msglen )
3797 return( -1 );
3798
3799 return( 0 );
3800}
3801
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003802/*
3803 * Mark bits in bitmask (used for DTLS HS reassembly)
3804 */
3805static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3806{
3807 unsigned int start_bits, end_bits;
3808
3809 start_bits = 8 - ( offset % 8 );
3810 if( start_bits != 8 )
3811 {
3812 size_t first_byte_idx = offset / 8;
3813
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003814 /* Special case */
3815 if( len <= start_bits )
3816 {
3817 for( ; len != 0; len-- )
3818 mask[first_byte_idx] |= 1 << ( start_bits - len );
3819
3820 /* Avoid potential issues with offset or len becoming invalid */
3821 return;
3822 }
3823
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003824 offset += start_bits; /* Now offset % 8 == 0 */
3825 len -= start_bits;
3826
3827 for( ; start_bits != 0; start_bits-- )
3828 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3829 }
3830
3831 end_bits = len % 8;
3832 if( end_bits != 0 )
3833 {
3834 size_t last_byte_idx = ( offset + len ) / 8;
3835
3836 len -= end_bits; /* Now len % 8 == 0 */
3837
3838 for( ; end_bits != 0; end_bits-- )
3839 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3840 }
3841
3842 memset( mask + offset / 8, 0xFF, len / 8 );
3843}
3844
3845/*
3846 * Check that bitmask is full
3847 */
3848static int ssl_bitmask_check( unsigned char *mask, size_t len )
3849{
3850 size_t i;
3851
3852 for( i = 0; i < len / 8; i++ )
3853 if( mask[i] != 0xFF )
3854 return( -1 );
3855
3856 for( i = 0; i < len % 8; i++ )
3857 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3858 return( -1 );
3859
3860 return( 0 );
3861}
3862
Hanno Becker56e205e2018-08-16 09:06:12 +01003863/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003864static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003865 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003866{
Hanno Becker56e205e2018-08-16 09:06:12 +01003867 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003868
Hanno Becker56e205e2018-08-16 09:06:12 +01003869 alloc_len = 12; /* Handshake header */
3870 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003871
Hanno Beckerd07df862018-08-16 09:14:58 +01003872 if( add_bitmap )
3873 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003874
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003875 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003876}
Hanno Becker56e205e2018-08-16 09:06:12 +01003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003879
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003880static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003881{
3882 return( ( ssl->in_msg[1] << 16 ) |
3883 ( ssl->in_msg[2] << 8 ) |
3884 ssl->in_msg[3] );
3885}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003886
Simon Butcher99000142016-10-13 17:21:01 +01003887int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003892 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003894 }
3895
Hanno Becker12555c62018-08-16 12:47:53 +01003896 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003899 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003900 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003903 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003904 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003905 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003906 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003907
Hanno Becker44650b72018-08-16 12:51:11 +01003908 if( ssl_check_hs_header( ssl ) != 0 )
3909 {
3910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3911 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3912 }
3913
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003914 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003915 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3916 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3917 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3918 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003919 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003920 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3921 {
3922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3923 recv_msg_seq,
3924 ssl->handshake->in_msg_seq ) );
3925 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3926 }
3927
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003928 /* Retransmit only on last message from previous flight, to avoid
3929 * too many retransmissions.
3930 * Besides, No sane server ever retransmits HelloVerifyRequest */
3931 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003935 "message_seq = %d, start_of_flight = %d",
3936 recv_msg_seq,
3937 ssl->handshake->in_flight_start_seq ) );
3938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003942 return( ret );
3943 }
3944 }
3945 else
3946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003948 "message_seq = %d, expected = %d",
3949 recv_msg_seq,
3950 ssl->handshake->in_msg_seq ) );
3951 }
3952
Hanno Becker90333da2017-10-10 11:27:13 +01003953 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003954 }
3955 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003956
Hanno Becker6d97ef52018-08-16 13:09:04 +01003957 /* Message reassembly is handled alongside buffering of future
3958 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003959 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003960 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003961 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003964 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003965 }
3966 }
3967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969 /* With TLS we don't handle fragmentation (for now) */
3970 if( ssl->in_msglen < ssl->in_hslen )
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3973 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003974 }
3975
Simon Butcher99000142016-10-13 17:21:01 +01003976 return( 0 );
3977}
3978
3979void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3980{
Hanno Becker0271f962018-08-16 13:23:47 +01003981 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003982
Hanno Becker0271f962018-08-16 13:23:47 +01003983 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003984 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003985 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003986 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003987
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003988 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003991 ssl->handshake != NULL )
3992 {
Hanno Becker0271f962018-08-16 13:23:47 +01003993 unsigned offset;
3994 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003995
Hanno Becker0271f962018-08-16 13:23:47 +01003996 /* Increment handshake sequence number */
3997 hs->in_msg_seq++;
3998
3999 /*
4000 * Clear up handshake buffering and reassembly structure.
4001 */
4002
4003 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004004 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004005
4006 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004007 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4008 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004009 offset++, hs_buf++ )
4010 {
4011 *hs_buf = *(hs_buf + 1);
4012 }
4013
4014 /* Create a fresh last entry */
4015 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004016 }
4017#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004018}
4019
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004020/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004021 * DTLS anti-replay: RFC 6347 4.1.2.6
4022 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004023 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4024 * Bit n is set iff record number in_window_top - n has been seen.
4025 *
4026 * Usually, in_window_top is the last record number seen and the lsb of
4027 * in_window is set. The only exception is the initial state (record number 0
4028 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4031static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004032{
4033 ssl->in_window_top = 0;
4034 ssl->in_window = 0;
4035}
4036
4037static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4038{
4039 return( ( (uint64_t) buf[0] << 40 ) |
4040 ( (uint64_t) buf[1] << 32 ) |
4041 ( (uint64_t) buf[2] << 24 ) |
4042 ( (uint64_t) buf[3] << 16 ) |
4043 ( (uint64_t) buf[4] << 8 ) |
4044 ( (uint64_t) buf[5] ) );
4045}
4046
4047/*
4048 * Return 0 if sequence number is acceptable, -1 otherwise
4049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004051{
4052 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4053 uint64_t bit;
4054
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004055 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004056 return( 0 );
4057
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004058 if( rec_seqnum > ssl->in_window_top )
4059 return( 0 );
4060
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004061 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004062
4063 if( bit >= 64 )
4064 return( -1 );
4065
4066 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4067 return( -1 );
4068
4069 return( 0 );
4070}
4071
4072/*
4073 * Update replay window on new validated record
4074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004076{
4077 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4078
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004079 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004080 return;
4081
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004082 if( rec_seqnum > ssl->in_window_top )
4083 {
4084 /* Update window_top and the contents of the window */
4085 uint64_t shift = rec_seqnum - ssl->in_window_top;
4086
4087 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004088 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004089 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004090 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004091 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004092 ssl->in_window |= 1;
4093 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004094
4095 ssl->in_window_top = rec_seqnum;
4096 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004097 else
4098 {
4099 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004100 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004101
4102 if( bit < 64 ) /* Always true, but be extra sure */
4103 ssl->in_window |= (uint64_t) 1 << bit;
4104 }
4105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004107
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004108#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004109/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004110static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4111
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004112/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004113 * Without any SSL context, check if a datagram looks like a ClientHello with
4114 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004115 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004116 *
4117 * - if cookie is valid, return 0
4118 * - if ClientHello looks superficially valid but cookie is not,
4119 * fill obuf and set olen, then
4120 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4121 * - otherwise return a specific error code
4122 */
4123static int ssl_check_dtls_clihlo_cookie(
4124 mbedtls_ssl_cookie_write_t *f_cookie_write,
4125 mbedtls_ssl_cookie_check_t *f_cookie_check,
4126 void *p_cookie,
4127 const unsigned char *cli_id, size_t cli_id_len,
4128 const unsigned char *in, size_t in_len,
4129 unsigned char *obuf, size_t buf_len, size_t *olen )
4130{
4131 size_t sid_len, cookie_len;
4132 unsigned char *p;
4133
4134 if( f_cookie_write == NULL || f_cookie_check == NULL )
4135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4136
4137 /*
4138 * Structure of ClientHello with record and handshake headers,
4139 * and expected values. We don't need to check a lot, more checks will be
4140 * done when actually parsing the ClientHello - skipping those checks
4141 * avoids code duplication and does not make cookie forging any easier.
4142 *
4143 * 0-0 ContentType type; copied, must be handshake
4144 * 1-2 ProtocolVersion version; copied
4145 * 3-4 uint16 epoch; copied, must be 0
4146 * 5-10 uint48 sequence_number; copied
4147 * 11-12 uint16 length; (ignored)
4148 *
4149 * 13-13 HandshakeType msg_type; (ignored)
4150 * 14-16 uint24 length; (ignored)
4151 * 17-18 uint16 message_seq; copied
4152 * 19-21 uint24 fragment_offset; copied, must be 0
4153 * 22-24 uint24 fragment_length; (ignored)
4154 *
4155 * 25-26 ProtocolVersion client_version; (ignored)
4156 * 27-58 Random random; (ignored)
4157 * 59-xx SessionID session_id; 1 byte len + sid_len content
4158 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4159 * ...
4160 *
4161 * Minimum length is 61 bytes.
4162 */
4163 if( in_len < 61 ||
4164 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4165 in[3] != 0 || in[4] != 0 ||
4166 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4167 {
4168 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4169 }
4170
4171 sid_len = in[59];
4172 if( sid_len > in_len - 61 )
4173 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4174
4175 cookie_len = in[60 + sid_len];
4176 if( cookie_len > in_len - 60 )
4177 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4178
4179 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4180 cli_id, cli_id_len ) == 0 )
4181 {
4182 /* Valid cookie */
4183 return( 0 );
4184 }
4185
4186 /*
4187 * If we get here, we've got an invalid cookie, let's prepare HVR.
4188 *
4189 * 0-0 ContentType type; copied
4190 * 1-2 ProtocolVersion version; copied
4191 * 3-4 uint16 epoch; copied
4192 * 5-10 uint48 sequence_number; copied
4193 * 11-12 uint16 length; olen - 13
4194 *
4195 * 13-13 HandshakeType msg_type; hello_verify_request
4196 * 14-16 uint24 length; olen - 25
4197 * 17-18 uint16 message_seq; copied
4198 * 19-21 uint24 fragment_offset; copied
4199 * 22-24 uint24 fragment_length; olen - 25
4200 *
4201 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4202 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4203 *
4204 * Minimum length is 28.
4205 */
4206 if( buf_len < 28 )
4207 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4208
4209 /* Copy most fields and adapt others */
4210 memcpy( obuf, in, 25 );
4211 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4212 obuf[25] = 0xfe;
4213 obuf[26] = 0xff;
4214
4215 /* Generate and write actual cookie */
4216 p = obuf + 28;
4217 if( f_cookie_write( p_cookie,
4218 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4219 {
4220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4221 }
4222
4223 *olen = p - obuf;
4224
4225 /* Go back and fill length fields */
4226 obuf[27] = (unsigned char)( *olen - 28 );
4227
4228 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4229 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4230 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4231
4232 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4233 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4234
4235 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4236}
4237
4238/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004239 * Handle possible client reconnect with the same UDP quadruplet
4240 * (RFC 6347 Section 4.2.8).
4241 *
4242 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4243 * that looks like a ClientHello.
4244 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004245 * - if the input looks like a ClientHello without cookies,
4246 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004247 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004248 * - if the input looks like a ClientHello with a valid cookie,
4249 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004250 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004251 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004252 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004253 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004254 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4255 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004256 */
4257static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4258{
4259 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004260 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004261
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004262 ret = ssl_check_dtls_clihlo_cookie(
4263 ssl->conf->f_cookie_write,
4264 ssl->conf->f_cookie_check,
4265 ssl->conf->p_cookie,
4266 ssl->cli_id, ssl->cli_id_len,
4267 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004268 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004269
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004270 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4271
4272 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004273 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004274 int send_ret;
4275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4276 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4277 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004278 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004279 * If the error is permanent we'll catch it later,
4280 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004281 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4282 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4283 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004284
4285 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004286 }
4287
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004288 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004289 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004291 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4292 {
4293 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4294 return( ret );
4295 }
4296
4297 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004298 }
4299
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004300 return( ret );
4301}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004302#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004303
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004304/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004305 * ContentType type;
4306 * ProtocolVersion version;
4307 * uint16 epoch; // DTLS only
4308 * uint48 sequence_number; // DTLS only
4309 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004310 *
4311 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004312 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004313 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4314 *
4315 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004316 * 1. proceed with the record if this function returns 0
4317 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4318 * 3. return CLIENT_RECONNECT if this function return that value
4319 * 4. drop the whole datagram if this function returns anything else.
4320 * Point 2 is needed when the peer is resending, and we have already received
4321 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004324{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004325 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004328
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004330 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004331 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004335 ssl->in_msgtype,
4336 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004337
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004338 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4340 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4341 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4342 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004345
4346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004347 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4348 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004349 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4350#endif /* MBEDTLS_SSL_PROTO_DTLS */
4351 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4352 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004355 }
4356
4357 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004358 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4361 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 }
4363
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004364 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4367 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 }
4369
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004370 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004371 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004372 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004376 }
4377
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004378 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004379 * DTLS-related tests.
4380 * Check epoch before checking length constraint because
4381 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4382 * message gets duplicated before the corresponding Finished message,
4383 * the second ChangeCipherSpec should be discarded because it belongs
4384 * to an old epoch, but not because its length is shorter than
4385 * the minimum record length for packets using the new record transform.
4386 * Note that these two kinds of failures are handled differently,
4387 * as an unexpected record is silently skipped but an invalid
4388 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004389 */
4390#if defined(MBEDTLS_SSL_PROTO_DTLS)
4391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4392 {
4393 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4394
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004395 /* Check epoch (and sequence number) with DTLS */
4396 if( rec_epoch != ssl->in_epoch )
4397 {
4398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4399 "expected %d, received %d",
4400 ssl->in_epoch, rec_epoch ) );
4401
4402#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4403 /*
4404 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4405 * access the first byte of record content (handshake type), as we
4406 * have an active transform (possibly iv_len != 0), so use the
4407 * fact that the record header len is 13 instead.
4408 */
4409 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4410 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4411 rec_epoch == 0 &&
4412 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4413 ssl->in_left > 13 &&
4414 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4415 {
4416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4417 "from the same port" ) );
4418 return( ssl_handle_possible_reconnect( ssl ) );
4419 }
4420 else
4421#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004422 {
4423 /* Consider buffering the record. */
4424 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4425 {
4426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4427 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4428 }
4429
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004430 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004431 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004432 }
4433
4434#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4435 /* Replay detection only works for the current epoch */
4436 if( rec_epoch == ssl->in_epoch &&
4437 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4438 {
4439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4440 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4441 }
4442#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004443
Hanno Becker52c6dc62017-05-26 16:07:36 +01004444 /* Drop unexpected ApplicationData records,
4445 * except at the beginning of renegotiations */
4446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4447 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4448#if defined(MBEDTLS_SSL_RENEGOTIATION)
4449 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4450 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4451#endif
4452 )
4453 {
4454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4455 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4456 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004457 }
4458#endif /* MBEDTLS_SSL_PROTO_DTLS */
4459
Hanno Becker52c6dc62017-05-26 16:07:36 +01004460
4461 /* Check length against bounds of the current transform and version */
4462 if( ssl->transform_in == NULL )
4463 {
4464 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004465 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004466 {
4467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4468 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4469 }
4470 }
4471 else
4472 {
4473 if( ssl->in_msglen < ssl->transform_in->minlen )
4474 {
4475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4476 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4477 }
4478
4479#if defined(MBEDTLS_SSL_PROTO_SSL3)
4480 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004481 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004482 {
4483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4484 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4485 }
4486#endif
4487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4488 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4489 /*
4490 * TLS encrypted messages can have up to 256 bytes of padding
4491 */
4492 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4493 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004494 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004495 {
4496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4497 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4498 }
4499#endif
4500 }
4501
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004502 return( 0 );
4503}
Paul Bakker5121ce52009-01-03 21:22:43 +00004504
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004505/*
4506 * If applicable, decrypt (and decompress) record content
4507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004509{
4510 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4513 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4516 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 ret = mbedtls_ssl_hw_record_read( ssl );
4521 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4524 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004525 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004526
4527 if( ret == 0 )
4528 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004531 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 {
4533 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 return( ret );
4537 }
4538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 ssl->in_msg, ssl->in_msglen );
4541
Angus Grattond8213d02016-05-25 20:56:48 +10004542 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 }
4547 }
4548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004550 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004552 {
4553 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004556 return( ret );
4557 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004565 }
4566#endif
4567
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004568 return( 0 );
4569}
4570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004572
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004573/*
4574 * Read a record.
4575 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004576 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4577 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4578 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004579 */
Hanno Becker1097b342018-08-15 14:09:41 +01004580
4581/* Helper functions for mbedtls_ssl_read_record(). */
4582static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004583static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4584static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004585
Hanno Becker327c93b2018-08-15 13:56:18 +01004586int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004587 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588{
4589 int ret;
4590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004593 if( ssl->keep_current_message == 0 )
4594 {
4595 do {
Simon Butcher99000142016-10-13 17:21:01 +01004596
Hanno Becker26994592018-08-15 14:14:59 +01004597 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004598 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004599 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004600
Hanno Beckere74d5562018-08-15 14:26:08 +01004601 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004602 {
Hanno Becker40f50842018-08-15 14:48:01 +01004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
4604 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004605
Hanno Becker40f50842018-08-15 14:48:01 +01004606 /* We only check for buffered messages if the
4607 * current datagram is fully consumed. */
4608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004609 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004610 {
Hanno Becker40f50842018-08-15 14:48:01 +01004611 if( ssl_load_buffered_message( ssl ) == 0 )
4612 have_buffered = 1;
4613 }
4614
4615 if( have_buffered == 0 )
4616#endif /* MBEDTLS_SSL_PROTO_DTLS */
4617 {
4618 ret = ssl_get_next_record( ssl );
4619 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4620 continue;
4621
4622 if( ret != 0 )
4623 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004624 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004625 return( ret );
4626 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004627 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004628 }
4629
4630 ret = mbedtls_ssl_handle_message_type( ssl );
4631
Hanno Becker40f50842018-08-15 14:48:01 +01004632#if defined(MBEDTLS_SSL_PROTO_DTLS)
4633 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4634 {
4635 /* Buffer future message */
4636 ret = ssl_buffer_message( ssl );
4637 if( ret != 0 )
4638 return( ret );
4639
4640 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4641 }
4642#endif /* MBEDTLS_SSL_PROTO_DTLS */
4643
Hanno Becker90333da2017-10-10 11:27:13 +01004644 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4645 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004646
4647 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004648 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004649 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004650 return( ret );
4651 }
4652
Hanno Becker327c93b2018-08-15 13:56:18 +01004653 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004654 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004655 {
4656 mbedtls_ssl_update_handshake_status( ssl );
4657 }
Simon Butcher99000142016-10-13 17:21:01 +01004658 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004659 else
Simon Butcher99000142016-10-13 17:21:01 +01004660 {
Hanno Becker02f59072018-08-15 14:00:24 +01004661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004662 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004663 }
4664
4665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4666
4667 return( 0 );
4668}
4669
Hanno Becker40f50842018-08-15 14:48:01 +01004670#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004671static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004672{
Hanno Becker40f50842018-08-15 14:48:01 +01004673 if( ssl->in_left > ssl->next_record_offset )
4674 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004675
Hanno Becker40f50842018-08-15 14:48:01 +01004676 return( 0 );
4677}
4678
4679static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4680{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004681 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004682 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004683 int ret = 0;
4684
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004685 if( hs == NULL )
4686 return( -1 );
4687
Hanno Beckere00ae372018-08-20 09:39:42 +01004688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4689
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004690 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4691 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4692 {
4693 /* Check if we have seen a ChangeCipherSpec before.
4694 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004695 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004696 {
4697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4698 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004699 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004700 }
4701
Hanno Becker39b8bc92018-08-28 17:17:13 +01004702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004703 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4704 ssl->in_msglen = 1;
4705 ssl->in_msg[0] = 1;
4706
4707 /* As long as they are equal, the exact value doesn't matter. */
4708 ssl->in_left = 0;
4709 ssl->next_record_offset = 0;
4710
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004711 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004712 goto exit;
4713 }
Hanno Becker37f95322018-08-16 13:55:32 +01004714
Hanno Beckerb8f50142018-08-28 10:01:34 +01004715#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004716 /* Debug only */
4717 {
4718 unsigned offset;
4719 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4720 {
4721 hs_buf = &hs->buffering.hs[offset];
4722 if( hs_buf->is_valid == 1 )
4723 {
4724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4725 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004726 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004727 }
4728 }
4729 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004730#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004731
4732 /* Check if we have buffered and/or fully reassembled the
4733 * next handshake message. */
4734 hs_buf = &hs->buffering.hs[0];
4735 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4736 {
4737 /* Synthesize a record containing the buffered HS message. */
4738 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4739 ( hs_buf->data[2] << 8 ) |
4740 hs_buf->data[3];
4741
4742 /* Double-check that we haven't accidentally buffered
4743 * a message that doesn't fit into the input buffer. */
4744 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4745 {
4746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4748 }
4749
4750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4751 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4752 hs_buf->data, msg_len + 12 );
4753
4754 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4755 ssl->in_hslen = msg_len + 12;
4756 ssl->in_msglen = msg_len + 12;
4757 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4758
4759 ret = 0;
4760 goto exit;
4761 }
4762 else
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4765 hs->in_msg_seq ) );
4766 }
4767
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004768 ret = -1;
4769
4770exit:
4771
4772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4773 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004774}
4775
Hanno Beckera02b0b42018-08-21 17:20:27 +01004776static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4777 size_t desired )
4778{
4779 int offset;
4780 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4782 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004783
Hanno Becker01315ea2018-08-21 17:22:17 +01004784 /* Get rid of future records epoch first, if such exist. */
4785 ssl_free_buffered_record( ssl );
4786
4787 /* Check if we have enough space available now. */
4788 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4789 hs->buffering.total_bytes_buffered ) )
4790 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004792 return( 0 );
4793 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004794
Hanno Becker4f432ad2018-08-28 10:02:32 +01004795 /* We don't have enough space to buffer the next expected handshake
4796 * message. Remove buffers used for future messages to gain space,
4797 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004798 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4799 offset >= 0; offset-- )
4800 {
4801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4802 offset ) );
4803
Hanno Beckerb309b922018-08-23 13:18:05 +01004804 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004805
4806 /* Check if we have enough space available now. */
4807 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4808 hs->buffering.total_bytes_buffered ) )
4809 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004811 return( 0 );
4812 }
4813 }
4814
4815 return( -1 );
4816}
4817
Hanno Becker40f50842018-08-15 14:48:01 +01004818static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4819{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004820 int ret = 0;
4821 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4822
4823 if( hs == NULL )
4824 return( 0 );
4825
4826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4827
4828 switch( ssl->in_msgtype )
4829 {
4830 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004832
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004833 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004834 break;
4835
4836 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004837 {
4838 unsigned recv_msg_seq_offset;
4839 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4840 mbedtls_ssl_hs_buffer *hs_buf;
4841 size_t msg_len = ssl->in_hslen - 12;
4842
4843 /* We should never receive an old handshake
4844 * message - double-check nonetheless. */
4845 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4846 {
4847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4849 }
4850
4851 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4852 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4853 {
4854 /* Silently ignore -- message too far in the future */
4855 MBEDTLS_SSL_DEBUG_MSG( 2,
4856 ( "Ignore future HS message with sequence number %u, "
4857 "buffering window %u - %u",
4858 recv_msg_seq, ssl->handshake->in_msg_seq,
4859 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4860
4861 goto exit;
4862 }
4863
4864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4865 recv_msg_seq, recv_msg_seq_offset ) );
4866
4867 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4868
4869 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004870 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004871 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004872 size_t reassembly_buf_sz;
4873
Hanno Becker37f95322018-08-16 13:55:32 +01004874 hs_buf->is_fragmented =
4875 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4876
4877 /* We copy the message back into the input buffer
4878 * after reassembly, so check that it's not too large.
4879 * This is an implementation-specific limitation
4880 * and not one from the standard, hence it is not
4881 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004882 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004883 {
4884 /* Ignore message */
4885 goto exit;
4886 }
4887
Hanno Beckere0b150f2018-08-21 15:51:03 +01004888 /* Check if we have enough space to buffer the message. */
4889 if( hs->buffering.total_bytes_buffered >
4890 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4891 {
4892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4894 }
4895
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004896 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4897 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004898
4899 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4900 hs->buffering.total_bytes_buffered ) )
4901 {
4902 if( recv_msg_seq_offset > 0 )
4903 {
4904 /* If we can't buffer a future message because
4905 * of space limitations -- ignore. */
4906 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",
4907 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4908 (unsigned) hs->buffering.total_bytes_buffered ) );
4909 goto exit;
4910 }
Hanno Beckere1801392018-08-21 16:51:05 +01004911 else
4912 {
4913 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",
4914 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4915 (unsigned) hs->buffering.total_bytes_buffered ) );
4916 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004917
Hanno Beckera02b0b42018-08-21 17:20:27 +01004918 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004919 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004920 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",
4921 (unsigned) msg_len,
4922 (unsigned) reassembly_buf_sz,
4923 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004924 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004925 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4926 goto exit;
4927 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004928 }
4929
4930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4931 msg_len ) );
4932
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004933 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4934 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004935 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004936 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004937 goto exit;
4938 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004939 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004940
4941 /* Prepare final header: copy msg_type, length and message_seq,
4942 * then add standardised fragment_offset and fragment_length */
4943 memcpy( hs_buf->data, ssl->in_msg, 6 );
4944 memset( hs_buf->data + 6, 0, 3 );
4945 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4946
4947 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004948
4949 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004950 }
4951 else
4952 {
4953 /* Make sure msg_type and length are consistent */
4954 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4957 /* Ignore */
4958 goto exit;
4959 }
4960 }
4961
Hanno Becker4422bbb2018-08-20 09:40:19 +01004962 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004963 {
4964 size_t frag_len, frag_off;
4965 unsigned char * const msg = hs_buf->data + 12;
4966
4967 /*
4968 * Check and copy current fragment
4969 */
4970
4971 /* Validation of header fields already done in
4972 * mbedtls_ssl_prepare_handshake_record(). */
4973 frag_off = ssl_get_hs_frag_off( ssl );
4974 frag_len = ssl_get_hs_frag_len( ssl );
4975
4976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4977 frag_off, frag_len ) );
4978 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4979
4980 if( hs_buf->is_fragmented )
4981 {
4982 unsigned char * const bitmask = msg + msg_len;
4983 ssl_bitmask_set( bitmask, frag_off, frag_len );
4984 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4985 msg_len ) == 0 );
4986 }
4987 else
4988 {
4989 hs_buf->is_complete = 1;
4990 }
4991
4992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4993 hs_buf->is_complete ? "" : "not yet " ) );
4994 }
4995
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004996 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004997 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004998
4999 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005000 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005001 break;
5002 }
5003
5004exit:
5005
5006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5007 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005008}
5009#endif /* MBEDTLS_SSL_PROTO_DTLS */
5010
Hanno Becker1097b342018-08-15 14:09:41 +01005011static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005012{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005013 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005014 * Consume last content-layer message and potentially
5015 * update in_msglen which keeps track of the contents'
5016 * consumption state.
5017 *
5018 * (1) Handshake messages:
5019 * Remove last handshake message, move content
5020 * and adapt in_msglen.
5021 *
5022 * (2) Alert messages:
5023 * Consume whole record content, in_msglen = 0.
5024 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005025 * (3) Change cipher spec:
5026 * Consume whole record content, in_msglen = 0.
5027 *
5028 * (4) Application data:
5029 * Don't do anything - the record layer provides
5030 * the application data as a stream transport
5031 * and consumes through mbedtls_ssl_read only.
5032 *
5033 */
5034
5035 /* Case (1): Handshake messages */
5036 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005038 /* Hard assertion to be sure that no application data
5039 * is in flight, as corrupting ssl->in_msglen during
5040 * ssl->in_offt != NULL is fatal. */
5041 if( ssl->in_offt != NULL )
5042 {
5043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5045 }
5046
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005047 /*
5048 * Get next Handshake message in the current record
5049 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005050
Hanno Becker4a810fb2017-05-24 16:27:30 +01005051 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005052 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005053 * current handshake content: If DTLS handshake
5054 * fragmentation is used, that's the fragment
5055 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005056 * size here is faulty and should be changed at
5057 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005058 * (2) While it doesn't seem to cause problems, one
5059 * has to be very careful not to assume that in_hslen
5060 * is always <= in_msglen in a sensible communication.
5061 * Again, it's wrong for DTLS handshake fragmentation.
5062 * The following check is therefore mandatory, and
5063 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005064 * Additionally, ssl->in_hslen might be arbitrarily out of
5065 * bounds after handling a DTLS message with an unexpected
5066 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005067 */
5068 if( ssl->in_hslen < ssl->in_msglen )
5069 {
5070 ssl->in_msglen -= ssl->in_hslen;
5071 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5072 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005073
Hanno Becker4a810fb2017-05-24 16:27:30 +01005074 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5075 ssl->in_msg, ssl->in_msglen );
5076 }
5077 else
5078 {
5079 ssl->in_msglen = 0;
5080 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005081
Hanno Becker4a810fb2017-05-24 16:27:30 +01005082 ssl->in_hslen = 0;
5083 }
5084 /* Case (4): Application data */
5085 else if( ssl->in_offt != NULL )
5086 {
5087 return( 0 );
5088 }
5089 /* Everything else (CCS & Alerts) */
5090 else
5091 {
5092 ssl->in_msglen = 0;
5093 }
5094
Hanno Becker1097b342018-08-15 14:09:41 +01005095 return( 0 );
5096}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005097
Hanno Beckere74d5562018-08-15 14:26:08 +01005098static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5099{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005100 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005101 return( 1 );
5102
5103 return( 0 );
5104}
5105
Hanno Becker5f066e72018-08-16 14:56:31 +01005106#if defined(MBEDTLS_SSL_PROTO_DTLS)
5107
5108static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5109{
5110 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5111 if( hs == NULL )
5112 return;
5113
Hanno Becker01315ea2018-08-21 17:22:17 +01005114 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005115 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005116 hs->buffering.total_bytes_buffered -=
5117 hs->buffering.future_record.len;
5118
5119 mbedtls_free( hs->buffering.future_record.data );
5120 hs->buffering.future_record.data = NULL;
5121 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005122}
5123
5124static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5125{
5126 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5127 unsigned char * rec;
5128 size_t rec_len;
5129 unsigned rec_epoch;
5130
5131 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5132 return( 0 );
5133
5134 if( hs == NULL )
5135 return( 0 );
5136
Hanno Becker5f066e72018-08-16 14:56:31 +01005137 rec = hs->buffering.future_record.data;
5138 rec_len = hs->buffering.future_record.len;
5139 rec_epoch = hs->buffering.future_record.epoch;
5140
5141 if( rec == NULL )
5142 return( 0 );
5143
Hanno Becker4cb782d2018-08-20 11:19:05 +01005144 /* Only consider loading future records if the
5145 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005146 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005147 return( 0 );
5148
Hanno Becker5f066e72018-08-16 14:56:31 +01005149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5150
5151 if( rec_epoch != ssl->in_epoch )
5152 {
5153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5154 goto exit;
5155 }
5156
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5158
5159 /* Double-check that the record is not too large */
5160 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5161 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5165 }
5166
5167 memcpy( ssl->in_hdr, rec, rec_len );
5168 ssl->in_left = rec_len;
5169 ssl->next_record_offset = 0;
5170
5171 ssl_free_buffered_record( ssl );
5172
5173exit:
5174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5175 return( 0 );
5176}
5177
5178static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5179{
5180 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5181 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005182 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005183
5184 /* Don't buffer future records outside handshakes. */
5185 if( hs == NULL )
5186 return( 0 );
5187
5188 /* Only buffer handshake records (we are only interested
5189 * in Finished messages). */
5190 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5191 return( 0 );
5192
5193 /* Don't buffer more than one future epoch record. */
5194 if( hs->buffering.future_record.data != NULL )
5195 return( 0 );
5196
Hanno Becker01315ea2018-08-21 17:22:17 +01005197 /* Don't buffer record if there's not enough buffering space remaining. */
5198 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5199 hs->buffering.total_bytes_buffered ) )
5200 {
5201 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",
5202 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5203 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005204 return( 0 );
5205 }
5206
Hanno Becker5f066e72018-08-16 14:56:31 +01005207 /* Buffer record */
5208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5209 ssl->in_epoch + 1 ) );
5210 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5211 rec_hdr_len + ssl->in_msglen );
5212
5213 /* ssl_parse_record_header() only considers records
5214 * of the next epoch as candidates for buffering. */
5215 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005216 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005217
5218 hs->buffering.future_record.data =
5219 mbedtls_calloc( 1, hs->buffering.future_record.len );
5220 if( hs->buffering.future_record.data == NULL )
5221 {
5222 /* If we run out of RAM trying to buffer a
5223 * record from the next epoch, just ignore. */
5224 return( 0 );
5225 }
5226
Hanno Becker01315ea2018-08-21 17:22:17 +01005227 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005228
Hanno Becker01315ea2018-08-21 17:22:17 +01005229 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005230 return( 0 );
5231}
5232
5233#endif /* MBEDTLS_SSL_PROTO_DTLS */
5234
Hanno Beckere74d5562018-08-15 14:26:08 +01005235static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005236{
5237 int ret;
5238
Hanno Becker5f066e72018-08-16 14:56:31 +01005239#if defined(MBEDTLS_SSL_PROTO_DTLS)
5240 /* We might have buffered a future record; if so,
5241 * and if the epoch matches now, load it.
5242 * On success, this call will set ssl->in_left to
5243 * the length of the buffered record, so that
5244 * the calls to ssl_fetch_input() below will
5245 * essentially be no-ops. */
5246 ret = ssl_load_buffered_record( ssl );
5247 if( ret != 0 )
5248 return( ret );
5249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005254 return( ret );
5255 }
5256
5257 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5261 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005262 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005263 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5264 {
5265 ret = ssl_buffer_future_record( ssl );
5266 if( ret != 0 )
5267 return( ret );
5268
5269 /* Fall through to handling of unexpected records */
5270 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5271 }
5272
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005273 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5274 {
5275 /* Skip unexpected record (but not whole datagram) */
5276 ssl->next_record_offset = ssl->in_msglen
5277 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005278
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5280 "(header)" ) );
5281 }
5282 else
5283 {
5284 /* Skip invalid record and the rest of the datagram */
5285 ssl->next_record_offset = 0;
5286 ssl->in_left = 0;
5287
5288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5289 "(header)" ) );
5290 }
5291
5292 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005293 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005294 }
5295#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005296 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005297 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005298
5299 /*
5300 * Read and optionally decrypt the message contents
5301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005302 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5303 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005306 return( ret );
5307 }
5308
5309 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005314 if( ssl->next_record_offset < ssl->in_left )
5315 {
5316 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5317 }
5318 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005319 else
5320#endif
5321 ssl->in_left = 0;
5322
5323 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005327 {
5328 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5330 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005331 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005332 /* Except when waiting for Finished as a bad mac here
5333 * probably means something went wrong in the handshake
5334 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5335 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5336 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5337 {
5338#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5339 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5340 {
5341 mbedtls_ssl_send_alert_message( ssl,
5342 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5343 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5344 }
5345#endif
5346 return( ret );
5347 }
5348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005350 if( ssl->conf->badmac_limit != 0 &&
5351 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5354 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005355 }
5356#endif
5357
Hanno Becker4a810fb2017-05-24 16:27:30 +01005358 /* As above, invalid records cause
5359 * dismissal of the whole datagram. */
5360
5361 ssl->next_record_offset = 0;
5362 ssl->in_left = 0;
5363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005365 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005366 }
5367
5368 return( ret );
5369 }
5370 else
5371#endif
5372 {
5373 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5375 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 mbedtls_ssl_send_alert_message( ssl,
5378 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5379 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005380 }
5381#endif
5382 return( ret );
5383 }
5384 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005385
Simon Butcher99000142016-10-13 17:21:01 +01005386 return( 0 );
5387}
5388
5389int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5390{
5391 int ret;
5392
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005393 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005394 * Handle particular types of records
5395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005397 {
Simon Butcher99000142016-10-13 17:21:01 +01005398 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5399 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005400 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005402 }
5403
Hanno Beckere678eaa2018-08-21 14:57:46 +01005404 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005406 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005407 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5409 ssl->in_msglen ) );
5410 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005411 }
5412
Hanno Beckere678eaa2018-08-21 14:57:46 +01005413 if( ssl->in_msg[0] != 1 )
5414 {
5415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5416 ssl->in_msg[0] ) );
5417 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5418 }
5419
5420#if defined(MBEDTLS_SSL_PROTO_DTLS)
5421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5422 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5423 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5424 {
5425 if( ssl->handshake == NULL )
5426 {
5427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5428 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5429 }
5430
5431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5432 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5433 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005434#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005435 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005439 if( ssl->in_msglen != 2 )
5440 {
5441 /* Note: Standard allows for more than one 2 byte alert
5442 to be packed in a single message, but Mbed TLS doesn't
5443 currently support this. */
5444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5445 ssl->in_msglen ) );
5446 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5447 }
5448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 ssl->in_msg[0], ssl->in_msg[1] ) );
5451
5452 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005453 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005458 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 }
5461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5463 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5466 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005468
5469#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5470 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5471 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5472 {
Hanno Becker90333da2017-10-10 11:27:13 +01005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005474 /* Will be handled when trying to parse ServerHello */
5475 return( 0 );
5476 }
5477#endif
5478
5479#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5480 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5481 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5482 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5483 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5484 {
5485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5486 /* Will be handled in mbedtls_ssl_parse_certificate() */
5487 return( 0 );
5488 }
5489#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5490
5491 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005492 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 }
5494
Hanno Beckerc76c6192017-06-06 10:03:17 +01005495#if defined(MBEDTLS_SSL_PROTO_DTLS)
5496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5497 ssl->handshake != NULL &&
5498 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5499 {
5500 ssl_handshake_wrapup_free_hs_transform( ssl );
5501 }
5502#endif
5503
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 return( 0 );
5505}
5506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005508{
5509 int ret;
5510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5512 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5513 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005514 {
5515 return( ret );
5516 }
5517
5518 return( 0 );
5519}
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005522 unsigned char level,
5523 unsigned char message )
5524{
5525 int ret;
5526
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005527 if( ssl == NULL || ssl->conf == NULL )
5528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005534 ssl->out_msglen = 2;
5535 ssl->out_msg[0] = level;
5536 ssl->out_msg[1] = message;
5537
Hanno Becker67bc7c32018-08-06 11:33:50 +01005538 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005541 return( ret );
5542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005544
5545 return( 0 );
5546}
5547
Paul Bakker5121ce52009-01-03 21:22:43 +00005548/*
5549 * Handshake functions
5550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5552 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5553 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5554 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5555 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5556 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5557 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005558/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5566 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005567 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5568 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005571 ssl->state++;
5572 return( 0 );
5573 }
5574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005577}
5578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5586 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005587 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5588 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005591 ssl->state++;
5592 return( 0 );
5593 }
5594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005597}
Gilles Peskinef9828522017-05-03 12:28:43 +02005598
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005600/* Some certificate support -> implement write and parse */
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005605 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 const mbedtls_x509_crt *crt;
5607 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5612 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005613 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5614 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617 ssl->state++;
5618 return( 0 );
5619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 {
5624 if( ssl->client_auth == 0 )
5625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005627 ssl->state++;
5628 return( 0 );
5629 }
5630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 /*
5633 * If using SSLv3 and got no cert, send an Alert message
5634 * (otherwise an empty Certificate message will be sent).
5635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5637 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
5639 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5641 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5642 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645 goto write_msg;
5646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#endif /* MBEDTLS_SSL_CLI_C */
5650#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005651 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5656 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
5658 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
5663 /*
5664 * 0 . 0 handshake type
5665 * 1 . 3 handshake length
5666 * 4 . 6 length of all certs
5667 * 7 . 9 length of cert. 1
5668 * 10 . n-1 peer certificate
5669 * n . n+2 length of cert. 2
5670 * n+3 . ... upper level cert, etc.
5671 */
5672 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005674
Paul Bakker29087132010-03-21 21:03:34 +00005675 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 {
5677 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005678 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005681 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 }
5684
5685 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5686 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5687 ssl->out_msg[i + 2] = (unsigned char)( n );
5688
5689 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5690 i += n; crt = crt->next;
5691 }
5692
5693 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5694 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5695 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5696
5697 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5699 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005700
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005701#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005702write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005703#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005704
5705 ssl->state++;
5706
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005707 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005708 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005710 return( ret );
5711 }
5712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714
Paul Bakkered27a042013-04-18 22:46:23 +02005715 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716}
5717
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005718/*
5719 * Once the certificate message is read, parse it into a cert chain and
5720 * perform basic checks, but leave actual verification to the caller
5721 */
5722static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005723{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005724 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005725 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005726 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_SRV_C)
5729#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005730 /*
5731 * Check if the client sent an empty certificate
5732 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005736 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5738 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5739 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005742
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005743 /* The client was asked for a certificate but didn't send
5744 one. The client should know what's going on, so we
5745 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005746 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005747 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 }
5749 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5753 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005754 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5758 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5759 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5760 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005763
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005764 /* The client was asked for a certificate but didn't send
5765 one. The client should know what's going on, so we
5766 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005767 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005768 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005769 }
5770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5772 MBEDTLS_SSL_PROTO_TLS1_2 */
5773#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005778 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5779 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005781 }
5782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5784 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 }
5791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005793
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005797 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005798
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005799 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005803 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5804 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 }
5807
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005808 /* In case we tried to reuse a session but it failed */
5809 if( ssl->session_negotiate->peer_cert != NULL )
5810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5812 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005813 }
5814
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005815 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005820 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5821 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 }
5824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005826
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005827 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005828
5829 while( i < ssl->in_hslen )
5830 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005831 if ( i + 3 > ssl->in_hslen ) {
5832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5834 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5835 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 if( ssl->in_msg[i] != 0 )
5838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005840 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5841 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 }
5844
5845 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5846 | (unsigned int) ssl->in_msg[i + 2];
5847 i += 3;
5848
5849 if( n < 128 || i + n > ssl->in_hslen )
5850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5853 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 }
5856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005858 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005859 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005861 case 0: /*ok*/
5862 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5863 /* Ignore certificate with an unknown algorithm: maybe a
5864 prior certificate was already trusted. */
5865 break;
5866
5867 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5868 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5869 goto crt_parse_der_failed;
5870
5871 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5872 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5873 goto crt_parse_der_failed;
5874
5875 default:
5876 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5877 crt_parse_der_failed:
5878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880 return( ret );
5881 }
5882
5883 i += n;
5884 }
5885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005887
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005888 /*
5889 * On client, make sure the server cert doesn't change during renego to
5890 * avoid "triple handshake" attack: https://secure-resumption.com/
5891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005895 {
5896 if( ssl->session->peer_cert == NULL )
5897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5900 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005902 }
5903
5904 if( ssl->session->peer_cert->raw.len !=
5905 ssl->session_negotiate->peer_cert->raw.len ||
5906 memcmp( ssl->session->peer_cert->raw.p,
5907 ssl->session_negotiate->peer_cert->raw.p,
5908 ssl->session->peer_cert->raw.len ) != 0 )
5909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5912 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005914 }
5915 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005917
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005918 return( 0 );
5919}
5920
5921int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5922{
5923 int ret;
5924 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5925 ssl->transform_negotiate->ciphersuite_info;
5926#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5927 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5928 ? ssl->handshake->sni_authmode
5929 : ssl->conf->authmode;
5930#else
5931 const int authmode = ssl->conf->authmode;
5932#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005933 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005934
5935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5936
5937 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5938 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5939 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5940 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5941 {
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5943 ssl->state++;
5944 return( 0 );
5945 }
5946
5947#if defined(MBEDTLS_SSL_SRV_C)
5948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5949 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5950 {
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5952 ssl->state++;
5953 return( 0 );
5954 }
5955
5956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5957 authmode == MBEDTLS_SSL_VERIFY_NONE )
5958 {
5959 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005961
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005962 ssl->state++;
5963 return( 0 );
5964 }
5965#endif
5966
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005967#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5968 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005969 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005970 {
5971 goto crt_verify;
5972 }
5973#endif
5974
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005975 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005976 {
5977 /* mbedtls_ssl_read_record may have sent an alert already. We
5978 let it decide whether to alert. */
5979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5980 return( ret );
5981 }
5982
5983 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5984 {
5985#if defined(MBEDTLS_SSL_SRV_C)
5986 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5987 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5988 {
5989 ret = 0;
5990 }
5991#endif
5992
5993 ssl->state++;
5994 return( ret );
5995 }
5996
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005997#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5998 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005999 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006000
6001crt_verify:
6002 if( ssl->handshake->ecrs_enabled)
6003 rs_ctx = &ssl->handshake->ecrs_ctx;
6004#endif
6005
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006006 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006007 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006008 mbedtls_x509_crt *ca_chain;
6009 mbedtls_x509_crl *ca_crl;
6010
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006011#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006012 if( ssl->handshake->sni_ca_chain != NULL )
6013 {
6014 ca_chain = ssl->handshake->sni_ca_chain;
6015 ca_crl = ssl->handshake->sni_ca_crl;
6016 }
6017 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006018#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006019 {
6020 ca_chain = ssl->conf->ca_chain;
6021 ca_crl = ssl->conf->ca_crl;
6022 }
6023
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006024 /*
6025 * Main check: verify certificate
6026 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006027 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006028 ssl->session_negotiate->peer_cert,
6029 ca_chain, ca_crl,
6030 ssl->conf->cert_profile,
6031 ssl->hostname,
6032 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006033 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034
6035 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006038 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006039
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006040#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6041 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006042 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006043#endif
6044
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006045 /*
6046 * Secondary checks: always done, but change 'ret' only if it was 0
6047 */
6048
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006049#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006052
6053 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006055 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006056 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006057 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006060 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006062 }
6063 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006064#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006067 ciphersuite_info,
6068 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006069 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006072 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006074 }
6075
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006076 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6077 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6078 * with details encoded in the verification flags. All other kinds
6079 * of error codes, including those from the user provided f_vrfy
6080 * functions, are treated as fatal and lead to a failure of
6081 * ssl_parse_certificate even if verification was optional. */
6082 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6083 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6084 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6085 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006087 }
6088
6089 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6090 {
6091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6092 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6093 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006094
6095 if( ret != 0 )
6096 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006097 uint8_t alert;
6098
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006099 /* The certificate may have been rejected for several reasons.
6100 Pick one and send the corresponding alert. Which alert to send
6101 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006102 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6103 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6104 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6105 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6106 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6107 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6108 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6109 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6110 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6111 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6112 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6113 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6114 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6115 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6116 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6117 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6118 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6119 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6120 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6121 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006122 else
6123 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6125 alert );
6126 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006127
Hanno Beckere6706e62017-05-15 16:05:15 +01006128#if defined(MBEDTLS_DEBUG_C)
6129 if( ssl->session_negotiate->verify_result != 0 )
6130 {
6131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6132 ssl->session_negotiate->verify_result ) );
6133 }
6134 else
6135 {
6136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6137 }
6138#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 }
6140
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006141 ssl->state++;
6142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006144
6145 return( ret );
6146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6148 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6149 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6150 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6151 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6152 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6153 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006156{
6157 int ret;
6158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 ssl->out_msglen = 1;
6163 ssl->out_msg[0] = 1;
6164
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 ssl->state++;
6166
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006167 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170 return( ret );
6171 }
6172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006174
6175 return( 0 );
6176}
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006179{
6180 int ret;
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183
Hanno Becker327c93b2018-08-15 13:56:18 +01006184 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 return( ret );
6188 }
6189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006193 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6194 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 }
6197
Hanno Beckere678eaa2018-08-21 14:57:46 +01006198 /* CCS records are only accepted if they have length 1 and content '1',
6199 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006200
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006201 /*
6202 * Switch to our negotiated transform and session parameters for inbound
6203 * data.
6204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006206 ssl->transform_in = ssl->transform_negotiate;
6207 ssl->session_in = ssl->session_negotiate;
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006213 ssl_dtls_replay_reset( ssl );
6214#endif
6215
6216 /* Increment epoch */
6217 if( ++ssl->in_epoch == 0 )
6218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006220 /* This is highly unlikely to happen for legitimate reasons, so
6221 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006223 }
6224 }
6225 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006227 memset( ssl->in_ctr, 0, 8 );
6228
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006229 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6232 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006240 }
6241 }
6242#endif
6243
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 ssl->state++;
6245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247
6248 return( 0 );
6249}
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6252 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006253{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006254 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6257 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6258 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006259 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006260 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6263#if defined(MBEDTLS_SHA512_C)
6264 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006265 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6266 else
6267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268#if defined(MBEDTLS_SHA256_C)
6269 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006270 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006271 else
6272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006276 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006277 }
Paul Bakker380da532012-04-18 16:10:25 +00006278}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6283 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006284 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6285 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6288#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006289 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006292 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006295}
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006298 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6301 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006302 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6303 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6306#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006307 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006310 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006313}
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6316 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6317static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006318 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006319{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006320 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6321 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006322}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006323#endif
Paul Bakker380da532012-04-18 16:10:25 +00006324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6326#if defined(MBEDTLS_SHA256_C)
6327static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006328 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006329{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006330 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006331}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006332#endif
Paul Bakker380da532012-04-18 16:10:25 +00006333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#if defined(MBEDTLS_SHA512_C)
6335static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006336 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006337{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006338 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006339}
Paul Bakker769075d2012-11-24 11:26:46 +01006340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006344static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006346{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006347 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006348 mbedtls_md5_context md5;
6349 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 unsigned char padbuf[48];
6352 unsigned char md5sum[16];
6353 unsigned char sha1sum[20];
6354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006356 if( !session )
6357 session = ssl->session;
6358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006360
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006361 mbedtls_md5_init( &md5 );
6362 mbedtls_sha1_init( &sha1 );
6363
6364 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6365 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366
6367 /*
6368 * SSLv3:
6369 * hash =
6370 * MD5( master + pad2 +
6371 * MD5( handshake + sender + master + pad1 ) )
6372 * + SHA1( master + pad2 +
6373 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006374 */
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006377 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6378 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006382 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6383 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006384#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006387 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006388
Paul Bakker1ef83d62012-04-11 12:09:53 +00006389 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006390
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006391 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6392 mbedtls_md5_update_ret( &md5, session->master, 48 );
6393 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6394 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006395
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006396 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6397 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6398 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6399 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
Paul Bakker1ef83d62012-04-11 12:09:53 +00006401 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006402
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006403 mbedtls_md5_starts_ret( &md5 );
6404 mbedtls_md5_update_ret( &md5, session->master, 48 );
6405 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6406 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6407 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006408
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006409 mbedtls_sha1_starts_ret( &sha1 );
6410 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6411 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6412 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6413 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006417 mbedtls_md5_free( &md5 );
6418 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006420 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6421 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6422 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006429static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006431{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006432 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006433 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006434 mbedtls_md5_context md5;
6435 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006436 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006439 if( !session )
6440 session = ssl->session;
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006443
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006444 mbedtls_md5_init( &md5 );
6445 mbedtls_sha1_init( &sha1 );
6446
6447 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6448 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006449
Paul Bakker1ef83d62012-04-11 12:09:53 +00006450 /*
6451 * TLSv1:
6452 * hash = PRF( master, finished_label,
6453 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6454 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006457 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6458 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006459#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006462 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6463 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006464#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006467 ? "client finished"
6468 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006470 mbedtls_md5_finish_ret( &md5, padbuf );
6471 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006472
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006473 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006474 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006477
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006478 mbedtls_md5_free( &md5 );
6479 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006481 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6488#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006489static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006491{
6492 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006493 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006494 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006495 unsigned char padbuf[32];
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006498 if( !session )
6499 session = ssl->session;
6500
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006501 mbedtls_sha256_init( &sha256 );
6502
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006504
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006505 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006506
6507 /*
6508 * TLSv1.2:
6509 * hash = PRF( master, finished_label,
6510 * Hash( handshake ) )[0.11]
6511 */
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#if !defined(MBEDTLS_SHA256_ALT)
6514 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006515 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006516#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006519 ? "client finished"
6520 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006521
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006522 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006523
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006524 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006525 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006528
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006529 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006530
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006531 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if defined(MBEDTLS_SHA512_C)
Rodrigo Dias Correa671600c2020-11-25 00:42:28 -03006538
Paul Bakkerca4ab492012-04-18 14:23:57 +00006539static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006541{
6542 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006543 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006544 mbedtls_sha512_context sha512;
Rodrigo Dias Correa671600c2020-11-25 00:42:28 -03006545 unsigned char padbuf[48];
Paul Bakkerca4ab492012-04-18 14:23:57 +00006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006548 if( !session )
6549 session = ssl->session;
6550
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006551 mbedtls_sha512_init( &sha512 );
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006554
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006555 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006556
6557 /*
6558 * TLSv1.2:
6559 * hash = PRF( master, finished_label,
6560 * Hash( handshake ) )[0.11]
6561 */
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006564 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6565 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006566#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006569 ? "client finished"
6570 : "server finished";
Shawn Carey69c5f5d2021-05-13 09:16:16 -04006571 /* mbedtls_sha512_finish_ret's output parameter is declared as a
6572 * 64-byte buffer, but sice we're using SHA-384, we know that the
6573 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
6574 * about it.
6575 */
6576#if defined(__GNUC__) && __GNUC__ >= 11
6577#pragma GCC diagnostic push
6578#pragma GCC diagnostic ignored "-Wstringop-overflow"
6579#endif
6580 mbedtls_sha512_finish_ret( &sha512, padbuf );
6581#if defined(__GNUC__) && __GNUC__ >= 11
6582#pragma GCC diagnostic pop
6583#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006584
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006585 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006586 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006589
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006590 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006591
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006592 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#endif /* MBEDTLS_SHA512_C */
6597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006602
6603 /*
6604 * Free our handshake params
6605 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006606 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006608 ssl->handshake = NULL;
6609
6610 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006611 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006612 */
6613 if( ssl->transform )
6614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 mbedtls_ssl_transform_free( ssl->transform );
6616 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006617 }
6618 ssl->transform = ssl->transform_negotiate;
6619 ssl->transform_negotiate = NULL;
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006622}
6623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006625{
6626 int resume = ssl->handshake->resume;
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#if defined(MBEDTLS_SSL_RENEGOTIATION)
6631 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006634 ssl->renego_records_seen = 0;
6635 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006636#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006637
6638 /*
6639 * Free the previous session and switch in the current one
6640 */
Paul Bakker0a597072012-09-25 21:55:46 +00006641 if( ssl->session )
6642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006644 /* RFC 7366 3.1: keep the EtM state */
6645 ssl->session_negotiate->encrypt_then_mac =
6646 ssl->session->encrypt_then_mac;
6647#endif
6648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 mbedtls_ssl_session_free( ssl->session );
6650 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006651 }
6652 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006653 ssl->session_negotiate = NULL;
6654
Paul Bakker0a597072012-09-25 21:55:46 +00006655 /*
6656 * Add cache entry
6657 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006658 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006659 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006660 resume == 0 )
6661 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006662 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006664 }
Paul Bakker0a597072012-09-25 21:55:46 +00006665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006668 ssl->handshake->flight != NULL )
6669 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006670 /* Cancel handshake timer */
6671 ssl_set_timer( ssl, 0 );
6672
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006673 /* Keep last flight around in case we need to resend it:
6674 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006676 }
6677 else
6678#endif
6679 ssl_handshake_wrapup_free_hs_transform( ssl );
6680
Paul Bakker48916f92012-09-16 19:57:18 +00006681 ssl->state++;
6682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006684}
6685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006687{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006688 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006691
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006692 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006693
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006694 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006695
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006696 /*
6697 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6698 * may define some other value. Currently (early 2016), no defined
6699 * ciphersuite does this (and this is unlikely to change as activity has
6700 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006705 ssl->verify_data_len = hash_len;
6706 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006707#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006708
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6711 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006712
6713 /*
6714 * In case of session resuming, invert the client and server
6715 * ChangeCipherSpec messages order.
6716 */
Paul Bakker0a597072012-09-25 21:55:46 +00006717 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006724 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006726#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 }
6728 else
6729 ssl->state++;
6730
Paul Bakker48916f92012-09-16 19:57:18 +00006731 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006732 * Switch to our negotiated transform and session parameters for outbound
6733 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006739 {
6740 unsigned char i;
6741
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006742 /* Remember current epoch settings for resending */
6743 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006744 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006745
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006746 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006747 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006748
6749 /* Increment epoch */
6750 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006751 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006752 break;
6753
6754 /* The loop goes to its end iff the counter is wrapping */
6755 if( i == 0 )
6756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6758 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006759 }
6760 }
6761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006763 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006765 ssl->transform_out = ssl->transform_negotiate;
6766 ssl->session_out = ssl->session_negotiate;
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6769 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6774 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006775 }
6776 }
6777#endif
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006782#endif
6783
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006784 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006787 return( ret );
6788 }
6789
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006790#if defined(MBEDTLS_SSL_PROTO_DTLS)
6791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6792 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6793 {
6794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6795 return( ret );
6796 }
6797#endif
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
6801 return( 0 );
6802}
6803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006805#define SSL_MAX_HASH_LEN 36
6806#else
6807#define SSL_MAX_HASH_LEN 12
6808#endif
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811{
Paul Bakker23986e52011-04-24 08:57:21 +00006812 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006813 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006814 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006817
Gilles Peskine7cab4992021-12-13 14:38:40 +01006818 /* There is currently no ciphersuite using another length with TLS 1.2 */
6819#if defined(MBEDTLS_SSL_PROTO_SSL3)
6820 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6821 hash_len = 36;
6822 else
6823#endif
6824 hash_len = 12;
6825
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006826 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Hanno Becker327c93b2018-08-15 13:56:18 +01006828 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006831 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 }
6833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6838 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006839 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6840 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 }
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6844 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6848 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006849 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
6850 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 }
6852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006854 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman459a4612021-06-28 21:58:56 +01006858 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006859 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
6860 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006861 }
6862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006864 ssl->verify_data_len = hash_len;
6865 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006866#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006867
Paul Bakker0a597072012-09-25 21:55:46 +00006868 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006877#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006878 }
6879 else
6880 ssl->state++;
6881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006885#endif
6886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006889exit:
6890 mbedtls_platform_zeroize( buf, hash_len );
6891 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006892}
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6899 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6900 mbedtls_md5_init( &handshake->fin_md5 );
6901 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006902 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6903 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6906#if defined(MBEDTLS_SHA256_C)
6907 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006908 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SHA512_C)
6911 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006912 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006915
6916 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006917
6918#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6919 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6920 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6921#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_DHM_C)
6924 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_ECDH_C)
6927 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006928#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006929#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006930 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006931#if defined(MBEDTLS_SSL_CLI_C)
6932 handshake->ecjpake_cache = NULL;
6933 handshake->ecjpake_cache_len = 0;
6934#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006935#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006936
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006937#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006938 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006939#endif
6940
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006941#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6942 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6943#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006944}
6945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6951 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 mbedtls_md_init( &transform->md_ctx_enc );
6954 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006955}
6956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006960}
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006963{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006964 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006965 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006967 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006969 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006970 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006971
6972 /*
6973 * Either the pointers are now NULL or cleared properly and can be freed.
6974 * Now allocate missing structures.
6975 */
6976 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006977 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006978 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006979 }
Paul Bakker48916f92012-09-16 19:57:18 +00006980
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006981 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006982 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006983 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006984 }
Paul Bakker48916f92012-09-16 19:57:18 +00006985
Paul Bakker82788fb2014-10-20 13:59:19 +02006986 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006987 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006988 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006989 }
Paul Bakker48916f92012-09-16 19:57:18 +00006990
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006991 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006992 if( ssl->handshake == NULL ||
6993 ssl->transform_negotiate == NULL ||
6994 ssl->session_negotiate == NULL )
6995 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 mbedtls_free( ssl->handshake );
6999 mbedtls_free( ssl->transform_negotiate );
7000 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007001
7002 ssl->handshake = NULL;
7003 ssl->transform_negotiate = NULL;
7004 ssl->session_negotiate = NULL;
7005
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007006 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007007 }
7008
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007009 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007011 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007012 ssl_handshake_params_init( ssl->handshake );
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007015 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7016 {
7017 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007018
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007019 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7020 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7021 else
7022 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007023
7024 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007025 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007026#endif
7027
Paul Bakker48916f92012-09-16 19:57:18 +00007028 return( 0 );
7029}
7030
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007031#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007032/* Dummy cookie callbacks for defaults */
7033static int ssl_cookie_write_dummy( void *ctx,
7034 unsigned char **p, unsigned char *end,
7035 const unsigned char *cli_id, size_t cli_id_len )
7036{
7037 ((void) ctx);
7038 ((void) p);
7039 ((void) end);
7040 ((void) cli_id);
7041 ((void) cli_id_len);
7042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007044}
7045
7046static int ssl_cookie_check_dummy( void *ctx,
7047 const unsigned char *cookie, size_t cookie_len,
7048 const unsigned char *cli_id, size_t cli_id_len )
7049{
7050 ((void) ctx);
7051 ((void) cookie);
7052 ((void) cookie_len);
7053 ((void) cli_id);
7054 ((void) cli_id_len);
7055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007057}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007058#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007059
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007060/* Once ssl->out_hdr as the address of the beginning of the
7061 * next outgoing record is set, deduce the other pointers.
7062 *
7063 * Note: For TLS, we save the implicit record sequence number
7064 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7065 * and the caller has to make sure there's space for this.
7066 */
7067
7068static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7069 mbedtls_ssl_transform *transform )
7070{
7071#if defined(MBEDTLS_SSL_PROTO_DTLS)
7072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7073 {
7074 ssl->out_ctr = ssl->out_hdr + 3;
7075 ssl->out_len = ssl->out_hdr + 11;
7076 ssl->out_iv = ssl->out_hdr + 13;
7077 }
7078 else
7079#endif
7080 {
7081 ssl->out_ctr = ssl->out_hdr - 8;
7082 ssl->out_len = ssl->out_hdr + 3;
7083 ssl->out_iv = ssl->out_hdr + 5;
7084 }
7085
7086 /* Adjust out_msg to make space for explicit IV, if used. */
7087 if( transform != NULL &&
7088 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7089 {
7090 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7091 }
7092 else
7093 ssl->out_msg = ssl->out_iv;
7094}
7095
7096/* Once ssl->in_hdr as the address of the beginning of the
7097 * next incoming record is set, deduce the other pointers.
7098 *
7099 * Note: For TLS, we save the implicit record sequence number
7100 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7101 * and the caller has to make sure there's space for this.
7102 */
7103
7104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7105 mbedtls_ssl_transform *transform )
7106{
7107#if defined(MBEDTLS_SSL_PROTO_DTLS)
7108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7109 {
7110 ssl->in_ctr = ssl->in_hdr + 3;
7111 ssl->in_len = ssl->in_hdr + 11;
7112 ssl->in_iv = ssl->in_hdr + 13;
7113 }
7114 else
7115#endif
7116 {
7117 ssl->in_ctr = ssl->in_hdr - 8;
7118 ssl->in_len = ssl->in_hdr + 3;
7119 ssl->in_iv = ssl->in_hdr + 5;
7120 }
7121
7122 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7123 if( transform != NULL &&
7124 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7125 {
7126 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7127 }
7128 else
7129 ssl->in_msg = ssl->in_iv;
7130}
7131
Paul Bakker5121ce52009-01-03 21:22:43 +00007132/*
7133 * Initialize an SSL context
7134 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007135void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7136{
7137 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7138}
7139
7140/*
7141 * Setup an SSL context
7142 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007143
7144static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7145{
7146 /* Set the incoming and outgoing record pointers. */
7147#if defined(MBEDTLS_SSL_PROTO_DTLS)
7148 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7149 {
7150 ssl->out_hdr = ssl->out_buf;
7151 ssl->in_hdr = ssl->in_buf;
7152 }
7153 else
7154#endif /* MBEDTLS_SSL_PROTO_DTLS */
7155 {
7156 ssl->out_hdr = ssl->out_buf + 8;
7157 ssl->in_hdr = ssl->in_buf + 8;
7158 }
7159
7160 /* Derive other internal pointers. */
7161 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7162 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7163}
7164
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007165int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007166 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007167{
Paul Bakker48916f92012-09-16 19:57:18 +00007168 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007169
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007170 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007171
7172 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007173 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007174 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007175
7176 /* Set to NULL in case of an error condition */
7177 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007178
Angus Grattond8213d02016-05-25 20:56:48 +10007179 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7180 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007181 {
Angus Grattond8213d02016-05-25 20:56:48 +10007182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007183 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007184 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007185 }
7186
7187 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7188 if( ssl->out_buf == NULL )
7189 {
7190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007191 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007192 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007193 }
7194
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007195 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007196
Paul Bakker48916f92012-09-16 19:57:18 +00007197 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007198 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007199
7200 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007201
7202error:
7203 mbedtls_free( ssl->in_buf );
7204 mbedtls_free( ssl->out_buf );
7205
7206 ssl->conf = NULL;
7207
7208 ssl->in_buf = NULL;
7209 ssl->out_buf = NULL;
7210
7211 ssl->in_hdr = NULL;
7212 ssl->in_ctr = NULL;
7213 ssl->in_len = NULL;
7214 ssl->in_iv = NULL;
7215 ssl->in_msg = NULL;
7216
7217 ssl->out_hdr = NULL;
7218 ssl->out_ctr = NULL;
7219 ssl->out_len = NULL;
7220 ssl->out_iv = NULL;
7221 ssl->out_msg = NULL;
7222
k-stachowiak9f7798e2018-07-31 16:52:32 +02007223 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007224}
7225
7226/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007227 * Reset an initialized and used SSL context for re-use while retaining
7228 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007229 *
7230 * If partial is non-zero, keep data in the input buffer and client ID.
7231 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007232 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007233static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007234{
Paul Bakker48916f92012-09-16 19:57:18 +00007235 int ret;
7236
Hanno Becker7e772132018-08-10 12:38:21 +01007237#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7238 !defined(MBEDTLS_SSL_SRV_C)
7239 ((void) partial);
7240#endif
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007243
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007244 /* Cancel any possibly running timer */
7245 ssl_set_timer( ssl, 0 );
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_RENEGOTIATION)
7248 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007249 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007250
7251 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7253 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007256
Paul Bakker7eb013f2011-10-06 12:37:39 +00007257 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007258 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007259
7260 ssl->in_msgtype = 0;
7261 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007263 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007264 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007267 ssl_dtls_replay_reset( ssl );
7268#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007269
7270 ssl->in_hslen = 0;
7271 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007272
7273 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007274
7275 ssl->out_msgtype = 0;
7276 ssl->out_msglen = 0;
7277 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7279 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007280 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007281#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007282
Hanno Becker19859472018-08-06 09:40:20 +01007283 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7284
Paul Bakker48916f92012-09-16 19:57:18 +00007285 ssl->transform_in = NULL;
7286 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007287
Hanno Becker78640902018-08-13 16:35:15 +01007288 ssl->session_in = NULL;
7289 ssl->session_out = NULL;
7290
Angus Grattond8213d02016-05-25 20:56:48 +10007291 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007292
7293#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007294 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007295#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7296 {
7297 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007298 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007299 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7302 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7305 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7308 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007309 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007310 }
7311#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007312
Paul Bakker48916f92012-09-16 19:57:18 +00007313 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 mbedtls_ssl_transform_free( ssl->transform );
7316 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007317 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007318 }
Paul Bakker48916f92012-09-16 19:57:18 +00007319
Paul Bakkerc0463502013-02-14 11:19:38 +01007320 if( ssl->session )
7321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322 mbedtls_ssl_session_free( ssl->session );
7323 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007324 ssl->session = NULL;
7325 }
7326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007328 ssl->alpn_chosen = NULL;
7329#endif
7330
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007331#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007332#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007333 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007334#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007335 {
7336 mbedtls_free( ssl->cli_id );
7337 ssl->cli_id = NULL;
7338 ssl->cli_id_len = 0;
7339 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007340#endif
7341
Paul Bakker48916f92012-09-16 19:57:18 +00007342 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7343 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007344
7345 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007346}
7347
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007348/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007349 * Reset an initialized and used SSL context for re-use while retaining
7350 * all application-set variables, function pointers and data.
7351 */
7352int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7353{
7354 return( ssl_session_reset_int( ssl, 0 ) );
7355}
7356
7357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 * SSL set accessors
7359 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007360void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007361{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007362 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007363}
7364
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007365void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007367 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007368}
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007371void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007373 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007374}
7375#endif
7376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007378void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007380 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007381}
7382#endif
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007385
Hanno Becker1841b0a2018-08-24 11:13:57 +01007386void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7387 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007388{
7389 ssl->disable_datagram_packing = !allow_packing;
7390}
7391
7392void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7393 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007395 conf->hs_timeout_min = min;
7396 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007397}
7398#endif
7399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007400void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007402 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007403}
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007406void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007407 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007408 void *p_vrfy )
7409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007410 conf->f_vrfy = f_vrfy;
7411 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007414
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007415void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007416 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007417 void *p_rng )
7418{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007419 conf->f_rng = f_rng;
7420 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007421}
7422
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007423void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007424 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007425 void *p_dbg )
7426{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007427 conf->f_dbg = f_dbg;
7428 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007429}
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007432 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007433 mbedtls_ssl_send_t *f_send,
7434 mbedtls_ssl_recv_t *f_recv,
7435 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007436{
7437 ssl->p_bio = p_bio;
7438 ssl->f_send = f_send;
7439 ssl->f_recv = f_recv;
7440 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007441}
7442
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007443#if defined(MBEDTLS_SSL_PROTO_DTLS)
7444void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7445{
7446 ssl->mtu = mtu;
7447}
7448#endif
7449
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007450void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007451{
7452 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007453}
7454
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007455void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7456 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007457 mbedtls_ssl_set_timer_t *f_set_timer,
7458 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007459{
7460 ssl->p_timer = p_timer;
7461 ssl->f_set_timer = f_set_timer;
7462 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007463
7464 /* Make sure we start with no timer running */
7465 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007466}
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007469void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007470 void *p_cache,
7471 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7472 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007473{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007474 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007475 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007476 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#if defined(MBEDTLS_SSL_CLI_C)
7481int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007482{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007483 int ret;
7484
7485 if( ssl == NULL ||
7486 session == NULL ||
7487 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007488 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007491 }
7492
7493 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7494 return( ret );
7495
Paul Bakker0a597072012-09-25 21:55:46 +00007496 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007497
7498 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007501
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007502void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007503 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007505 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7506 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7507 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7508 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007509}
7510
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007511void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007512 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007513 int major, int minor )
7514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007516 return;
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007519 return;
7520
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007521 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007522}
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007525void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007526 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007527{
7528 conf->cert_profile = profile;
7529}
7530
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007531/* Append a new keycert entry to a (possibly empty) list */
7532static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7533 mbedtls_x509_crt *cert,
7534 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007535{
niisato8ee24222018-06-25 19:05:48 +09007536 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007537
niisato8ee24222018-06-25 19:05:48 +09007538 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7539 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007540 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007541
niisato8ee24222018-06-25 19:05:48 +09007542 new_cert->cert = cert;
7543 new_cert->key = key;
7544 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007545
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007546 /* Update head is the list was null, else add to the end */
7547 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007548 {
niisato8ee24222018-06-25 19:05:48 +09007549 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007550 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007551 else
7552 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007553 mbedtls_ssl_key_cert *cur = *head;
7554 while( cur->next != NULL )
7555 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007556 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007557 }
7558
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007559 return( 0 );
7560}
7561
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007562int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007563 mbedtls_x509_crt *own_cert,
7564 mbedtls_pk_context *pk_key )
7565{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007566 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007567}
7568
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007569void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007570 mbedtls_x509_crt *ca_chain,
7571 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007572{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007573 conf->ca_chain = ca_chain;
7574 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007577
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007578#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7579int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7580 mbedtls_x509_crt *own_cert,
7581 mbedtls_pk_context *pk_key )
7582{
7583 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7584 own_cert, pk_key ) );
7585}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007586
7587void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7588 mbedtls_x509_crt *ca_chain,
7589 mbedtls_x509_crl *ca_crl )
7590{
7591 ssl->handshake->sni_ca_chain = ca_chain;
7592 ssl->handshake->sni_ca_crl = ca_crl;
7593}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007594
7595void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7596 int authmode )
7597{
7598 ssl->handshake->sni_authmode = authmode;
7599}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007600#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7601
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007602#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007603/*
7604 * Set EC J-PAKE password for current handshake
7605 */
7606int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7607 const unsigned char *pw,
7608 size_t pw_len )
7609{
7610 mbedtls_ecjpake_role role;
7611
Janos Follath8eb64132016-06-03 15:40:57 +01007612 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7614
7615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7616 role = MBEDTLS_ECJPAKE_SERVER;
7617 else
7618 role = MBEDTLS_ECJPAKE_CLIENT;
7619
7620 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7621 role,
7622 MBEDTLS_MD_SHA256,
7623 MBEDTLS_ECP_DP_SECP256R1,
7624 pw, pw_len ) );
7625}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007626#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007629int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007630 const unsigned char *psk, size_t psk_len,
7631 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007632{
Paul Bakker6db455e2013-09-18 17:29:31 +02007633 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007638
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007639 /* Identity len will be encoded on two bytes */
7640 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007641 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007642 {
7643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7644 }
7645
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007646 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007647 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007648 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007649
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007650 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007651 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007652 conf->psk_len = 0;
7653 }
7654 if( conf->psk_identity != NULL )
7655 {
7656 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007657 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007658 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007659 }
7660
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007661 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7662 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007663 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007664 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007665 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007666 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007667 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007668 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007669 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007670
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007671 conf->psk_len = psk_len;
7672 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007673
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007674 memcpy( conf->psk, psk, conf->psk_len );
7675 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007676
7677 return( 0 );
7678}
7679
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007680int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7681 const unsigned char *psk, size_t psk_len )
7682{
7683 if( psk == NULL || ssl->handshake == NULL )
7684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7685
7686 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7688
7689 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007690 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007691 mbedtls_platform_zeroize( ssl->handshake->psk,
7692 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007693 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007694 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007695 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007696
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007697 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007698 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007699
7700 ssl->handshake->psk_len = psk_len;
7701 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7702
7703 return( 0 );
7704}
7705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007706void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007708 size_t),
7709 void *p_psk )
7710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007711 conf->f_psk = f_psk;
7712 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007715
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007716#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007717
7718#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007719int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007720{
7721 int ret;
7722
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007723 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7724 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7725 {
7726 mbedtls_mpi_free( &conf->dhm_P );
7727 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007728 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
7731 return( 0 );
7732}
Hanno Becker470a8c42017-10-04 15:28:46 +01007733#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007734
Hanno Beckera90658f2017-10-04 15:29:08 +01007735int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7736 const unsigned char *dhm_P, size_t P_len,
7737 const unsigned char *dhm_G, size_t G_len )
7738{
7739 int ret;
7740
7741 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7742 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7743 {
7744 mbedtls_mpi_free( &conf->dhm_P );
7745 mbedtls_mpi_free( &conf->dhm_G );
7746 return( ret );
7747 }
7748
7749 return( 0 );
7750}
Paul Bakker5121ce52009-01-03 21:22:43 +00007751
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007752int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007753{
7754 int ret;
7755
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007756 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7757 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7758 {
7759 mbedtls_mpi_free( &conf->dhm_P );
7760 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007761 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007762 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007763
7764 return( 0 );
7765}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007766#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007767
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007768#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7769/*
7770 * Set the minimum length for Diffie-Hellman parameters
7771 */
7772void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7773 unsigned int bitlen )
7774{
7775 conf->dhm_min_bitlen = bitlen;
7776}
7777#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7778
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007779#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007780/*
7781 * Set allowed/preferred hashes for handshake signatures
7782 */
7783void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7784 const int *hashes )
7785{
7786 conf->sig_hashes = hashes;
7787}
Hanno Becker947194e2017-04-07 13:25:49 +01007788#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007789
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007790#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007791/*
7792 * Set the allowed elliptic curves
7793 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007794void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007795 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007796{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007797 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007798}
Hanno Becker947194e2017-04-07 13:25:49 +01007799#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007800
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007801#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007803{
Hanno Becker947194e2017-04-07 13:25:49 +01007804 /* Initialize to suppress unnecessary compiler warning */
7805 size_t hostname_len = 0;
7806
7807 /* Check if new hostname is valid before
7808 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007809 if( hostname != NULL )
7810 {
7811 hostname_len = strlen( hostname );
7812
7813 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7815 }
7816
7817 /* Now it's clear that we will overwrite the old hostname,
7818 * so we can free it safely */
7819
7820 if( ssl->hostname != NULL )
7821 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007822 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007823 mbedtls_free( ssl->hostname );
7824 }
7825
7826 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007827
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007829 {
7830 ssl->hostname = NULL;
7831 }
7832 else
7833 {
7834 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007835 if( ssl->hostname == NULL )
7836 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007837
Hanno Becker947194e2017-04-07 13:25:49 +01007838 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007839
Hanno Becker947194e2017-04-07 13:25:49 +01007840 ssl->hostname[hostname_len] = '\0';
7841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
7843 return( 0 );
7844}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007845#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007846
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007847#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007848void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007850 const unsigned char *, size_t),
7851 void *p_sni )
7852{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007853 conf->f_sni = f_sni;
7854 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007859int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007860{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007861 size_t cur_len, tot_len;
7862 const char **p;
7863
7864 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007865 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7866 * MUST NOT be truncated."
7867 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007868 */
7869 tot_len = 0;
7870 for( p = protos; *p != NULL; p++ )
7871 {
7872 cur_len = strlen( *p );
7873 tot_len += cur_len;
7874
Ronald Cron157cffe2020-04-23 16:41:44 +02007875 if( ( cur_len == 0 ) ||
7876 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7877 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007879 }
7880
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007881 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007882
7883 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007884}
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007887{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007888 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007891
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007892void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007893{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007894 conf->max_major_ver = major;
7895 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007896}
7897
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007898void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007899{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007900 conf->min_major_ver = major;
7901 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007905void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007906{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007907 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007908}
7909#endif
7910
Janos Follath088ce432017-04-10 12:42:31 +01007911#if defined(MBEDTLS_SSL_SRV_C)
7912void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7913 char cert_req_ca_list )
7914{
7915 conf->cert_req_ca_list = cert_req_ca_list;
7916}
7917#endif
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007920void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007923}
7924#endif
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007927void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007928{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007929 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007930}
7931#endif
7932
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007933#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007934void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007936 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007937}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007938#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007941int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007944 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007947 }
7948
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007949 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007950
7951 return( 0 );
7952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007956void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007958 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007963void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007964{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007965 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007966}
7967#endif
7968
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007969void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007971 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007972}
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007975void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007977 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007978}
7979
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007980void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007982 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007983}
7984
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007985void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007986 const unsigned char period[8] )
7987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007988 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007993#if defined(MBEDTLS_SSL_CLI_C)
7994void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007995{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007996 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007997}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007998#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007999
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008000#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008001void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8002 mbedtls_ssl_ticket_write_t *f_ticket_write,
8003 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8004 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008005{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008006 conf->f_ticket_write = f_ticket_write;
8007 conf->f_ticket_parse = f_ticket_parse;
8008 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008009}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008010#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008012
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008013#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8014void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8015 mbedtls_ssl_export_keys_t *f_export_keys,
8016 void *p_export_keys )
8017{
8018 conf->f_export_keys = f_export_keys;
8019 conf->p_export_keys = p_export_keys;
8020}
8021#endif
8022
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008023#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008024void mbedtls_ssl_conf_async_private_cb(
8025 mbedtls_ssl_config *conf,
8026 mbedtls_ssl_async_sign_t *f_async_sign,
8027 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8028 mbedtls_ssl_async_resume_t *f_async_resume,
8029 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008030 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008031{
8032 conf->f_async_sign_start = f_async_sign;
8033 conf->f_async_decrypt_start = f_async_decrypt;
8034 conf->f_async_resume = f_async_resume;
8035 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008036 conf->p_async_config_data = async_config_data;
8037}
8038
Gilles Peskine8f97af72018-04-26 11:46:10 +02008039void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8040{
8041 return( conf->p_async_config_data );
8042}
8043
Gilles Peskine1febfef2018-04-30 11:54:39 +02008044void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008045{
8046 if( ssl->handshake == NULL )
8047 return( NULL );
8048 else
8049 return( ssl->handshake->user_async_ctx );
8050}
8051
Gilles Peskine1febfef2018-04-30 11:54:39 +02008052void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008053 void *ctx )
8054{
8055 if( ssl->handshake != NULL )
8056 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008057}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008058#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008059
Paul Bakker5121ce52009-01-03 21:22:43 +00008060/*
8061 * SSL get accessors
8062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008064{
8065 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8066}
8067
Hanno Becker8b170a02017-10-10 11:51:19 +01008068int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8069{
8070 /*
8071 * Case A: We're currently holding back
8072 * a message for further processing.
8073 */
8074
8075 if( ssl->keep_current_message == 1 )
8076 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008078 return( 1 );
8079 }
8080
8081 /*
8082 * Case B: Further records are pending in the current datagram.
8083 */
8084
8085#if defined(MBEDTLS_SSL_PROTO_DTLS)
8086 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8087 ssl->in_left > ssl->next_record_offset )
8088 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008090 return( 1 );
8091 }
8092#endif /* MBEDTLS_SSL_PROTO_DTLS */
8093
8094 /*
8095 * Case C: A handshake message is being processed.
8096 */
8097
Hanno Becker8b170a02017-10-10 11:51:19 +01008098 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8099 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008101 return( 1 );
8102 }
8103
8104 /*
8105 * Case D: An application data message is being processed
8106 */
8107 if( ssl->in_offt != NULL )
8108 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008110 return( 1 );
8111 }
8112
8113 /*
8114 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008115 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008116 * we implement support for multiple alerts in single records.
8117 */
8118
8119 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8120 return( 0 );
8121}
8122
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008123uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008124{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008125 if( ssl->session != NULL )
8126 return( ssl->session->verify_result );
8127
8128 if( ssl->session_negotiate != NULL )
8129 return( ssl->session_negotiate->verify_result );
8130
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008131 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008132}
8133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008135{
Paul Bakker926c8e42013-03-06 10:23:34 +01008136 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008137 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008140}
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008146 {
8147 switch( ssl->minor_ver )
8148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008150 return( "DTLSv1.0" );
8151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008153 return( "DTLSv1.2" );
8154
8155 default:
8156 return( "unknown (DTLS)" );
8157 }
8158 }
8159#endif
8160
Paul Bakker43ca69c2011-01-15 17:35:19 +00008161 switch( ssl->minor_ver )
8162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008164 return( "SSLv3.0" );
8165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008167 return( "TLSv1.0" );
8168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008170 return( "TLSv1.1" );
8171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008173 return( "TLSv1.2" );
8174
Paul Bakker43ca69c2011-01-15 17:35:19 +00008175 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008176 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008177 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008178}
8179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008181{
Hanno Becker3136ede2018-08-17 15:28:19 +01008182 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008184 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008185
Hanno Becker78640902018-08-13 16:35:15 +01008186 if( transform == NULL )
8187 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189#if defined(MBEDTLS_ZLIB_SUPPORT)
8190 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008192#endif
8193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 case MBEDTLS_MODE_GCM:
8197 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008198 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008200 transform_expansion = transform->minlen;
8201 break;
8202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008204
8205 block_size = mbedtls_cipher_get_block_size(
8206 &transform->cipher_ctx_enc );
8207
Hanno Becker3136ede2018-08-17 15:28:19 +01008208 /* Expansion due to the addition of the MAC. */
8209 transform_expansion += transform->maclen;
8210
8211 /* Expansion due to the addition of CBC padding;
8212 * Theoretically up to 256 bytes, but we never use
8213 * more than the block size of the underlying cipher. */
8214 transform_expansion += block_size;
8215
8216 /* For TLS 1.1 or higher, an explicit IV is added
8217 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008218#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8219 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008220 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008221#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008222
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008223 break;
8224
8225 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008228 }
8229
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008230 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008231}
8232
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008233#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8234size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8235{
8236 size_t max_len;
8237
8238 /*
8239 * Assume mfl_code is correct since it was checked when set
8240 */
Angus Grattond8213d02016-05-25 20:56:48 +10008241 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008242
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008243 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008244 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008245 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008246 {
Angus Grattond8213d02016-05-25 20:56:48 +10008247 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008248 }
8249
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008250 /* During a handshake, use the value being negotiated */
8251 if( ssl->session_negotiate != NULL &&
8252 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8253 {
8254 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8255 }
8256
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008257 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008258}
8259#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8260
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008261#if defined(MBEDTLS_SSL_PROTO_DTLS)
8262static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8263{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008264 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8266 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8267 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8268 return ( 0 );
8269
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008270 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8271 return( ssl->mtu );
8272
8273 if( ssl->mtu == 0 )
8274 return( ssl->handshake->mtu );
8275
8276 return( ssl->mtu < ssl->handshake->mtu ?
8277 ssl->mtu : ssl->handshake->mtu );
8278}
8279#endif /* MBEDTLS_SSL_PROTO_DTLS */
8280
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008281int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8282{
8283 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8284
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008285#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8286 !defined(MBEDTLS_SSL_PROTO_DTLS)
8287 (void) ssl;
8288#endif
8289
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008290#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8291 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8292
8293 if( max_len > mfl )
8294 max_len = mfl;
8295#endif
8296
8297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008298 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008299 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008300 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008301 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8302 const size_t overhead = (size_t) ret;
8303
8304 if( ret < 0 )
8305 return( ret );
8306
8307 if( mtu <= overhead )
8308 {
8309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8310 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8311 }
8312
8313 if( max_len > mtu - overhead )
8314 max_len = mtu - overhead;
8315 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008316#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008317
Hanno Becker0defedb2018-08-10 12:35:02 +01008318#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8319 !defined(MBEDTLS_SSL_PROTO_DTLS)
8320 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008321#endif
8322
8323 return( (int) max_len );
8324}
8325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#if defined(MBEDTLS_X509_CRT_PARSE_C)
8327const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008328{
8329 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008330 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008331
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008332 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#if defined(MBEDTLS_SSL_CLI_C)
8337int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008338{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008339 if( ssl == NULL ||
8340 dst == NULL ||
8341 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008342 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008345 }
8346
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008347 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008350
Paul Bakker5121ce52009-01-03 21:22:43 +00008351/*
Paul Bakker1961b702013-01-25 14:49:24 +01008352 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008357
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008358 if( ssl == NULL || ssl->conf == NULL )
8359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008368#endif
8369
Paul Bakker1961b702013-01-25 14:49:24 +01008370 return( ret );
8371}
8372
8373/*
8374 * Perform the SSL handshake
8375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008377{
8378 int ret = 0;
8379
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008380 if( ssl == NULL || ssl->conf == NULL )
8381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008388
8389 if( ret != 0 )
8390 break;
8391 }
8392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008394
8395 return( ret );
8396}
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_SSL_RENEGOTIATION)
8399#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008400/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008401 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008404{
8405 int ret;
8406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008408
8409 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8411 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008412
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008413 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008414 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008416 return( ret );
8417 }
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008420
8421 return( 0 );
8422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008424
8425/*
8426 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 * - any side: calling mbedtls_ssl_renegotiate(),
8428 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8429 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008430 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008431 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008435{
8436 int ret;
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008439
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008440 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8441 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008442
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008443 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8444 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008448 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008449 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008450 ssl->handshake->out_msg_seq = 1;
8451 else
8452 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008453 }
8454#endif
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8457 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008462 return( ret );
8463 }
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008466
8467 return( 0 );
8468}
8469
8470/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008471 * Renegotiate current connection on client,
8472 * or request renegotiation on server
8473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008475{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008477
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008478 if( ssl == NULL || ssl->conf == NULL )
8479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008482 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008489
8490 /* Did we already try/start sending HelloRequest? */
8491 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008493
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008494 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008499 /*
8500 * On client, either start the renegotiation process or,
8501 * if already in progress, continue the handshake
8502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008507
8508 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008511 return( ret );
8512 }
8513 }
8514 else
8515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008519 return( ret );
8520 }
8521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008523
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008524 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008525}
8526
8527/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008528 * Check record counters and renegotiate if they're above the limit.
8529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008531{
Andres AG2196c7f2016-12-15 17:01:16 +00008532 size_t ep_len = ssl_ep_len( ssl );
8533 int in_ctr_cmp;
8534 int out_ctr_cmp;
8535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8537 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008538 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008539 {
8540 return( 0 );
8541 }
8542
Andres AG2196c7f2016-12-15 17:01:16 +00008543 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8544 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008545 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008546 ssl->conf->renego_period + ep_len, 8 - ep_len );
8547
8548 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008549 {
8550 return( 0 );
8551 }
8552
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008557
8558/*
8559 * Receive application data decrypted from the SSL layer
8560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008562{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008563 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008564 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008565
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008566 if( ssl == NULL || ssl->conf == NULL )
8567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008575 return( ret );
8576
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008577 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008579 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008580 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008581 return( ret );
8582 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008583 }
8584#endif
8585
Hanno Becker4a810fb2017-05-24 16:27:30 +01008586 /*
8587 * Check if renegotiation is necessary and/or handshake is
8588 * in process. If yes, perform/continue, and fall through
8589 * if an unexpected packet is received while the client
8590 * is waiting for the ServerHello.
8591 *
8592 * (There is no equivalent to the last condition on
8593 * the server-side as it is not treated as within
8594 * a handshake while waiting for the ClientHello
8595 * after a renegotiation request.)
8596 */
8597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008599 ret = ssl_check_ctr_renegotiate( ssl );
8600 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8601 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008604 return( ret );
8605 }
8606#endif
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008611 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8612 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008615 return( ret );
8616 }
8617 }
8618
Hanno Beckere41158b2017-10-23 13:30:32 +01008619 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008620 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008621 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008622 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008623 if( ssl->f_get_timer != NULL &&
8624 ssl->f_get_timer( ssl->p_timer ) == -1 )
8625 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008626 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008627 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008628
Hanno Becker327c93b2018-08-15 13:56:18 +01008629 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008630 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008631 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8632 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008633
Hanno Becker4a810fb2017-05-24 16:27:30 +01008634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008636 }
8637
8638 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008640 {
8641 /*
8642 * OpenSSL sends empty messages to randomize the IV
8643 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008644 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008647 return( 0 );
8648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008650 return( ret );
8651 }
8652 }
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008657
Hanno Becker4a810fb2017-05-24 16:27:30 +01008658 /*
8659 * - For client-side, expect SERVER_HELLO_REQUEST.
8660 * - For server-side, expect CLIENT_HELLO.
8661 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8662 */
8663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008665 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008667 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008670
8671 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008674 {
8675 continue;
8676 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008679 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008680#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008681
Hanno Becker4a810fb2017-05-24 16:27:30 +01008682#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008683 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008687
8688 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008690 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008691 {
8692 continue;
8693 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008696 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008697#endif /* MBEDTLS_SSL_SRV_C */
8698
Hanno Becker21df7f92017-10-17 11:03:26 +01008699#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008700 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008701 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8702 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8703 ssl->conf->allow_legacy_renegotiation ==
8704 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8705 {
8706 /*
8707 * Accept renegotiation request
8708 */
Paul Bakker48916f92012-09-16 19:57:18 +00008709
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008710 /* DTLS clients need to know renego is server-initiated */
8711#if defined(MBEDTLS_SSL_PROTO_DTLS)
8712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8713 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8714 {
8715 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8716 }
8717#endif
8718 ret = ssl_start_renegotiation( ssl );
8719 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8720 ret != 0 )
8721 {
8722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8723 return( ret );
8724 }
8725 }
8726 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008728 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008729 /*
8730 * Refuse renegotiation
8731 */
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735#if defined(MBEDTLS_SSL_PROTO_SSL3)
8736 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008737 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008738 /* SSLv3 does not have a "no_renegotiation" warning, so
8739 we send a fatal alert and abort the connection. */
8740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8741 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8742 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008743 }
8744 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8746#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8747 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8748 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8751 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8752 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008753 {
8754 return( ret );
8755 }
Paul Bakker48916f92012-09-16 19:57:18 +00008756 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8759 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008763 }
Paul Bakker48916f92012-09-16 19:57:18 +00008764 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008765
Hanno Becker90333da2017-10-10 11:27:13 +01008766 /* At this point, we don't know whether the renegotiation has been
8767 * completed or not. The cases to consider are the following:
8768 * 1) The renegotiation is complete. In this case, no new record
8769 * has been read yet.
8770 * 2) The renegotiation is incomplete because the client received
8771 * an application data record while awaiting the ServerHello.
8772 * 3) The renegotiation is incomplete because the client received
8773 * a non-handshake, non-application data message while awaiting
8774 * the ServerHello.
8775 * In each of these case, looping will be the proper action:
8776 * - For 1), the next iteration will read a new record and check
8777 * if it's application data.
8778 * - For 2), the loop condition isn't satisfied as application data
8779 * is present, hence continue is the same as break
8780 * - For 3), the loop condition is satisfied and read_record
8781 * will re-deliver the message that was held back by the client
8782 * when expecting the ServerHello.
8783 */
8784 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008785 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008786#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008788 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008789 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008790 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008791 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008794 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008796 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008797 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8802 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008805 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008806 }
8807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8811 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008812 }
8813
8814 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008815
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008816 /* We're going to return something now, cancel timer,
8817 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008819 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008820
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008822 /* If we requested renego but received AppData, resend HelloRequest.
8823 * Do it now, after setting in_offt, to avoid taking this branch
8824 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008828 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008829 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008832 return( ret );
8833 }
8834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008836#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008837 }
8838
8839 n = ( len < ssl->in_msglen )
8840 ? len : ssl->in_msglen;
8841
8842 memcpy( buf, ssl->in_offt, n );
8843 ssl->in_msglen -= n;
8844
gabor-mezei-arm3eaa1032020-07-15 10:55:00 +02008845 /* Zeroising the plaintext buffer to erase unused application data
8846 from the memory. */
8847 mbedtls_platform_zeroize( ssl->in_offt, n );
8848
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008850 {
8851 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008853 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008855 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008856 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008857 /* more data available */
8858 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008862
Paul Bakker23986e52011-04-24 08:57:21 +00008863 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008864}
8865
8866/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008867 * Send application data to be encrypted by the SSL layer, taking care of max
8868 * fragment length and buffer size.
8869 *
8870 * According to RFC 5246 Section 6.2.1:
8871 *
8872 * Zero-length fragments of Application data MAY be sent as they are
8873 * potentially useful as a traffic analysis countermeasure.
8874 *
8875 * Therefore, it is possible that the input message length is 0 and the
8876 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008877 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008878static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008879 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008880{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008881 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8882 const size_t max_len = (size_t) ret;
8883
8884 if( ret < 0 )
8885 {
8886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8887 return( ret );
8888 }
8889
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008890 if( len > max_len )
8891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008893 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008896 "maximum fragment length: %d > %d",
8897 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008899 }
8900 else
8901#endif
8902 len = max_len;
8903 }
Paul Bakker887bd502011-06-08 13:10:54 +00008904
Paul Bakker5121ce52009-01-03 21:22:43 +00008905 if( ssl->out_left != 0 )
8906 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008907 /*
8908 * The user has previously tried to send the data and
8909 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8910 * written. In this case, we expect the high-level write function
8911 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008916 return( ret );
8917 }
8918 }
Paul Bakker887bd502011-06-08 13:10:54 +00008919 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008920 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008921 /*
8922 * The user is trying to send a message the first time, so we need to
8923 * copy the data into the internal buffers and setup the data structure
8924 * to keep track of partial writes
8925 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008926 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008928 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008929
Hanno Becker67bc7c32018-08-06 11:33:50 +01008930 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008933 return( ret );
8934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008935 }
8936
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008937 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008938}
8939
8940/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008941 * Write application data, doing 1/n-1 splitting if necessary.
8942 *
8943 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008944 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008945 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008948static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008949 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008950{
8951 int ret;
8952
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008953 if( ssl->conf->cbc_record_splitting ==
8954 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008955 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8957 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8958 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008959 {
8960 return( ssl_write_real( ssl, buf, len ) );
8961 }
8962
8963 if( ssl->split_done == 0 )
8964 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008965 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008966 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008967 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008968 }
8969
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008970 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8971 return( ret );
8972 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008973
8974 return( ret + 1 );
8975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008977
8978/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008979 * Write application data (public-facing wrapper)
8980 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008981int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008982{
8983 int ret;
8984
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008986
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008987 if( ssl == NULL || ssl->conf == NULL )
8988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8989
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008991 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8992 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008993 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008994 return( ret );
8995 }
8996#endif
8997
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008998 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008999 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009000 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009001 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009003 return( ret );
9004 }
9005 }
9006
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009007#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009008 ret = ssl_write_split( ssl, buf, len );
9009#else
9010 ret = ssl_write_real( ssl, buf, len );
9011#endif
9012
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009014
9015 return( ret );
9016}
9017
9018/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009019 * Notify the peer that the connection is being closed
9020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009022{
9023 int ret;
9024
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009025 if( ssl == NULL || ssl->conf == NULL )
9026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009029
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009030 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9036 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9037 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009040 return( ret );
9041 }
9042 }
9043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009045
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009046 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009047}
9048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009050{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009051 if( transform == NULL )
9052 return;
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009055 deflateEnd( &transform->ctx_deflate );
9056 inflateEnd( &transform->ctx_inflate );
9057#endif
9058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9060 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 mbedtls_md_free( &transform->md_ctx_enc );
9063 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009064
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009065 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009066}
9067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068#if defined(MBEDTLS_X509_CRT_PARSE_C)
9069static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009072
9073 while( cur != NULL )
9074 {
9075 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009077 cur = next;
9078 }
9079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009081
Hanno Becker0271f962018-08-16 13:23:47 +01009082#if defined(MBEDTLS_SSL_PROTO_DTLS)
9083
9084static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9085{
9086 unsigned offset;
9087 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9088
9089 if( hs == NULL )
9090 return;
9091
Hanno Becker283f5ef2018-08-24 09:34:47 +01009092 ssl_free_buffered_record( ssl );
9093
Hanno Becker0271f962018-08-16 13:23:47 +01009094 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009095 ssl_buffering_free_slot( ssl, offset );
9096}
9097
9098static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9099 uint8_t slot )
9100{
9101 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9102 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009103
9104 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9105 return;
9106
Hanno Beckere605b192018-08-21 15:59:07 +01009107 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009108 {
Hanno Beckere605b192018-08-21 15:59:07 +01009109 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009110 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009111 mbedtls_free( hs_buf->data );
9112 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009113 }
9114}
9115
9116#endif /* MBEDTLS_SSL_PROTO_DTLS */
9117
Gilles Peskine9b562d52018-04-25 20:32:43 +02009118void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009119{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009120 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9121
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009122 if( handshake == NULL )
9123 return;
9124
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009125#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9126 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9127 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009128 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009129 handshake->async_in_progress = 0;
9130 }
9131#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9132
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9134 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9135 mbedtls_md5_free( &handshake->fin_md5 );
9136 mbedtls_sha1_free( &handshake->fin_sha1 );
9137#endif
9138#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9139#if defined(MBEDTLS_SHA256_C)
9140 mbedtls_sha256_free( &handshake->fin_sha256 );
9141#endif
9142#if defined(MBEDTLS_SHA512_C)
9143 mbedtls_sha512_free( &handshake->fin_sha512 );
9144#endif
9145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#if defined(MBEDTLS_DHM_C)
9148 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#if defined(MBEDTLS_ECDH_C)
9151 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009152#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009153#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009154 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009155#if defined(MBEDTLS_SSL_CLI_C)
9156 mbedtls_free( handshake->ecjpake_cache );
9157 handshake->ecjpake_cache = NULL;
9158 handshake->ecjpake_cache_len = 0;
9159#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009160#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009161
Janos Follath4ae5c292016-02-10 11:27:43 +00009162#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9163 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009164 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009166#endif
9167
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009168#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9169 if( handshake->psk != NULL )
9170 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009171 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009172 mbedtls_free( handshake->psk );
9173 }
9174#endif
9175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9177 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009178 /*
9179 * Free only the linked list wrapper, not the keys themselves
9180 * since the belong to the SNI callback
9181 */
9182 if( handshake->sni_key_cert != NULL )
9183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009185
9186 while( cur != NULL )
9187 {
9188 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009190 cur = next;
9191 }
9192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009194
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009195#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009196 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009197#endif
9198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199#if defined(MBEDTLS_SSL_PROTO_DTLS)
9200 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009201 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009202 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009203#endif
9204
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009205 mbedtls_platform_zeroize( handshake,
9206 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009207}
9208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009210{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009211 if( session == NULL )
9212 return;
9213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009215 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 mbedtls_x509_crt_free( session->peer_cert );
9218 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009219 }
Paul Bakkered27a042013-04-18 22:46:23 +02009220#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009221
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009222#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009224#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009225
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009226 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009227}
9228
Paul Bakker5121ce52009-01-03 21:22:43 +00009229/*
9230 * Free an SSL context
9231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009233{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009234 if( ssl == NULL )
9235 return;
9236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009238
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009239 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009240 {
Angus Grattond8213d02016-05-25 20:56:48 +10009241 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009243 }
9244
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009245 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009246 {
Angus Grattond8213d02016-05-25 20:56:48 +10009247 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009249 }
9250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009252 if( ssl->compress_buf != NULL )
9253 {
Angus Grattond8213d02016-05-25 20:56:48 +10009254 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009256 }
9257#endif
9258
Paul Bakker48916f92012-09-16 19:57:18 +00009259 if( ssl->transform )
9260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 mbedtls_ssl_transform_free( ssl->transform );
9262 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009263 }
9264
9265 if( ssl->handshake )
9266 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009267 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9269 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 mbedtls_free( ssl->handshake );
9272 mbedtls_free( ssl->transform_negotiate );
9273 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009274 }
9275
Paul Bakkerc0463502013-02-14 11:19:38 +01009276 if( ssl->session )
9277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 mbedtls_ssl_session_free( ssl->session );
9279 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009280 }
9281
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009282#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009283 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009284 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009285 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009287 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9291 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9294 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009295 }
9296#endif
9297
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009298#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009300#endif
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009303
Paul Bakker86f04f42013-02-14 11:20:09 +01009304 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009305 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009306}
9307
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009308/*
9309 * Initialze mbedtls_ssl_config
9310 */
9311void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9312{
9313 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9314}
9315
Simon Butcherc97b6972015-12-27 23:48:17 +00009316#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009317static int ssl_preset_default_hashes[] = {
9318#if defined(MBEDTLS_SHA512_C)
9319 MBEDTLS_MD_SHA512,
9320 MBEDTLS_MD_SHA384,
9321#endif
9322#if defined(MBEDTLS_SHA256_C)
9323 MBEDTLS_MD_SHA256,
9324 MBEDTLS_MD_SHA224,
9325#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009326#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009327 MBEDTLS_MD_SHA1,
9328#endif
9329 MBEDTLS_MD_NONE
9330};
Simon Butcherc97b6972015-12-27 23:48:17 +00009331#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009332
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009333static int ssl_preset_suiteb_ciphersuites[] = {
9334 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9335 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9336 0
9337};
9338
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009339#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009340static int ssl_preset_suiteb_hashes[] = {
9341 MBEDTLS_MD_SHA256,
9342 MBEDTLS_MD_SHA384,
9343 MBEDTLS_MD_NONE
9344};
9345#endif
9346
9347#if defined(MBEDTLS_ECP_C)
9348static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009349#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009350 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009351#endif
9352#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009353 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009354#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009355 MBEDTLS_ECP_DP_NONE
9356};
9357#endif
9358
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009359/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009360 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009361 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009362int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009363 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009364{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009365#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009366 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009367#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009368
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009369 /* Use the functions here so that they are covered in tests,
9370 * but otherwise access member directly for efficiency */
9371 mbedtls_ssl_conf_endpoint( conf, endpoint );
9372 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009373
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009374 /*
9375 * Things that are common to all presets
9376 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009377#if defined(MBEDTLS_SSL_CLI_C)
9378 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9379 {
9380 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9381#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9382 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9383#endif
9384 }
9385#endif
9386
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009387#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009388 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009389#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009390
9391#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9392 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9393#endif
9394
9395#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9396 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9397#endif
9398
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009399#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9400 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9401#endif
9402
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009403#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009404 conf->f_cookie_write = ssl_cookie_write_dummy;
9405 conf->f_cookie_check = ssl_cookie_check_dummy;
9406#endif
9407
9408#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9409 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9410#endif
9411
Janos Follath088ce432017-04-10 12:42:31 +01009412#if defined(MBEDTLS_SSL_SRV_C)
9413 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9414#endif
9415
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009416#if defined(MBEDTLS_SSL_PROTO_DTLS)
9417 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9418 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9419#endif
9420
9421#if defined(MBEDTLS_SSL_RENEGOTIATION)
9422 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009423 memset( conf->renego_period, 0x00, 2 );
9424 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009425#endif
9426
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009427#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9428 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9429 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009430 const unsigned char dhm_p[] =
9431 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9432 const unsigned char dhm_g[] =
9433 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9434
Hanno Beckera90658f2017-10-04 15:29:08 +01009435 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9436 dhm_p, sizeof( dhm_p ),
9437 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009438 {
9439 return( ret );
9440 }
9441 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009442#endif
9443
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009444 /*
9445 * Preset-specific defaults
9446 */
9447 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009448 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009449 /*
9450 * NSA Suite B
9451 */
9452 case MBEDTLS_SSL_PRESET_SUITEB:
9453 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9454 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9455 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9456 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9457
9458 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9459 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9460 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9461 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9462 ssl_preset_suiteb_ciphersuites;
9463
9464#if defined(MBEDTLS_X509_CRT_PARSE_C)
9465 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009466#endif
9467
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009468#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009469 conf->sig_hashes = ssl_preset_suiteb_hashes;
9470#endif
9471
9472#if defined(MBEDTLS_ECP_C)
9473 conf->curve_list = ssl_preset_suiteb_curves;
9474#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009475 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009476
9477 /*
9478 * Default
9479 */
9480 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009481 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9482 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9483 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9484 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9485 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9486 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9487 MBEDTLS_SSL_MIN_MINOR_VERSION :
9488 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009489 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9490 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9491
9492#if defined(MBEDTLS_SSL_PROTO_DTLS)
9493 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9494 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9495#endif
9496
9497 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9498 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9499 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9500 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9501 mbedtls_ssl_list_ciphersuites();
9502
9503#if defined(MBEDTLS_X509_CRT_PARSE_C)
9504 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9505#endif
9506
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009507#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009508 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009509#endif
9510
9511#if defined(MBEDTLS_ECP_C)
9512 conf->curve_list = mbedtls_ecp_grp_id_list();
9513#endif
9514
9515#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9516 conf->dhm_min_bitlen = 1024;
9517#endif
9518 }
9519
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009520 return( 0 );
9521}
9522
9523/*
9524 * Free mbedtls_ssl_config
9525 */
9526void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9527{
9528#if defined(MBEDTLS_DHM_C)
9529 mbedtls_mpi_free( &conf->dhm_P );
9530 mbedtls_mpi_free( &conf->dhm_G );
9531#endif
9532
9533#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9534 if( conf->psk != NULL )
9535 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009536 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009537 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009538 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009539 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009540 }
9541
9542 if( conf->psk_identity != NULL )
9543 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009544 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009545 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009546 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009547 conf->psk_identity_len = 0;
9548 }
9549#endif
9550
9551#if defined(MBEDTLS_X509_CRT_PARSE_C)
9552 ssl_key_cert_free( conf->key_cert );
9553#endif
9554
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009555 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009556}
9557
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009558#if defined(MBEDTLS_PK_C) && \
9559 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009560/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009564{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#if defined(MBEDTLS_RSA_C)
9566 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9567 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_ECDSA_C)
9570 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9571 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009574}
9575
Hanno Becker7e5437a2017-04-28 17:15:26 +01009576unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9577{
9578 switch( type ) {
9579 case MBEDTLS_PK_RSA:
9580 return( MBEDTLS_SSL_SIG_RSA );
9581 case MBEDTLS_PK_ECDSA:
9582 case MBEDTLS_PK_ECKEY:
9583 return( MBEDTLS_SSL_SIG_ECDSA );
9584 default:
9585 return( MBEDTLS_SSL_SIG_ANON );
9586 }
9587}
9588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009590{
9591 switch( sig )
9592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593#if defined(MBEDTLS_RSA_C)
9594 case MBEDTLS_SSL_SIG_RSA:
9595 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#if defined(MBEDTLS_ECDSA_C)
9598 case MBEDTLS_SSL_SIG_ECDSA:
9599 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009600#endif
9601 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009603 }
9604}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009605#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009606
Hanno Becker7e5437a2017-04-28 17:15:26 +01009607#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9608 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9609
9610/* Find an entry in a signature-hash set matching a given hash algorithm. */
9611mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9612 mbedtls_pk_type_t sig_alg )
9613{
9614 switch( sig_alg )
9615 {
9616 case MBEDTLS_PK_RSA:
9617 return( set->rsa );
9618 case MBEDTLS_PK_ECDSA:
9619 return( set->ecdsa );
9620 default:
9621 return( MBEDTLS_MD_NONE );
9622 }
9623}
9624
9625/* Add a signature-hash-pair to a signature-hash set */
9626void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9627 mbedtls_pk_type_t sig_alg,
9628 mbedtls_md_type_t md_alg )
9629{
9630 switch( sig_alg )
9631 {
9632 case MBEDTLS_PK_RSA:
9633 if( set->rsa == MBEDTLS_MD_NONE )
9634 set->rsa = md_alg;
9635 break;
9636
9637 case MBEDTLS_PK_ECDSA:
9638 if( set->ecdsa == MBEDTLS_MD_NONE )
9639 set->ecdsa = md_alg;
9640 break;
9641
9642 default:
9643 break;
9644 }
9645}
9646
9647/* Allow exactly one hash algorithm for each signature. */
9648void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9649 mbedtls_md_type_t md_alg )
9650{
9651 set->rsa = md_alg;
9652 set->ecdsa = md_alg;
9653}
9654
9655#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9656 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9657
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009658/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009659 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009662{
9663 switch( hash )
9664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#if defined(MBEDTLS_MD5_C)
9666 case MBEDTLS_SSL_HASH_MD5:
9667 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SHA1_C)
9670 case MBEDTLS_SSL_HASH_SHA1:
9671 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673#if defined(MBEDTLS_SHA256_C)
9674 case MBEDTLS_SSL_HASH_SHA224:
9675 return( MBEDTLS_MD_SHA224 );
9676 case MBEDTLS_SSL_HASH_SHA256:
9677 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_SHA512_C)
9680 case MBEDTLS_SSL_HASH_SHA384:
9681 return( MBEDTLS_MD_SHA384 );
9682 case MBEDTLS_SSL_HASH_SHA512:
9683 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009684#endif
9685 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009687 }
9688}
9689
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009690/*
9691 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9692 */
9693unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9694{
9695 switch( md )
9696 {
9697#if defined(MBEDTLS_MD5_C)
9698 case MBEDTLS_MD_MD5:
9699 return( MBEDTLS_SSL_HASH_MD5 );
9700#endif
9701#if defined(MBEDTLS_SHA1_C)
9702 case MBEDTLS_MD_SHA1:
9703 return( MBEDTLS_SSL_HASH_SHA1 );
9704#endif
9705#if defined(MBEDTLS_SHA256_C)
9706 case MBEDTLS_MD_SHA224:
9707 return( MBEDTLS_SSL_HASH_SHA224 );
9708 case MBEDTLS_MD_SHA256:
9709 return( MBEDTLS_SSL_HASH_SHA256 );
9710#endif
9711#if defined(MBEDTLS_SHA512_C)
9712 case MBEDTLS_MD_SHA384:
9713 return( MBEDTLS_SSL_HASH_SHA384 );
9714 case MBEDTLS_MD_SHA512:
9715 return( MBEDTLS_SSL_HASH_SHA512 );
9716#endif
9717 default:
9718 return( MBEDTLS_SSL_HASH_NONE );
9719 }
9720}
9721
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009722#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009723/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009724 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009725 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009726 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009727int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009728{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009730
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009731 if( ssl->conf->curve_list == NULL )
9732 return( -1 );
9733
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009734 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009735 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009736 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009737
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009738 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009739}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009740#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009741
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009742#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009743/*
9744 * Check if a hash proposed by the peer is in our list.
9745 * Return 0 if we're willing to use it, -1 otherwise.
9746 */
9747int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9748 mbedtls_md_type_t md )
9749{
9750 const int *cur;
9751
9752 if( ssl->conf->sig_hashes == NULL )
9753 return( -1 );
9754
9755 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9756 if( *cur == (int) md )
9757 return( 0 );
9758
9759 return( -1 );
9760}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009761#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763#if defined(MBEDTLS_X509_CRT_PARSE_C)
9764int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9765 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009766 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009767 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009768{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009769 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009771 int usage = 0;
9772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009774 const char *ext_oid;
9775 size_t ext_len;
9776#endif
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9779 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009780 ((void) cert);
9781 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009782 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009783#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9786 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009787 {
9788 /* Server part of the key exchange */
9789 switch( ciphersuite->key_exchange )
9790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 case MBEDTLS_KEY_EXCHANGE_RSA:
9792 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009793 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009794 break;
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9797 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9798 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9799 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009800 break;
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9803 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009804 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009805 break;
9806
9807 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 case MBEDTLS_KEY_EXCHANGE_NONE:
9809 case MBEDTLS_KEY_EXCHANGE_PSK:
9810 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9811 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009812 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009813 usage = 0;
9814 }
9815 }
9816 else
9817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9819 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009820 }
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009823 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009824 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009825 ret = -1;
9826 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009827#else
9828 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9832 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9835 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009836 }
9837 else
9838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9840 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009841 }
9842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009844 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009845 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009846 ret = -1;
9847 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009849
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009850 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009853
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009854/*
9855 * Convert version numbers to/from wire format
9856 * and, for DTLS, to/from TLS equivalent.
9857 *
9858 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009859 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009860 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9861 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009864 unsigned char ver[2] )
9865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866#if defined(MBEDTLS_SSL_PROTO_DTLS)
9867 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009870 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9871
9872 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9873 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9874 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009875 else
9876#else
9877 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009878#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009879 {
9880 ver[0] = (unsigned char) major;
9881 ver[1] = (unsigned char) minor;
9882 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009883}
9884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009886 const unsigned char ver[2] )
9887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888#if defined(MBEDTLS_SSL_PROTO_DTLS)
9889 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009890 {
9891 *major = 255 - ver[0] + 2;
9892 *minor = 255 - ver[1] + 1;
9893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009895 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9896 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009897 else
9898#else
9899 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009900#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009901 {
9902 *major = ver[0];
9903 *minor = ver[1];
9904 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009905}
9906
Simon Butcher99000142016-10-13 17:21:01 +01009907int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9908{
9909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9910 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9911 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9912
9913 switch( md )
9914 {
9915#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9916#if defined(MBEDTLS_MD5_C)
9917 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009918 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009919#endif
9920#if defined(MBEDTLS_SHA1_C)
9921 case MBEDTLS_SSL_HASH_SHA1:
9922 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9923 break;
9924#endif
9925#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9926#if defined(MBEDTLS_SHA512_C)
9927 case MBEDTLS_SSL_HASH_SHA384:
9928 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9929 break;
9930#endif
9931#if defined(MBEDTLS_SHA256_C)
9932 case MBEDTLS_SSL_HASH_SHA256:
9933 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9934 break;
9935#endif
9936 default:
9937 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9938 }
9939
9940 return 0;
9941#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9942 (void) ssl;
9943 (void) md;
9944
9945 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9947}
9948
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009949#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9950 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9951int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9952 unsigned char *output,
9953 unsigned char *data, size_t data_len )
9954{
9955 int ret = 0;
9956 mbedtls_md5_context mbedtls_md5;
9957 mbedtls_sha1_context mbedtls_sha1;
9958
9959 mbedtls_md5_init( &mbedtls_md5 );
9960 mbedtls_sha1_init( &mbedtls_sha1 );
9961
9962 /*
9963 * digitally-signed struct {
9964 * opaque md5_hash[16];
9965 * opaque sha_hash[20];
9966 * };
9967 *
9968 * md5_hash
9969 * MD5(ClientHello.random + ServerHello.random
9970 * + ServerParams);
9971 * sha_hash
9972 * SHA(ClientHello.random + ServerHello.random
9973 * + ServerParams);
9974 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009975 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009976 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009977 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009978 goto exit;
9979 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009980 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009981 ssl->handshake->randbytes, 64 ) ) != 0 )
9982 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009984 goto exit;
9985 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009986 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009987 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009989 goto exit;
9990 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009991 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009992 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009994 goto exit;
9995 }
9996
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009997 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009998 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010000 goto exit;
10001 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010002 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010003 ssl->handshake->randbytes, 64 ) ) != 0 )
10004 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010006 goto exit;
10007 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010008 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010009 data_len ) ) != 0 )
10010 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010012 goto exit;
10013 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010014 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010015 output + 16 ) ) != 0 )
10016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010018 goto exit;
10019 }
10020
10021exit:
10022 mbedtls_md5_free( &mbedtls_md5 );
10023 mbedtls_sha1_free( &mbedtls_sha1 );
10024
10025 if( ret != 0 )
10026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10027 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10028
10029 return( ret );
10030
10031}
10032#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10033 MBEDTLS_SSL_PROTO_TLS1_1 */
10034
10035#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10036 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10037int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010038 unsigned char *hash, size_t *hashlen,
10039 unsigned char *data, size_t data_len,
10040 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010041{
10042 int ret = 0;
10043 mbedtls_md_context_t ctx;
10044 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010045 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010046
10047 mbedtls_md_init( &ctx );
10048
10049 /*
10050 * digitally-signed struct {
10051 * opaque client_random[32];
10052 * opaque server_random[32];
10053 * ServerDHParams params;
10054 * };
10055 */
10056 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10057 {
10058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10059 goto exit;
10060 }
10061 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10062 {
10063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10064 goto exit;
10065 }
10066 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10067 {
10068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10069 goto exit;
10070 }
10071 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10072 {
10073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10074 goto exit;
10075 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010076 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010077 {
10078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10079 goto exit;
10080 }
10081
10082exit:
10083 mbedtls_md_free( &ctx );
10084
10085 if( ret != 0 )
10086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10087 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10088
10089 return( ret );
10090}
10091#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10092 MBEDTLS_SSL_PROTO_TLS1_2 */
10093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094#endif /* MBEDTLS_SSL_TLS_C */