blob: 1b4c380507043d5d0b5cc15c0777a5ed53212f81 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 if( src->peer_cert != NULL )
306 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200307 int ret;
308
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200309 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200310 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200316 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319 dst->peer_cert = NULL;
320 return( ret );
321 }
322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200325#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326 if( src->ticket != NULL )
327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200329 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200331
332 memcpy( dst->ticket, src->ticket, src->ticket_len );
333 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200334#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 return( 0 );
337}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200338#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 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, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
518static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100519 const unsigned char *secret, size_t slen,
520 const char *label,
521 const unsigned char *random, size_t rlen,
522 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523{
524 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100525 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000526 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
528 const mbedtls_md_info_t *md_info;
529 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100530 int ret;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100538
539 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000541
542 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100543 memcpy( tmp + md_len, label, nb );
544 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000545 nb += rlen;
546
547 /*
548 * Compute P_<hash>(secret, label + random)[0..dlen]
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551 return( ret );
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
554 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
555 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100556
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100557 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_md_hmac_reset ( &md_ctx );
560 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
561 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_md_hmac_reset ( &md_ctx );
564 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
565 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000566
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100567 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
569 for( j = 0; j < k; j++ )
570 dstbuf[i + j] = h_i[j];
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500575 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
576 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000577
578 return( 0 );
579}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582static int tls_prf_sha256( const unsigned char *secret, size_t slen,
583 const char *label,
584 const unsigned char *random, size_t rlen,
585 unsigned char *dstbuf, size_t dlen )
586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588 label, random, rlen, dstbuf, dlen ) );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593static int tls_prf_sha384( const unsigned char *secret, size_t slen,
594 const char *label,
595 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000596 unsigned char *dstbuf, size_t dlen )
597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100599 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_SHA512_C */
602#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
607 defined(MBEDTLS_SSL_PROTO_TLS1_1)
608static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
Paul Bakker380da532012-04-18 16:10:25 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_SSL_PROTO_SSL3)
612static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
613static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200614#endif
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
617static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
618static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#endif
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
622#if defined(MBEDTLS_SHA256_C)
623static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
624static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
625static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SHA512_C)
629static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
630static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
631static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000636{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200637 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 unsigned char keyblk[256];
640 unsigned char *key1;
641 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100642 unsigned char *mac_enc;
643 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000644 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200645 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 const mbedtls_cipher_info_t *cipher_info;
647 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_ssl_session *session = ssl->session_negotiate;
650 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
651 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100656 if( cipher_info == NULL )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100659 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100661 }
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100664 if( md_info == NULL )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100667 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100669 }
670
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_SSL3)
675 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000676 {
Paul Bakker48916f92012-09-16 19:57:18 +0000677 handshake->tls_prf = ssl3_prf;
678 handshake->calc_verify = ssl_calc_verify_ssl;
679 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000680 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200681 else
682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
684 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 {
Paul Bakker48916f92012-09-16 19:57:18 +0000686 handshake->tls_prf = tls1_prf;
687 handshake->calc_verify = ssl_calc_verify_tls;
688 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200690 else
691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
693#if defined(MBEDTLS_SHA512_C)
694 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
695 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000696 {
Paul Bakker48916f92012-09-16 19:57:18 +0000697 handshake->tls_prf = tls_prf_sha384;
698 handshake->calc_verify = ssl_calc_verify_tls_sha384;
699 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000700 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000701 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SHA256_C)
704 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000705 {
Paul Bakker48916f92012-09-16 19:57:18 +0000706 handshake->tls_prf = tls_prf_sha256;
707 handshake->calc_verify = ssl_calc_verify_tls_sha256;
708 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200710 else
711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200716 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000717
718 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 * SSLv3:
720 * master =
721 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
722 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
723 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200724 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 * master = PRF( premaster, "master secret", randbytes )[0..47]
727 */
Paul Bakker0a597072012-09-25 21:55:46 +0000728 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000731 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
734 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200735 {
736 unsigned char session_hash[48];
737 size_t hash_len;
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200740
741 ssl->handshake->calc_verify( ssl, session_hash );
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
744 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200747 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200749 {
750 hash_len = 48;
751 }
752 else
753#endif
754 hash_len = 32;
755 }
756 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200758 hash_len = 36;
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200761
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100762 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
763 "extended master secret",
764 session_hash, hash_len,
765 session->master, 48 );
766 if( ret != 0 )
767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100769 return( ret );
770 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200771
772 }
773 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200774#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100775 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
776 "master secret",
777 handshake->randbytes, 64,
778 session->master, 48 );
779 if( ret != 0 )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100782 return( ret );
783 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200784
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500785 mbedtls_platform_zeroize( handshake->premaster,
786 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 /*
792 * Swap the client and server random values.
793 */
Paul Bakker48916f92012-09-16 19:57:18 +0000794 memcpy( tmp, handshake->randbytes, 64 );
795 memcpy( handshake->randbytes, tmp + 32, 32 );
796 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500797 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
799 /*
800 * SSLv3:
801 * key block =
802 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
803 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
804 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
805 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
806 * ...
807 *
808 * TLSv1:
809 * key block = PRF( master, "key expansion", randbytes )
810 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100811 ret = handshake->tls_prf( session->master, 48, "key expansion",
812 handshake->randbytes, 64, keyblk, 256 );
813 if( ret != 0 )
814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100816 return( ret );
817 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
820 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
822 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
823 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500825 mbedtls_platform_zeroize( handshake->randbytes,
826 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
828 /*
829 * Determine the appropriate key, IV and MAC length.
830 */
Paul Bakker68884e32013-01-07 18:20:04 +0100831
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200832 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200835 cipher_info->mode == MBEDTLS_MODE_CCM ||
836 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200838 size_t taglen, explicit_ivlen;
839
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200840 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000841 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200843 /* All modes haves 96-bit IVs;
844 * GCM and CCM has 4 implicit and 8 explicit bytes
845 * ChachaPoly has all 12 bytes implicit
846 */
Paul Bakker68884e32013-01-07 18:20:04 +0100847 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200848 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
849 transform->fixed_ivlen = 12;
850 else
851 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200853 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
854 taglen = transform->ciphersuite_info->flags &
855 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
856
857
858 /* Minimum length of encrypted record */
859 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
860 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100861 }
862 else
863 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200864 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
866 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200869 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100870 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = mbedtls_md_get_size( md_info );
874 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877 /*
878 * If HMAC is to be truncated, we shall keep the leftmost bytes,
879 * (rfc 6066 page 13 or rfc 2104 section 4),
880 * so we only need to adjust the length here.
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000885
886#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
887 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000888 * HMAC implementation which also truncates the key
889 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000890 mac_key_len = transform->maclen;
891#endif
892 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200894
895 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100896 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200898 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 transform->minlen = transform->maclen;
901 else
Paul Bakker68884e32013-01-07 18:20:04 +0100902 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200903 /*
904 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100905 * 1. if EtM is in use: one block plus MAC
906 * otherwise: * first multiple of blocklen greater than maclen
907 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
910 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100911 {
912 transform->minlen = transform->maclen
913 + cipher_info->block_size;
914 }
915 else
916#endif
917 {
918 transform->minlen = transform->maclen
919 + cipher_info->block_size
920 - transform->maclen % cipher_info->block_size;
921 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
924 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
925 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200926 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100927 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
930 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
931 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200932 {
933 transform->minlen += transform->ivlen;
934 }
935 else
936#endif
937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 }
Paul Bakker68884e32013-01-07 18:20:04 +0100941 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 }
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000945 transform->keylen, transform->minlen, transform->ivlen,
946 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
948 /*
949 * Finally setup the cipher contexts, IVs and MAC secrets.
950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000954 key1 = keyblk + mac_key_len * 2;
955 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Paul Bakker68884e32013-01-07 18:20:04 +0100957 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000958 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000960 /*
961 * This is not used in TLS v1.1.
962 */
Paul Bakker48916f92012-09-16 19:57:18 +0000963 iv_copy_len = ( transform->fixed_ivlen ) ?
964 transform->fixed_ivlen : transform->ivlen;
965 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
966 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000967 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 }
969 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#endif /* MBEDTLS_SSL_CLI_C */
971#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 key1 = keyblk + mac_key_len * 2 + transform->keylen;
975 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Hanno Becker81c7b182017-11-09 18:39:33 +0000977 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000980 /*
981 * This is not used in TLS v1.1.
982 */
Paul Bakker48916f92012-09-16 19:57:18 +0000983 iv_copy_len = ( transform->fixed_ivlen ) ?
984 transform->fixed_ivlen : transform->ivlen;
985 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
986 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000987 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100994 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_PROTO_SSL3)
997 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001003 }
1004
Hanno Becker81c7b182017-11-09 18:39:33 +00001005 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1006 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001007 }
1008 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1010#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1011 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1012 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001013 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001014 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1015 For AEAD-based ciphersuites, there is nothing to do here. */
1016 if( mac_key_len != 0 )
1017 {
1018 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1019 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1020 }
Paul Bakker68884e32013-01-07 18:20:04 +01001021 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001022 else
1023#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001027 }
Paul Bakker68884e32013-01-07 18:20:04 +01001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1030 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001035 transform->iv_enc, transform->iv_dec,
1036 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001037 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001038 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1041 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001042 }
1043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001045
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001046#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1047 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001048 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001049 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1050 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001052 iv_copy_len );
1053 }
1054#endif
1055
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001056 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001057 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001060 return( ret );
1061 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001062
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001063 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001064 cipher_info ) ) != 0 )
1065 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001067 return( ret );
1068 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001071 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075 return( ret );
1076 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001079 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083 return( ret );
1084 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_CIPHER_MODE_CBC)
1087 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1090 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001094 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1097 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 return( ret );
1101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001105 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001108 // Initialize compression
1109 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001111 {
Paul Bakker16770332013-10-11 09:59:44 +02001112 if( ssl->compress_buf == NULL )
1113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001115 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001116 if( ssl->compress_buf == NULL )
1117 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001119 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001120 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001121 }
1122 }
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001125
Paul Bakker48916f92012-09-16 19:57:18 +00001126 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1127 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001128
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001129 if( deflateInit( &transform->ctx_deflate,
1130 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001131 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1134 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001135 }
1136 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 return( 0 );
1142}
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_PROTO_SSL3)
1145void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001147 mbedtls_md5_context md5;
1148 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 unsigned char pad_1[48];
1150 unsigned char pad_2[48];
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001154 mbedtls_md5_init( &md5 );
1155 mbedtls_sha1_init( &sha1 );
1156
1157 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1158 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker380da532012-04-18 16:10:25 +00001160 memset( pad_1, 0x36, 48 );
1161 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001163 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1164 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1165 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001167 mbedtls_md5_starts_ret( &md5 );
1168 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1169 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1170 mbedtls_md5_update_ret( &md5, hash, 16 );
1171 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001173 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1174 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1175 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001176
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001177 mbedtls_sha1_starts_ret( &sha1 );
1178 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1179 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1180 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1181 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001185
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001186 mbedtls_md5_free( &md5 );
1187 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001188
Paul Bakker380da532012-04-18 16:10:25 +00001189 return;
1190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1194void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001195{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001196 mbedtls_md5_context md5;
1197 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001201 mbedtls_md5_init( &md5 );
1202 mbedtls_sha1_init( &sha1 );
1203
1204 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1205 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001206
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001207 mbedtls_md5_finish_ret( &md5, hash );
1208 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001212
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001213 mbedtls_md5_free( &md5 );
1214 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001215
Paul Bakker380da532012-04-18 16:10:25 +00001216 return;
1217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1221#if defined(MBEDTLS_SHA256_C)
1222void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001223{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001224 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001225
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001226 mbedtls_sha256_init( &sha256 );
1227
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001229
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001230 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001231 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001235
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001236 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001237
Paul Bakker380da532012-04-18 16:10:25 +00001238 return;
1239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_SHA512_C)
1243void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001244{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001245 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001246
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001247 mbedtls_sha512_init( &sha512 );
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001250
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001251 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001252 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001257 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 return;
1260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#endif /* MBEDTLS_SHA512_C */
1262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1265int 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 +02001266{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001267 unsigned char *p = ssl->handshake->premaster;
1268 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001269 const unsigned char *psk = ssl->conf->psk;
1270 size_t psk_len = ssl->conf->psk_len;
1271
1272 /* If the psk callback was called, use its result */
1273 if( ssl->handshake->psk != NULL )
1274 {
1275 psk = ssl->handshake->psk;
1276 psk_len = ssl->handshake->psk_len;
1277 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001278
1279 /*
1280 * PMS = struct {
1281 * opaque other_secret<0..2^16-1>;
1282 * opaque psk<0..2^16-1>;
1283 * };
1284 * with "other_secret" depending on the particular key exchange
1285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1287 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001288 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001289 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001292 *(p++) = (unsigned char)( psk_len >> 8 );
1293 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001294
1295 if( end < p || (size_t)( end - p ) < psk_len )
1296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1297
1298 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001299 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001300 }
1301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1303#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1304 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001305 {
1306 /*
1307 * other_secret already set by the ClientKeyExchange message,
1308 * and is 48 bytes long
1309 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001310 if( end - p < 2 )
1311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1312
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001313 *p++ = 0;
1314 *p++ = 48;
1315 p += 48;
1316 }
1317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1319#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1320 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001321 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001322 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001323 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001325 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001327 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001328 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001331 return( ret );
1332 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001333 *(p++) = (unsigned char)( len >> 8 );
1334 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001335 p += len;
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001338 }
1339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1341#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1342 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001343 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001344 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001345 size_t zlen;
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001348 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001349 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001352 return( ret );
1353 }
1354
1355 *(p++) = (unsigned char)( zlen >> 8 );
1356 *(p++) = (unsigned char)( zlen );
1357 p += zlen;
1358
Janos Follath3fbdada2018-08-15 10:26:53 +01001359 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1360 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001361 }
1362 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001367 }
1368
1369 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001370 if( end - p < 2 )
1371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001372
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001373 *(p++) = (unsigned char)( psk_len >> 8 );
1374 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001375
1376 if( end < p || (size_t)( end - p ) < psk_len )
1377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1378
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001379 memcpy( p, psk, psk_len );
1380 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001381
1382 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1383
1384 return( 0 );
1385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001389/*
1390 * SSLv3.0 MAC functions
1391 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001392#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001393static void ssl_mac( mbedtls_md_context_t *md_ctx,
1394 const unsigned char *secret,
1395 const unsigned char *buf, size_t len,
1396 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001397 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398{
1399 unsigned char header[11];
1400 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001401 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1403 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001404
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001405 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001407 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001408 else
Paul Bakker68884e32013-01-07 18:20:04 +01001409 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
1411 memcpy( header, ctr, 8 );
1412 header[ 8] = (unsigned char) type;
1413 header[ 9] = (unsigned char)( len >> 8 );
1414 header[10] = (unsigned char)( len );
1415
Paul Bakker68884e32013-01-07 18:20:04 +01001416 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_md_starts( md_ctx );
1418 mbedtls_md_update( md_ctx, secret, md_size );
1419 mbedtls_md_update( md_ctx, padding, padlen );
1420 mbedtls_md_update( md_ctx, header, 11 );
1421 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001422 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Paul Bakker68884e32013-01-07 18:20:04 +01001424 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_md_starts( md_ctx );
1426 mbedtls_md_update( md_ctx, secret, md_size );
1427 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001428 mbedtls_md_update( md_ctx, out, md_size );
1429 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001434 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001435#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001436#endif
1437
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001438/* The function below is only used in the Lucky 13 counter-measure in
1439 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1440#if defined(SSL_SOME_MODES_USE_MAC) && \
1441 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1442 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1443 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1444/* This function makes sure every byte in the memory region is accessed
1445 * (in ascending addresses order) */
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001446static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001447{
1448 unsigned char acc = 0;
1449 volatile unsigned char force;
1450
1451 for( ; len != 0; p++, len-- )
1452 acc ^= *p;
1453
1454 force = acc;
1455 (void) force;
1456}
1457#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1458
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001459/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001469 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001478 ssl->out_msg, ssl->out_msglen );
1479
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001481 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001483#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( mode == MBEDTLS_MODE_STREAM ||
1485 ( mode == MBEDTLS_MODE_CBC
1486#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1487 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001488#endif
1489 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_SSL_PROTO_SSL3)
1492 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001493 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001494 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001495
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001496 ssl_mac( &ssl->transform_out->md_ctx_enc,
1497 ssl->transform_out->mac_enc,
1498 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001499 ssl->out_ctr, ssl->out_msgtype,
1500 mac );
1501
1502 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001503 }
1504 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1507 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1508 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001509 {
Hanno Becker992b6872017-11-09 18:57:39 +00001510 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1513 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1514 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001516 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001517 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001519
1520 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001521 }
1522 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001527 }
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001530 ssl->out_msg + ssl->out_msglen,
1531 ssl->transform_out->maclen );
1532
1533 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001534 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001535 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001536#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001538 /*
1539 * Encrypt
1540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1542 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001544 int ret;
1545 size_t olen = 0;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 "including %d bytes of padding",
1549 ssl->out_msglen, 0 ) );
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001552 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001553 ssl->transform_out->ivlen,
1554 ssl->out_msg, ssl->out_msglen,
1555 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558 return( ret );
1559 }
1560
1561 if( ssl->out_msglen != olen )
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Paul Bakker68884e32013-01-07 18:20:04 +01001567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001569#if defined(MBEDTLS_GCM_C) || \
1570 defined(MBEDTLS_CCM_C) || \
1571 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001573 mode == MBEDTLS_MODE_CCM ||
1574 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001575 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001576 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001577 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001578 unsigned char *enc_msg;
1579 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 unsigned char iv[12];
1581 mbedtls_ssl_transform *transform = ssl->transform_out;
1582 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001584 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 /*
1587 * Prepare additional authenticated data
1588 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001589 memcpy( add_data, ssl->out_ctr, 8 );
1590 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001592 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001593 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1594 add_data[12] = ssl->out_msglen & 0xFF;
1595
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001596 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001597
Paul Bakker68884e32013-01-07 18:20:04 +01001598 /*
1599 * Generate IV
1600 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001601 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1602 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001603 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001604 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1605 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1606 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1607
1608 }
1609 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1610 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001611 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001612 unsigned char i;
1613
1614 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1615
1616 for( i = 0; i < 8; i++ )
1617 iv[i+4] ^= ssl->out_ctr[i];
1618 }
1619 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001620 {
1621 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001624 }
1625
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001626 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1627 iv, transform->ivlen );
1628 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1629 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001630
Paul Bakker68884e32013-01-07 18:20:04 +01001631 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001632 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001633 */
1634 enc_msg = ssl->out_msg;
1635 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001636 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001639 "including 0 bytes of padding",
1640 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001641
Paul Bakker68884e32013-01-07 18:20:04 +01001642 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001643 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001644 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001645 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1646 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001647 add_data, 13,
1648 enc_msg, enc_msglen,
1649 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001650 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001653 return( ret );
1654 }
1655
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001656 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001660 }
1661
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001662 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001663 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001669#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001672 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001673 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001674 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675
Paul Bakker48916f92012-09-16 19:57:18 +00001676 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1677 ssl->transform_out->ivlen;
1678 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 padlen = 0;
1680
1681 for( i = 0; i <= padlen; i++ )
1682 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1683
1684 ssl->out_msglen += padlen + 1;
1685
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001686 enc_msglen = ssl->out_msglen;
1687 enc_msg = ssl->out_msg;
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001690 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001691 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1692 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 {
1696 /*
1697 * Generate IV
1698 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001699 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001700 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001701 if( ret != 0 )
1702 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001703
Paul Bakker92be97b2013-01-02 17:30:03 +01001704 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001705 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001706
1707 /*
1708 * Fix pointer positions and message length with added IV
1709 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001710 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001711 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001712 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001718 ssl->out_msglen, ssl->transform_out->ivlen,
1719 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001722 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001723 ssl->transform_out->ivlen,
1724 enc_msg, enc_msglen,
1725 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001728 return( ret );
1729 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001730
Paul Bakkercca5b812013-08-31 17:40:26 +02001731 if( enc_msglen != olen )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001735 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1738 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001739 {
1740 /*
1741 * Save IV in SSL3 and TLS1
1742 */
1743 memcpy( ssl->transform_out->iv_enc,
1744 ssl->transform_out->cipher_ctx_enc.iv,
1745 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001747#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001752 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1753
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001754 /*
1755 * MAC(MAC_write_key, seq_num +
1756 * TLSCipherText.type +
1757 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001758 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759 * IV + // except for TLS 1.0
1760 * ENC(content + padding + padding_length));
1761 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 unsigned char pseudo_hdr[13];
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1767 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001768 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1769 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1774 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001775 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001776 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001778
Hanno Becker3d8c9072018-01-05 16:24:22 +00001779 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1780 ssl->transform_out->maclen );
1781
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001783 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001787 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001788#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794 /* Make extra sure authentication was performed, exactly once */
1795 if( auth_done != 1 )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
1803 return( 0 );
1804}
1805
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001806#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001807/*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001808 * Constant-flow conditional memcpy:
1809 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1810 * - otherwise, a no-op,
1811 * but with execution flow independent of the values of c1 and c2.
1812 *
1813 * Use only bit operations to avoid branches that could be used by some
1814 * compilers on some platforms to translate comparison operators.
1815 */
Manuel Pégourié-Gonnard94fd8dc2020-07-28 11:56:05 +02001816static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1817 const unsigned char *src,
1818 size_t len,
1819 size_t c1, size_t c2 )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001820{
1821 /* diff = 0 if c1 == c2, non-zero otherwise */
1822 const size_t diff = c1 ^ c2;
1823
1824 /* MSVC has a warning about unary minus on unsigned integer types,
1825 * but this is well-defined and precisely what we want to do here. */
1826#if defined(_MSC_VER)
1827#pragma warning( push )
1828#pragma warning( disable : 4146 )
1829#endif
1830
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001831 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001832 const size_t diff_msb = ( diff | -diff );
1833
1834 /* diff1 = c1 != c2 */
1835 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1836
1837 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001838 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001839
1840#if defined(_MSC_VER)
1841#pragma warning( pop )
1842#endif
1843
1844 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnard253b0de2020-07-29 10:04:36 +02001845 size_t i;
1846 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001847 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1848}
1849
1850/*
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001851 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnarde1b1fff2020-07-29 12:54:04 +02001852 *
1853 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1854 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001855 */
1856int mbedtls_ssl_cf_hmac(
1857 mbedtls_md_context_t *ctx,
1858 const unsigned char *add_data, size_t add_data_len,
1859 const unsigned char *data, size_t data_len_secret,
1860 size_t min_data_len, size_t max_data_len,
1861 unsigned char *output )
1862{
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001863 /*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001864 * This function breaks the HMAC abstraction and uses the md_clone()
1865 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001866 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001867 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001868 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001869 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001870 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001871 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1872 * minlen, then cloning the context, and for each byte up to maxlen
1873 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001874 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001875 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001876 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001877 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001878 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1879 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001880 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnarda6c13172020-07-28 11:45:02 +02001881 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001882 const unsigned char * const okey = ikey + block_size;
1883 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001884
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001885 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1886 mbedtls_md_context_t aux;
1887 size_t offset;
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001888 int ret;
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001889
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001890 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001891
1892#define MD_CHK( func_call ) \
1893 do { \
1894 ret = (func_call); \
1895 if( ret != 0 ) \
1896 goto cleanup; \
1897 } while( 0 )
1898
1899 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001900
1901 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1902 * so we can start directly with the message */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001903 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1904 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001905
1906 /* For each possible length, compute the hash up to that point */
1907 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001908 {
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001909 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1910 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001911 /* Keep only the correct inner_hash in the output buffer */
1912 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1913 offset, data_len_secret );
1914
1915 if( offset < max_data_len )
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001916 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001917 }
1918
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001919 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001920 MD_CHK( mbedtls_md_starts( ctx ) );
1921 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1922 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1923 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001924
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001925 /* Done, get ready for next time */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001926 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001927
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001928#undef MD_CHK
1929
1930cleanup:
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001931 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001932 return( ret );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001933}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001934#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001939 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001940#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001941 size_t padlen = 0, correct = 1;
1942#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001946 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950 }
1951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001953
Paul Bakker48916f92012-09-16 19:57:18 +00001954 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001957 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 }
1960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1962 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001963 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001964 int ret;
1965 size_t olen = 0;
1966
Paul Bakker68884e32013-01-07 18:20:04 +01001967 padlen = 0;
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001970 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001971 ssl->transform_in->ivlen,
1972 ssl->in_msg, ssl->in_msglen,
1973 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001976 return( ret );
1977 }
1978
1979 if( ssl->in_msglen != olen )
1980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 }
Paul Bakker68884e32013-01-07 18:20:04 +01001985 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001987#if defined(MBEDTLS_GCM_C) || \
1988 defined(MBEDTLS_CCM_C) || \
1989 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001991 mode == MBEDTLS_MODE_CCM ||
1992 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001993 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001994 int ret;
1995 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001996 unsigned char *dec_msg;
1997 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001998 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001999 unsigned char iv[12];
2000 mbedtls_ssl_transform *transform = ssl->transform_in;
2001 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002003 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002004
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002005 /*
2006 * Compute and update sizes
2007 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002008 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002011 "+ taglen (%d)", ssl->in_msglen,
2012 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002014 }
2015 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2016
Paul Bakker68884e32013-01-07 18:20:04 +01002017 dec_msg = ssl->in_msg;
2018 dec_msg_result = ssl->in_msg;
2019 ssl->in_msglen = dec_msglen;
2020
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 /*
2022 * Prepare additional authenticated data
2023 */
Paul Bakker68884e32013-01-07 18:20:04 +01002024 memcpy( add_data, ssl->in_ctr, 8 );
2025 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002027 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002028 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2029 add_data[12] = ssl->in_msglen & 0xFF;
2030
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002031 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002032
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002033 /*
2034 * Prepare IV
2035 */
2036 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2037 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002038 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2040 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002041
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 }
2043 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2044 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002045 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 unsigned char i;
2047
2048 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2049
2050 for( i = 0; i < 8; i++ )
2051 iv[i+4] ^= ssl->in_ctr[i];
2052 }
2053 else
2054 {
2055 /* Reminder if we ever add an AEAD mode with a different size */
2056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2058 }
2059
2060 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002062
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002063 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002064 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002068 add_data, 13,
2069 dec_msg, dec_msglen,
2070 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002071 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2076 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002077
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002078 return( ret );
2079 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002080 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002081
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002082 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002086 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002090#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 {
Paul Bakker45829992013-01-03 14:52:21 +01002093 /*
2094 * Decrypt and check the padding
2095 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002096 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002097 unsigned char *dec_msg;
2098 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002099 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002101 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 /*
Paul Bakker45829992013-01-03 14:52:21 +01002104 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2107 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002108 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002109#endif
Paul Bakker45829992013-01-03 14:52:21 +01002110
2111 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2112 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002115 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2116 ssl->transform_in->ivlen,
2117 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002119 }
2120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002121 dec_msglen = ssl->in_msglen;
2122 dec_msg = ssl->in_msg;
2123 dec_msg_result = ssl->in_msg;
2124
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002125 /*
2126 * Authenticate before decrypt if enabled
2127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2129 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002130 {
Hanno Becker992b6872017-11-09 18:57:39 +00002131 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002132 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002136 dec_msglen -= ssl->transform_in->maclen;
2137 ssl->in_msglen -= ssl->transform_in->maclen;
2138
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002139 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2140 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2141 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2142 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2147 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002148 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002149 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002154 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002155 ssl->transform_in->maclen );
2156
Hanno Becker992b6872017-11-09 18:57:39 +00002157 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2158 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002163 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002164 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002165 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167
2168 /*
2169 * Check length sanity
2170 */
2171 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002174 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002180 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002185 dec_msglen -= ssl->transform_in->ivlen;
2186 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002187
Paul Bakker48916f92012-09-16 19:57:18 +00002188 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002189 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002194 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002195 ssl->transform_in->ivlen,
2196 dec_msg, dec_msglen,
2197 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 return( ret );
2201 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002202
Paul Bakkercca5b812013-08-31 17:40:26 +02002203 if( dec_msglen != olen )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002207 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2210 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002211 {
2212 /*
2213 * Save IV in SSL3 and TLS1
2214 */
2215 memcpy( ssl->transform_in->iv_dec,
2216 ssl->transform_in->cipher_ctx_dec.iv,
2217 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
2221 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002222
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002224 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_DEBUG_ALL)
2227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002228 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002229#endif
Paul Bakker45829992013-01-03 14:52:21 +01002230 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002231 correct = 0;
2232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#if defined(MBEDTLS_SSL_PROTO_SSL3)
2235 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
Paul Bakker48916f92012-09-16 19:57:18 +00002237 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_DEBUG_ALL)
2240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002242 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002243#endif
Paul Bakker45829992013-01-03 14:52:21 +01002244 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 }
2246 }
2247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2249#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2251 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 /*
Paul Bakker45829992013-01-03 14:52:21 +01002254 * TLSv1+: always check the padding up to the first failure
2255 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002257 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002258 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002259 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002260
Paul Bakker956c9e02013-12-19 14:42:28 +01002261 /*
2262 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002263 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002264 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002265 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002266 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002267 *
2268 * In both cases we reset padding_idx to a safe value (0) to
2269 * prevent out-of-buffer reads.
2270 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002271 correct &= ( padlen <= ssl->in_msglen );
2272 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002273 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002274
2275 padding_idx *= correct;
2276
Angus Grattonb512bc12018-06-19 15:57:50 +10002277 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002278 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002279 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002280 pad_count += real_count *
2281 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2282 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002283
2284 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002287 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002289#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002290 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2294 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002298 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002299
2300 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002302 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002303#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002309#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 * Authenticate if not done yet.
2316 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002318#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002320 {
Hanno Becker992b6872017-11-09 18:57:39 +00002321 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002322
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002323 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002325 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2326 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#if defined(MBEDTLS_SSL_PROTO_SSL3)
2329 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002330 {
2331 ssl_mac( &ssl->transform_in->md_ctx_dec,
2332 ssl->transform_in->mac_dec,
2333 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002334 ssl->in_ctr, ssl->in_msgtype,
2335 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002336 }
2337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2339#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2340 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2341 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002342 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002343 int ret;
2344 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002345
2346 /*
2347 * The next two sizes are the minimum and maximum values of
2348 * in_msglen over all padlen values.
2349 *
2350 * They're independent of padlen, since we previously did
2351 * in_msglen -= padlen.
2352 *
2353 * Note that max_len + maclen is never more than the buffer
2354 * length, as we previously did in_msglen -= maclen too.
2355 */
2356 const size_t max_len = ssl->in_msglen + padlen;
2357 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2358
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002359 memcpy( add_data + 0, ssl->in_ctr, 8 );
2360 memcpy( add_data + 8, ssl->in_hdr, 3 );
2361 memcpy( add_data + 11, ssl->in_len, 2 );
2362
2363 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2364 add_data, sizeof( add_data ),
2365 ssl->in_msg, ssl->in_msglen,
2366 min_len, max_len,
2367 mac_expect );
2368 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002369 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2371 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02002372 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002373
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002374 /* Make sure we access all the memory that could contain the MAC,
2375 * before we check it in the next code block. This makes the
2376 * synchronisation requirements for just-in-time Prime+Probe
2377 * attacks much tighter and hopefully impractical. */
2378 ssl_read_memory( ssl->in_msg + min_len,
2379 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002380 }
2381 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2383 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002389#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002390 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2391 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2392 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002393#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Hanno Becker992b6872017-11-09 18:57:39 +00002395 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2396 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#if defined(MBEDTLS_SSL_DEBUG_ALL)
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002400#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002401 correct = 0;
2402 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002403 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002404 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002405
2406 /*
2407 * Finally check the correct flag
2408 */
2409 if( correct == 0 )
2410 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002411#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002412
2413 /* Make extra sure authentication was performed, exactly once */
2414 if( auth_done != 1 )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 if( ssl->in_msglen == 0 )
2421 {
Angus Gratton34817922018-06-19 15:58:22 +10002422#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2423 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2424 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2425 {
2426 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2428 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2429 }
2430#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2431
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 ssl->nb_zero++;
2433
2434 /*
2435 * Three or more empty messages may be a DoS attack
2436 * (excessive CPU consumption).
2437 */
2438 if( ssl->nb_zero > 3 )
2439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 }
2444 }
2445 else
2446 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002449 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002450 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002451 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002452 }
2453 else
2454#endif
2455 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002456 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002457 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2458 if( ++ssl->in_ctr[i - 1] != 0 )
2459 break;
2460
2461 /* The loop goes to its end iff the counter is wrapping */
2462 if( i == ssl_ep_len( ssl ) )
2463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2465 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002466 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002467 }
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
2471 return( 0 );
2472}
2473
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002474#undef MAC_NONE
2475#undef MAC_PLAINTEXT
2476#undef MAC_CIPHERTEXT
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002479/*
2480 * Compression/decompression functions
2481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002483{
2484 int ret;
2485 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002486 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002488 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002491
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002492 if( len_pre == 0 )
2493 return( 0 );
2494
Paul Bakker2770fbd2012-07-03 13:30:23 +00002495 memcpy( msg_pre, ssl->out_msg, len_pre );
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002498 ssl->out_msglen ) );
2499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002501 ssl->out_msg, ssl->out_msglen );
2502
Paul Bakker48916f92012-09-16 19:57:18 +00002503 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2504 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2505 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002506 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002507
Paul Bakker48916f92012-09-16 19:57:18 +00002508 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002509 if( ret != Z_OK )
2510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2512 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002513 }
2514
Angus Grattond8213d02016-05-25 20:56:48 +10002515 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002516 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002519 ssl->out_msglen ) );
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002522 ssl->out_msg, ssl->out_msglen );
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002525
2526 return( 0 );
2527}
2528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002530{
2531 int ret;
2532 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002533 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002534 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002535 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002538
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002539 if( len_pre == 0 )
2540 return( 0 );
2541
Paul Bakker2770fbd2012-07-03 13:30:23 +00002542 memcpy( msg_pre, ssl->in_msg, len_pre );
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002545 ssl->in_msglen ) );
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002548 ssl->in_msg, ssl->in_msglen );
2549
Paul Bakker48916f92012-09-16 19:57:18 +00002550 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2551 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2552 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002553 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002554 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002555
Paul Bakker48916f92012-09-16 19:57:18 +00002556 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002557 if( ret != Z_OK )
2558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2560 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002561 }
2562
Angus Grattond8213d02016-05-25 20:56:48 +10002563 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002564 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002567 ssl->in_msglen ) );
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002570 ssl->in_msg, ssl->in_msglen );
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002573
2574 return( 0 );
2575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2579static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#if defined(MBEDTLS_SSL_PROTO_DTLS)
2582static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002583{
2584 /* If renegotiation is not enforced, retransmit until we would reach max
2585 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002586 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002587 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002588 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002589 unsigned char doublings = 1;
2590
2591 while( ratio != 0 )
2592 {
2593 ++doublings;
2594 ratio >>= 1;
2595 }
2596
2597 if( ++ssl->renego_records_seen > doublings )
2598 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002600 return( 0 );
2601 }
2602 }
2603
2604 return( ssl_write_hello_request( ssl ) );
2605}
2606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002608
Paul Bakker5121ce52009-01-03 21:22:43 +00002609/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002610 * Fill the input message buffer by appending data to it.
2611 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002612 *
2613 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2614 * available (from this read and/or a previous one). Otherwise, an error code
2615 * is returned (possibly EOF or WANT_READ).
2616 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002617 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2618 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2619 * since we always read a whole datagram at once.
2620 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002621 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002622 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002625{
Paul Bakker23986e52011-04-24 08:57:21 +00002626 int ret;
2627 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002631 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002634 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002636 }
2637
Angus Grattond8213d02016-05-25 20:56:48 +10002638 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002642 }
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002647 uint32_t timeout;
2648
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002649 /* Just to be sure */
2650 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2651 {
2652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2653 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2655 }
2656
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002657 /*
2658 * The point is, we need to always read a full datagram at once, so we
2659 * sometimes read more then requested, and handle the additional data.
2660 * It could be the rest of the current record (while fetching the
2661 * header) and/or some other records in the same datagram.
2662 */
2663
2664 /*
2665 * Move to the next record in the already read datagram if applicable
2666 */
2667 if( ssl->next_record_offset != 0 )
2668 {
2669 if( ssl->in_left < ssl->next_record_offset )
2670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002673 }
2674
2675 ssl->in_left -= ssl->next_record_offset;
2676
2677 if( ssl->in_left != 0 )
2678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002680 ssl->next_record_offset ) );
2681 memmove( ssl->in_hdr,
2682 ssl->in_hdr + ssl->next_record_offset,
2683 ssl->in_left );
2684 }
2685
2686 ssl->next_record_offset = 0;
2687 }
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002691
2692 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002693 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002694 */
2695 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002699 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002700
2701 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002702 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002703 * are not at the beginning of a new record, the caller did something
2704 * wrong.
2705 */
2706 if( ssl->in_left != 0 )
2707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002710 }
2711
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002712 /*
2713 * Don't even try to read if time's out already.
2714 * This avoids by-passing the timer when repeatedly receiving messages
2715 * that will end up being dropped.
2716 */
2717 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002718 {
2719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002720 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002721 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002722 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002723 {
Angus Grattond8213d02016-05-25 20:56:48 +10002724 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002727 timeout = ssl->handshake->retransmit_timeout;
2728 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002729 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002732
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002733 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002734 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2735 timeout );
2736 else
2737 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002740
2741 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002743 }
2744
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002745 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002748 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002751 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002752 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002755 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002756 }
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002761 return( ret );
2762 }
2763
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002764 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002765 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002767 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002769 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002770 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002773 return( ret );
2774 }
2775
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002776 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002779 }
2780
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 if( ret < 0 )
2782 return( ret );
2783
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002784 ssl->in_left = ret;
2785 }
2786 else
2787#endif
2788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002790 ssl->in_left, nb_want ) );
2791
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002792 while( ssl->in_left < nb_want )
2793 {
2794 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002795
2796 if( ssl_check_timer( ssl ) != 0 )
2797 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2798 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002799 {
2800 if( ssl->f_recv_timeout != NULL )
2801 {
2802 ret = ssl->f_recv_timeout( ssl->p_bio,
2803 ssl->in_hdr + ssl->in_left, len,
2804 ssl->conf->read_timeout );
2805 }
2806 else
2807 {
2808 ret = ssl->f_recv( ssl->p_bio,
2809 ssl->in_hdr + ssl->in_left, len );
2810 }
2811 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002814 ssl->in_left, nb_want ) );
2815 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002816
2817 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002819
2820 if( ret < 0 )
2821 return( ret );
2822
makise-homura8c3fa632020-08-24 18:33:54 +03002823 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002824 {
Darryl Green11999bb2018-03-13 15:22:58 +00002825 MBEDTLS_SSL_DEBUG_MSG( 1,
2826 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002827 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2829 }
2830
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002831 ssl->in_left += ret;
2832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 return( 0 );
2838}
2839
2840/*
2841 * Flush any data not yet written
2842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002844{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002845 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002846 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002850 if( ssl->f_send == NULL )
2851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002853 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002855 }
2856
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002857 /* Avoid incrementing counter if data is flushed */
2858 if( ssl->out_left == 0 )
2859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002861 return( 0 );
2862 }
2863
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 while( ssl->out_left > 0 )
2865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2867 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Becker2b1e3542018-08-06 11:19:13 +01002869 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002870 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
2874 if( ret <= 0 )
2875 return( ret );
2876
makise-homura8c3fa632020-08-24 18:33:54 +03002877 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002878 {
Darryl Green11999bb2018-03-13 15:22:58 +00002879 MBEDTLS_SSL_DEBUG_MSG( 1,
2880 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002881 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2883 }
2884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 ssl->out_left -= ret;
2886 }
2887
Hanno Becker2b1e3542018-08-06 11:19:13 +01002888#if defined(MBEDTLS_SSL_PROTO_DTLS)
2889 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002890 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002891 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002892 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002893 else
2894#endif
2895 {
2896 ssl->out_hdr = ssl->out_buf + 8;
2897 }
2898 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
2902 return( 0 );
2903}
2904
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002905/*
2906 * Functions to handle the DTLS retransmission state machine
2907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002909/*
2910 * Append current handshake message to current outgoing flight
2911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2916 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2917 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002918
2919 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002920 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002921 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002924 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925 }
2926
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002927 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002928 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002931 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002932 }
2933
2934 /* Copy current handshake message with headers */
2935 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2936 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002937 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002938 msg->next = NULL;
2939
2940 /* Append to the current flight */
2941 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002942 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002943 else
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002946 while( cur->next != NULL )
2947 cur = cur->next;
2948 cur->next = msg;
2949 }
2950
Hanno Becker3b235902018-08-06 09:54:53 +01002951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002952 return( 0 );
2953}
2954
2955/*
2956 * Free the current flight of handshake messages
2957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 mbedtls_ssl_flight_item *cur = flight;
2961 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002962
2963 while( cur != NULL )
2964 {
2965 next = cur->next;
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 mbedtls_free( cur->p );
2968 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002969
2970 cur = next;
2971 }
2972}
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2975static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002976#endif
2977
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002978/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002979 * Swap transform_out and out_ctr with the alternative ones
2980 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002981static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002984 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002985#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2986 int ret;
2987#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002988
2989 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002992 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002993 }
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002996
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002997 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002998 tmp_transform = ssl->transform_out;
2999 ssl->transform_out = ssl->handshake->alt_transform_out;
3000 ssl->handshake->alt_transform_out = tmp_transform;
3001
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003002 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003003 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3004 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003005 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003006
3007 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003008 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3011 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3016 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003017 }
3018 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003019#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3020
3021 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003022}
3023
3024/*
3025 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003026 */
3027int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3028{
3029 int ret = 0;
3030
3031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3032
3033 ret = mbedtls_ssl_flight_transmit( ssl );
3034
3035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3036
3037 return( ret );
3038}
3039
3040/*
3041 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003042 *
3043 * Need to remember the current message in case flush_output returns
3044 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003045 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003047int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003049 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003053 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003055
3056 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003057 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003058 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3059 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003062 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003063
3064 while( ssl->handshake->cur_msg != NULL )
3065 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003066 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003067 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003068
Hanno Beckere1dcb032018-08-17 16:47:58 +01003069 int const is_finished =
3070 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3071 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3072
Hanno Becker04da1892018-08-14 13:22:10 +01003073 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3074 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3075
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003076 /* Swap epochs before sending Finished: we can't do it after
3077 * sending ChangeCipherSpec, in case write returns WANT_READ.
3078 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003079 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003080 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003082 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3083 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003084 }
3085
Hanno Becker67bc7c32018-08-06 11:33:50 +01003086 ret = ssl_get_remaining_payload_in_datagram( ssl );
3087 if( ret < 0 )
3088 return( ret );
3089 max_frag_len = (size_t) ret;
3090
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003091 /* CCS is copied as is, while HS messages may need fragmentation */
3092 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3093 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003094 if( max_frag_len == 0 )
3095 {
3096 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3097 return( ret );
3098
3099 continue;
3100 }
3101
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003102 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003103 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003104 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003106 /* Update position inside current message */
3107 ssl->handshake->cur_msg_p += cur->len;
3108 }
3109 else
3110 {
3111 const unsigned char * const p = ssl->handshake->cur_msg_p;
3112 const size_t hs_len = cur->len - 12;
3113 const size_t frag_off = p - ( cur->p + 12 );
3114 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003115 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003116
Hanno Beckere1dcb032018-08-17 16:47:58 +01003117 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003118 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003119 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003120 {
3121 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3122 return( ret );
3123 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124
Hanno Becker67bc7c32018-08-06 11:33:50 +01003125 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3126 return( ret );
3127
3128 continue;
3129 }
3130 max_hs_frag_len = max_frag_len - 12;
3131
3132 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3133 max_hs_frag_len : rem_len;
3134
3135 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003136 {
3137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003138 (unsigned) cur_hs_frag_len,
3139 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003140 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003141
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003142 /* Messages are stored with handshake headers as if not fragmented,
3143 * copy beginning of headers then fill fragmentation fields.
3144 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3145 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003146
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003147 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3148 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3149 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3150
Hanno Becker67bc7c32018-08-06 11:33:50 +01003151 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3152 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3153 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003154
3155 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3156
Hanno Becker3f7b9732018-08-28 09:53:25 +01003157 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003158 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3159 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003160 ssl->out_msgtype = cur->type;
3161
3162 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003163 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003164 }
3165
3166 /* If done with the current message move to the next one if any */
3167 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3168 {
3169 if( cur->next != NULL )
3170 {
3171 ssl->handshake->cur_msg = cur->next;
3172 ssl->handshake->cur_msg_p = cur->next->p + 12;
3173 }
3174 else
3175 {
3176 ssl->handshake->cur_msg = NULL;
3177 ssl->handshake->cur_msg_p = NULL;
3178 }
3179 }
3180
3181 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003182 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003185 return( ret );
3186 }
3187 }
3188
Hanno Becker67bc7c32018-08-06 11:33:50 +01003189 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3190 return( ret );
3191
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003192 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3194 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003195 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003198 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3199 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003200
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202
3203 return( 0 );
3204}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003205
3206/*
3207 * To be called when the last message of an incoming flight is received.
3208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003210{
3211 /* We won't need to resend that one any more */
3212 ssl_flight_free( ssl->handshake->flight );
3213 ssl->handshake->flight = NULL;
3214 ssl->handshake->cur_msg = NULL;
3215
3216 /* The next incoming flight will start with this msg_seq */
3217 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3218
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003219 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003220 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003221
Hanno Becker0271f962018-08-16 13:23:47 +01003222 /* Clear future message buffering structure. */
3223 ssl_buffering_free( ssl );
3224
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003225 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003226 ssl_set_timer( ssl, 0 );
3227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3229 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003232 }
3233 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003235}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003236
3237/*
3238 * To be called when the last message of an outgoing flight is send.
3239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003241{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003242 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003243 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3246 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003249 }
3250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254
Paul Bakker5121ce52009-01-03 21:22:43 +00003255/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003256 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258
3259/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003260 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003261 *
3262 * - fill in handshake headers
3263 * - update handshake checksum
3264 * - DTLS: save message for resending
3265 * - then pass to the record layer
3266 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003267 * DTLS: except for HelloRequest, messages are only queued, and will only be
3268 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003269 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003270 * Inputs:
3271 * - ssl->out_msglen: 4 + actual handshake message len
3272 * (4 is the size of handshake headers for TLS)
3273 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3274 * - ssl->out_msg + 4: the handshake message body
3275 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003276 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003277 * - ssl->out_msglen: the length of the record contents
3278 * (including handshake headers but excluding record headers)
3279 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003280 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003281int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003283 int ret;
3284 const size_t hs_len = ssl->out_msglen - 4;
3285 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3288
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003289 /*
3290 * Sanity checks
3291 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003292 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003293 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3294 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003295 /* In SSLv3, the client might send a NoCertificate alert. */
3296#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3297 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3298 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3299 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3300#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3301 {
3302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3303 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3304 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003307 /* Whenever we send anything different from a
3308 * HelloRequest we should be in a handshake - double check. */
3309 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3310 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003311 ssl->handshake == NULL )
3312 {
3313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003326
Hanno Beckerb50a2532018-08-06 11:52:54 +01003327 /* Double-check that we did not exceed the bounds
3328 * of the outgoing record buffer.
3329 * This should never fail as the various message
3330 * writing functions must obey the bounds of the
3331 * outgoing record buffer, but better be safe.
3332 *
3333 * Note: We deliberately do not check for the MTU or MFL here.
3334 */
3335 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3338 "size %u, maximum %u",
3339 (unsigned) ssl->out_msglen,
3340 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3342 }
3343
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003344 /*
3345 * Fill handshake headers
3346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003349 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3350 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3351 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003353 /*
3354 * DTLS has additional fields in the Handshake layer,
3355 * between the length field and the actual payload:
3356 * uint16 message_seq;
3357 * uint24 fragment_offset;
3358 * uint24 fragment_length;
3359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003361 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003362 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003363 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003364 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003365 {
3366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3367 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003368 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003369 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3371 }
3372
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003373 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003374 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003375
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003376 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003377 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003378 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003379 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3380 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3381 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003382 }
3383 else
3384 {
3385 ssl->out_msg[4] = 0;
3386 ssl->out_msg[5] = 0;
3387 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003388
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003389 /* Handshake hashes are computed without fragmentation,
3390 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003391 memset( ssl->out_msg + 6, 0x00, 3 );
3392 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003393 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003395
Hanno Becker0207e532018-08-28 10:28:28 +01003396 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003397 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3398 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 }
3400
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003401 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003404 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3405 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003406 {
3407 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003410 return( ret );
3411 }
3412 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003413 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003415 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003417 {
3418 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3419 return( ret );
3420 }
3421 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003422
3423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3424
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003425 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003426}
3427
3428/*
3429 * Record layer functions
3430 */
3431
3432/*
3433 * Write current record.
3434 *
3435 * Uses:
3436 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3437 * - ssl->out_msglen: length of the record content (excl headers)
3438 * - ssl->out_msg: record content
3439 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003440int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003441{
3442 int ret, done = 0;
3443 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003445
3446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003449 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003451 {
3452 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455 return( ret );
3456 }
3457
3458 len = ssl->out_msglen;
3459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3463 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ret = mbedtls_ssl_hw_record_write( ssl );
3468 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3471 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003472 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003473
3474 if( ret == 0 )
3475 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003478 if( !done )
3479 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003480 unsigned i;
3481 size_t protected_record_size;
3482
Paul Bakker05ef8352012-05-08 09:17:57 +00003483 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003485 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003486
Hanno Becker19859472018-08-06 09:40:20 +01003487 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003488 ssl->out_len[0] = (unsigned char)( len >> 8 );
3489 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003490
Paul Bakker48916f92012-09-16 19:57:18 +00003491 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003492 {
3493 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003496 return( ret );
3497 }
3498
3499 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003500 ssl->out_len[0] = (unsigned char)( len >> 8 );
3501 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003502 }
3503
Hanno Becker2b1e3542018-08-06 11:19:13 +01003504 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3505
3506#if defined(MBEDTLS_SSL_PROTO_DTLS)
3507 /* In case of DTLS, double-check that we don't exceed
3508 * the remaining space in the datagram. */
3509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3510 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003511 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003512 if( ret < 0 )
3513 return( ret );
3514
3515 if( protected_record_size > (size_t) ret )
3516 {
3517 /* Should never happen */
3518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3519 }
3520 }
3521#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003524 "version = [%d:%d], msglen = %d",
3525 ssl->out_hdr[0], ssl->out_hdr[1],
3526 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003529 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003530
3531 ssl->out_left += protected_record_size;
3532 ssl->out_hdr += protected_record_size;
3533 ssl_update_out_pointers( ssl, ssl->transform_out );
3534
Hanno Becker04484622018-08-06 09:49:38 +01003535 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3536 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3537 break;
3538
3539 /* The loop goes to its end iff the counter is wrapping */
3540 if( i == ssl_ep_len( ssl ) )
3541 {
3542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3543 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 }
3546
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3549 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003550 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003551 size_t remaining;
3552 ret = ssl_get_remaining_payload_in_datagram( ssl );
3553 if( ret < 0 )
3554 {
3555 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3556 ret );
3557 return( ret );
3558 }
3559
3560 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003561 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003562 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003563 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003564 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 else
3566 {
Hanno Becker513815a2018-08-20 11:56:09 +01003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003568 }
3569 }
3570#endif /* MBEDTLS_SSL_PROTO_DTLS */
3571
3572 if( ( flush == SSL_FORCE_FLUSH ) &&
3573 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 return( ret );
3577 }
3578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580
3581 return( 0 );
3582}
3583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003585
3586static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3587{
3588 if( ssl->in_msglen < ssl->in_hslen ||
3589 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3590 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3591 {
3592 return( 1 );
3593 }
3594 return( 0 );
3595}
Hanno Becker44650b72018-08-16 12:51:11 +01003596
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003597static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003598{
3599 return( ( ssl->in_msg[9] << 16 ) |
3600 ( ssl->in_msg[10] << 8 ) |
3601 ssl->in_msg[11] );
3602}
3603
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003604static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003605{
3606 return( ( ssl->in_msg[6] << 16 ) |
3607 ( ssl->in_msg[7] << 8 ) |
3608 ssl->in_msg[8] );
3609}
3610
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003611static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003612{
3613 uint32_t msg_len, frag_off, frag_len;
3614
3615 msg_len = ssl_get_hs_total_len( ssl );
3616 frag_off = ssl_get_hs_frag_off( ssl );
3617 frag_len = ssl_get_hs_frag_len( ssl );
3618
3619 if( frag_off > msg_len )
3620 return( -1 );
3621
3622 if( frag_len > msg_len - frag_off )
3623 return( -1 );
3624
3625 if( frag_len + 12 > ssl->in_msglen )
3626 return( -1 );
3627
3628 return( 0 );
3629}
3630
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003631/*
3632 * Mark bits in bitmask (used for DTLS HS reassembly)
3633 */
3634static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3635{
3636 unsigned int start_bits, end_bits;
3637
3638 start_bits = 8 - ( offset % 8 );
3639 if( start_bits != 8 )
3640 {
3641 size_t first_byte_idx = offset / 8;
3642
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003643 /* Special case */
3644 if( len <= start_bits )
3645 {
3646 for( ; len != 0; len-- )
3647 mask[first_byte_idx] |= 1 << ( start_bits - len );
3648
3649 /* Avoid potential issues with offset or len becoming invalid */
3650 return;
3651 }
3652
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003653 offset += start_bits; /* Now offset % 8 == 0 */
3654 len -= start_bits;
3655
3656 for( ; start_bits != 0; start_bits-- )
3657 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3658 }
3659
3660 end_bits = len % 8;
3661 if( end_bits != 0 )
3662 {
3663 size_t last_byte_idx = ( offset + len ) / 8;
3664
3665 len -= end_bits; /* Now len % 8 == 0 */
3666
3667 for( ; end_bits != 0; end_bits-- )
3668 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3669 }
3670
3671 memset( mask + offset / 8, 0xFF, len / 8 );
3672}
3673
3674/*
3675 * Check that bitmask is full
3676 */
3677static int ssl_bitmask_check( unsigned char *mask, size_t len )
3678{
3679 size_t i;
3680
3681 for( i = 0; i < len / 8; i++ )
3682 if( mask[i] != 0xFF )
3683 return( -1 );
3684
3685 for( i = 0; i < len % 8; i++ )
3686 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3687 return( -1 );
3688
3689 return( 0 );
3690}
3691
Hanno Becker56e205e2018-08-16 09:06:12 +01003692/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003693static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003694 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003695{
Hanno Becker56e205e2018-08-16 09:06:12 +01003696 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003697
Hanno Becker56e205e2018-08-16 09:06:12 +01003698 alloc_len = 12; /* Handshake header */
3699 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003700
Hanno Beckerd07df862018-08-16 09:14:58 +01003701 if( add_bitmap )
3702 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003703
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003704 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003705}
Hanno Becker56e205e2018-08-16 09:06:12 +01003706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003708
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003709static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003710{
3711 return( ( ssl->in_msg[1] << 16 ) |
3712 ( ssl->in_msg[2] << 8 ) |
3713 ssl->in_msg[3] );
3714}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003715
Simon Butcher99000142016-10-13 17:21:01 +01003716int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003721 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003723 }
3724
Hanno Becker12555c62018-08-16 12:47:53 +01003725 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003728 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003729 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003733 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003734 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003735 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003736
Hanno Becker44650b72018-08-16 12:51:11 +01003737 if( ssl_check_hs_header( ssl ) != 0 )
3738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3740 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3741 }
3742
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003743 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003744 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3745 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3746 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3747 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003748 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003749 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3752 recv_msg_seq,
3753 ssl->handshake->in_msg_seq ) );
3754 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3755 }
3756
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003757 /* Retransmit only on last message from previous flight, to avoid
3758 * too many retransmissions.
3759 * Besides, No sane server ever retransmits HelloVerifyRequest */
3760 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003764 "message_seq = %d, start_of_flight = %d",
3765 recv_msg_seq,
3766 ssl->handshake->in_flight_start_seq ) );
3767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003771 return( ret );
3772 }
3773 }
3774 else
3775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003777 "message_seq = %d, expected = %d",
3778 recv_msg_seq,
3779 ssl->handshake->in_msg_seq ) );
3780 }
3781
Hanno Becker90333da2017-10-10 11:27:13 +01003782 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003783 }
3784 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003785
Hanno Becker6d97ef52018-08-16 13:09:04 +01003786 /* Message reassembly is handled alongside buffering of future
3787 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003788 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003789 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003790 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003793 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003794 }
3795 }
3796 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003798 /* With TLS we don't handle fragmentation (for now) */
3799 if( ssl->in_msglen < ssl->in_hslen )
3800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3802 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003803 }
3804
Simon Butcher99000142016-10-13 17:21:01 +01003805 return( 0 );
3806}
3807
3808void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3809{
Hanno Becker0271f962018-08-16 13:23:47 +01003810 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003811
Hanno Becker0271f962018-08-16 13:23:47 +01003812 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003813 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003814 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003815 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003816
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003817 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003820 ssl->handshake != NULL )
3821 {
Hanno Becker0271f962018-08-16 13:23:47 +01003822 unsigned offset;
3823 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003824
Hanno Becker0271f962018-08-16 13:23:47 +01003825 /* Increment handshake sequence number */
3826 hs->in_msg_seq++;
3827
3828 /*
3829 * Clear up handshake buffering and reassembly structure.
3830 */
3831
3832 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003833 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003834
3835 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003836 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3837 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003838 offset++, hs_buf++ )
3839 {
3840 *hs_buf = *(hs_buf + 1);
3841 }
3842
3843 /* Create a fresh last entry */
3844 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003845 }
3846#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003847}
3848
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003849/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003850 * DTLS anti-replay: RFC 6347 4.1.2.6
3851 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003852 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3853 * Bit n is set iff record number in_window_top - n has been seen.
3854 *
3855 * Usually, in_window_top is the last record number seen and the lsb of
3856 * in_window is set. The only exception is the initial state (record number 0
3857 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3860static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003861{
3862 ssl->in_window_top = 0;
3863 ssl->in_window = 0;
3864}
3865
3866static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3867{
3868 return( ( (uint64_t) buf[0] << 40 ) |
3869 ( (uint64_t) buf[1] << 32 ) |
3870 ( (uint64_t) buf[2] << 24 ) |
3871 ( (uint64_t) buf[3] << 16 ) |
3872 ( (uint64_t) buf[4] << 8 ) |
3873 ( (uint64_t) buf[5] ) );
3874}
3875
3876/*
3877 * Return 0 if sequence number is acceptable, -1 otherwise
3878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003880{
3881 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3882 uint64_t bit;
3883
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003884 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003885 return( 0 );
3886
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003887 if( rec_seqnum > ssl->in_window_top )
3888 return( 0 );
3889
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003890 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003891
3892 if( bit >= 64 )
3893 return( -1 );
3894
3895 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3896 return( -1 );
3897
3898 return( 0 );
3899}
3900
3901/*
3902 * Update replay window on new validated record
3903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003905{
3906 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3907
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003908 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003909 return;
3910
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003911 if( rec_seqnum > ssl->in_window_top )
3912 {
3913 /* Update window_top and the contents of the window */
3914 uint64_t shift = rec_seqnum - ssl->in_window_top;
3915
3916 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003917 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003918 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003919 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003920 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003921 ssl->in_window |= 1;
3922 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003923
3924 ssl->in_window_top = rec_seqnum;
3925 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003926 else
3927 {
3928 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003929 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003930
3931 if( bit < 64 ) /* Always true, but be extra sure */
3932 ssl->in_window |= (uint64_t) 1 << bit;
3933 }
3934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003936
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003937#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003938/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003939static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3940
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003941/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003942 * Without any SSL context, check if a datagram looks like a ClientHello with
3943 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003944 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003945 *
3946 * - if cookie is valid, return 0
3947 * - if ClientHello looks superficially valid but cookie is not,
3948 * fill obuf and set olen, then
3949 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3950 * - otherwise return a specific error code
3951 */
3952static int ssl_check_dtls_clihlo_cookie(
3953 mbedtls_ssl_cookie_write_t *f_cookie_write,
3954 mbedtls_ssl_cookie_check_t *f_cookie_check,
3955 void *p_cookie,
3956 const unsigned char *cli_id, size_t cli_id_len,
3957 const unsigned char *in, size_t in_len,
3958 unsigned char *obuf, size_t buf_len, size_t *olen )
3959{
3960 size_t sid_len, cookie_len;
3961 unsigned char *p;
3962
3963 if( f_cookie_write == NULL || f_cookie_check == NULL )
3964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3965
3966 /*
3967 * Structure of ClientHello with record and handshake headers,
3968 * and expected values. We don't need to check a lot, more checks will be
3969 * done when actually parsing the ClientHello - skipping those checks
3970 * avoids code duplication and does not make cookie forging any easier.
3971 *
3972 * 0-0 ContentType type; copied, must be handshake
3973 * 1-2 ProtocolVersion version; copied
3974 * 3-4 uint16 epoch; copied, must be 0
3975 * 5-10 uint48 sequence_number; copied
3976 * 11-12 uint16 length; (ignored)
3977 *
3978 * 13-13 HandshakeType msg_type; (ignored)
3979 * 14-16 uint24 length; (ignored)
3980 * 17-18 uint16 message_seq; copied
3981 * 19-21 uint24 fragment_offset; copied, must be 0
3982 * 22-24 uint24 fragment_length; (ignored)
3983 *
3984 * 25-26 ProtocolVersion client_version; (ignored)
3985 * 27-58 Random random; (ignored)
3986 * 59-xx SessionID session_id; 1 byte len + sid_len content
3987 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3988 * ...
3989 *
3990 * Minimum length is 61 bytes.
3991 */
3992 if( in_len < 61 ||
3993 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3994 in[3] != 0 || in[4] != 0 ||
3995 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3996 {
3997 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3998 }
3999
4000 sid_len = in[59];
4001 if( sid_len > in_len - 61 )
4002 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4003
4004 cookie_len = in[60 + sid_len];
4005 if( cookie_len > in_len - 60 )
4006 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4007
4008 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4009 cli_id, cli_id_len ) == 0 )
4010 {
4011 /* Valid cookie */
4012 return( 0 );
4013 }
4014
4015 /*
4016 * If we get here, we've got an invalid cookie, let's prepare HVR.
4017 *
4018 * 0-0 ContentType type; copied
4019 * 1-2 ProtocolVersion version; copied
4020 * 3-4 uint16 epoch; copied
4021 * 5-10 uint48 sequence_number; copied
4022 * 11-12 uint16 length; olen - 13
4023 *
4024 * 13-13 HandshakeType msg_type; hello_verify_request
4025 * 14-16 uint24 length; olen - 25
4026 * 17-18 uint16 message_seq; copied
4027 * 19-21 uint24 fragment_offset; copied
4028 * 22-24 uint24 fragment_length; olen - 25
4029 *
4030 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4031 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4032 *
4033 * Minimum length is 28.
4034 */
4035 if( buf_len < 28 )
4036 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4037
4038 /* Copy most fields and adapt others */
4039 memcpy( obuf, in, 25 );
4040 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4041 obuf[25] = 0xfe;
4042 obuf[26] = 0xff;
4043
4044 /* Generate and write actual cookie */
4045 p = obuf + 28;
4046 if( f_cookie_write( p_cookie,
4047 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4048 {
4049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4050 }
4051
4052 *olen = p - obuf;
4053
4054 /* Go back and fill length fields */
4055 obuf[27] = (unsigned char)( *olen - 28 );
4056
4057 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4058 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4059 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4060
4061 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4062 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4063
4064 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4065}
4066
4067/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004068 * Handle possible client reconnect with the same UDP quadruplet
4069 * (RFC 6347 Section 4.2.8).
4070 *
4071 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4072 * that looks like a ClientHello.
4073 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004074 * - if the input looks like a ClientHello without cookies,
4075 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004076 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004077 * - if the input looks like a ClientHello with a valid cookie,
4078 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004079 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004080 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004081 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004082 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004083 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4084 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004085 */
4086static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4087{
4088 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004089 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004090
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004091 ret = ssl_check_dtls_clihlo_cookie(
4092 ssl->conf->f_cookie_write,
4093 ssl->conf->f_cookie_check,
4094 ssl->conf->p_cookie,
4095 ssl->cli_id, ssl->cli_id_len,
4096 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004097 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004098
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004099 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4100
4101 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004102 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004103 int send_ret;
4104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4105 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4106 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004107 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004108 * If the error is permanent we'll catch it later,
4109 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004110 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4111 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4112 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004113
4114 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004115 }
4116
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004117 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004118 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004120 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4121 {
4122 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4123 return( ret );
4124 }
4125
4126 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004127 }
4128
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004129 return( ret );
4130}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004131#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004132
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004133/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004134 * ContentType type;
4135 * ProtocolVersion version;
4136 * uint16 epoch; // DTLS only
4137 * uint48 sequence_number; // DTLS only
4138 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004139 *
4140 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004141 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004142 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4143 *
4144 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004145 * 1. proceed with the record if this function returns 0
4146 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4147 * 3. return CLIENT_RECONNECT if this function return that value
4148 * 4. drop the whole datagram if this function returns anything else.
4149 * Point 2 is needed when the peer is resending, and we have already received
4150 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004153{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004154 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 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 +02004157
Paul Bakker5121ce52009-01-03 21:22:43 +00004158 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004159 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004160 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004164 ssl->in_msgtype,
4165 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004166
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004167 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4169 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4170 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4171 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004174
4175#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004176 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4177 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4179#endif /* MBEDTLS_SSL_PROTO_DTLS */
4180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4181 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004184 }
4185
4186 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004187 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4190 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 }
4192
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004193 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4196 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 }
4198
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004199 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004200 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004201 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4204 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004205 }
4206
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004207 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004208 * DTLS-related tests.
4209 * Check epoch before checking length constraint because
4210 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4211 * message gets duplicated before the corresponding Finished message,
4212 * the second ChangeCipherSpec should be discarded because it belongs
4213 * to an old epoch, but not because its length is shorter than
4214 * the minimum record length for packets using the new record transform.
4215 * Note that these two kinds of failures are handled differently,
4216 * as an unexpected record is silently skipped but an invalid
4217 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004218 */
4219#if defined(MBEDTLS_SSL_PROTO_DTLS)
4220 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4221 {
4222 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4223
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004224 /* Check epoch (and sequence number) with DTLS */
4225 if( rec_epoch != ssl->in_epoch )
4226 {
4227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4228 "expected %d, received %d",
4229 ssl->in_epoch, rec_epoch ) );
4230
4231#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4232 /*
4233 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4234 * access the first byte of record content (handshake type), as we
4235 * have an active transform (possibly iv_len != 0), so use the
4236 * fact that the record header len is 13 instead.
4237 */
4238 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4239 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4240 rec_epoch == 0 &&
4241 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4242 ssl->in_left > 13 &&
4243 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4244 {
4245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4246 "from the same port" ) );
4247 return( ssl_handle_possible_reconnect( ssl ) );
4248 }
4249 else
4250#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004251 {
4252 /* Consider buffering the record. */
4253 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4254 {
4255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4256 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4257 }
4258
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004259 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004260 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004261 }
4262
4263#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4264 /* Replay detection only works for the current epoch */
4265 if( rec_epoch == ssl->in_epoch &&
4266 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4267 {
4268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4269 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4270 }
4271#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004272
Hanno Becker52c6dc62017-05-26 16:07:36 +01004273 /* Drop unexpected ApplicationData records,
4274 * except at the beginning of renegotiations */
4275 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4276 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4277#if defined(MBEDTLS_SSL_RENEGOTIATION)
4278 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4279 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4280#endif
4281 )
4282 {
4283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4284 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4285 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004286 }
4287#endif /* MBEDTLS_SSL_PROTO_DTLS */
4288
Hanno Becker52c6dc62017-05-26 16:07:36 +01004289
4290 /* Check length against bounds of the current transform and version */
4291 if( ssl->transform_in == NULL )
4292 {
4293 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004294 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004295 {
4296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4298 }
4299 }
4300 else
4301 {
4302 if( ssl->in_msglen < ssl->transform_in->minlen )
4303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4305 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4306 }
4307
4308#if defined(MBEDTLS_SSL_PROTO_SSL3)
4309 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004310 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004311 {
4312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4313 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4314 }
4315#endif
4316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4317 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4318 /*
4319 * TLS encrypted messages can have up to 256 bytes of padding
4320 */
4321 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4322 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004323 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004324 {
4325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4326 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4327 }
4328#endif
4329 }
4330
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004331 return( 0 );
4332}
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334/*
4335 * If applicable, decrypt (and decompress) record content
4336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004338{
4339 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4342 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4345 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 ret = mbedtls_ssl_hw_record_read( ssl );
4350 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4353 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004354 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004355
4356 if( ret == 0 )
4357 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004360 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 {
4362 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 return( ret );
4366 }
4367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 ssl->in_msg, ssl->in_msglen );
4370
Angus Grattond8213d02016-05-25 20:56:48 +10004371 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4374 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 }
4376 }
4377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004379 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004381 {
4382 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004385 return( ret );
4386 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004394 }
4395#endif
4396
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004397 return( 0 );
4398}
4399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004401
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004402/*
4403 * Read a record.
4404 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004405 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4406 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4407 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004408 */
Hanno Becker1097b342018-08-15 14:09:41 +01004409
4410/* Helper functions for mbedtls_ssl_read_record(). */
4411static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004412static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4413static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004414
Hanno Becker327c93b2018-08-15 13:56:18 +01004415int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004416 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004417{
4418 int ret;
4419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004422 if( ssl->keep_current_message == 0 )
4423 {
4424 do {
Simon Butcher99000142016-10-13 17:21:01 +01004425
Hanno Becker26994592018-08-15 14:14:59 +01004426 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004427 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004428 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004429
Hanno Beckere74d5562018-08-15 14:26:08 +01004430 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004431 {
Hanno Becker40f50842018-08-15 14:48:01 +01004432#if defined(MBEDTLS_SSL_PROTO_DTLS)
4433 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004434
Hanno Becker40f50842018-08-15 14:48:01 +01004435 /* We only check for buffered messages if the
4436 * current datagram is fully consumed. */
4437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004438 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004439 {
Hanno Becker40f50842018-08-15 14:48:01 +01004440 if( ssl_load_buffered_message( ssl ) == 0 )
4441 have_buffered = 1;
4442 }
4443
4444 if( have_buffered == 0 )
4445#endif /* MBEDTLS_SSL_PROTO_DTLS */
4446 {
4447 ret = ssl_get_next_record( ssl );
4448 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4449 continue;
4450
4451 if( ret != 0 )
4452 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004453 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004454 return( ret );
4455 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004456 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004457 }
4458
4459 ret = mbedtls_ssl_handle_message_type( ssl );
4460
Hanno Becker40f50842018-08-15 14:48:01 +01004461#if defined(MBEDTLS_SSL_PROTO_DTLS)
4462 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4463 {
4464 /* Buffer future message */
4465 ret = ssl_buffer_message( ssl );
4466 if( ret != 0 )
4467 return( ret );
4468
4469 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4470 }
4471#endif /* MBEDTLS_SSL_PROTO_DTLS */
4472
Hanno Becker90333da2017-10-10 11:27:13 +01004473 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4474 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004475
4476 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004477 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004478 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004479 return( ret );
4480 }
4481
Hanno Becker327c93b2018-08-15 13:56:18 +01004482 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004483 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004484 {
4485 mbedtls_ssl_update_handshake_status( ssl );
4486 }
Simon Butcher99000142016-10-13 17:21:01 +01004487 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004488 else
Simon Butcher99000142016-10-13 17:21:01 +01004489 {
Hanno Becker02f59072018-08-15 14:00:24 +01004490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004491 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004492 }
4493
4494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4495
4496 return( 0 );
4497}
4498
Hanno Becker40f50842018-08-15 14:48:01 +01004499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004500static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004501{
Hanno Becker40f50842018-08-15 14:48:01 +01004502 if( ssl->in_left > ssl->next_record_offset )
4503 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004504
Hanno Becker40f50842018-08-15 14:48:01 +01004505 return( 0 );
4506}
4507
4508static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4509{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004510 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004511 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004512 int ret = 0;
4513
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004514 if( hs == NULL )
4515 return( -1 );
4516
Hanno Beckere00ae372018-08-20 09:39:42 +01004517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4518
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004519 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4520 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4521 {
4522 /* Check if we have seen a ChangeCipherSpec before.
4523 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004524 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004525 {
4526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4527 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004528 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004529 }
4530
Hanno Becker39b8bc92018-08-28 17:17:13 +01004531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004532 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4533 ssl->in_msglen = 1;
4534 ssl->in_msg[0] = 1;
4535
4536 /* As long as they are equal, the exact value doesn't matter. */
4537 ssl->in_left = 0;
4538 ssl->next_record_offset = 0;
4539
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004540 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004541 goto exit;
4542 }
Hanno Becker37f95322018-08-16 13:55:32 +01004543
Hanno Beckerb8f50142018-08-28 10:01:34 +01004544#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004545 /* Debug only */
4546 {
4547 unsigned offset;
4548 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4549 {
4550 hs_buf = &hs->buffering.hs[offset];
4551 if( hs_buf->is_valid == 1 )
4552 {
4553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4554 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004555 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004556 }
4557 }
4558 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004559#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004560
4561 /* Check if we have buffered and/or fully reassembled the
4562 * next handshake message. */
4563 hs_buf = &hs->buffering.hs[0];
4564 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4565 {
4566 /* Synthesize a record containing the buffered HS message. */
4567 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4568 ( hs_buf->data[2] << 8 ) |
4569 hs_buf->data[3];
4570
4571 /* Double-check that we haven't accidentally buffered
4572 * a message that doesn't fit into the input buffer. */
4573 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4574 {
4575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4577 }
4578
4579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4580 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4581 hs_buf->data, msg_len + 12 );
4582
4583 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4584 ssl->in_hslen = msg_len + 12;
4585 ssl->in_msglen = msg_len + 12;
4586 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4587
4588 ret = 0;
4589 goto exit;
4590 }
4591 else
4592 {
4593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4594 hs->in_msg_seq ) );
4595 }
4596
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004597 ret = -1;
4598
4599exit:
4600
4601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4602 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004603}
4604
Hanno Beckera02b0b42018-08-21 17:20:27 +01004605static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4606 size_t desired )
4607{
4608 int offset;
4609 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4611 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004612
Hanno Becker01315ea2018-08-21 17:22:17 +01004613 /* Get rid of future records epoch first, if such exist. */
4614 ssl_free_buffered_record( ssl );
4615
4616 /* Check if we have enough space available now. */
4617 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4618 hs->buffering.total_bytes_buffered ) )
4619 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004621 return( 0 );
4622 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004623
Hanno Becker4f432ad2018-08-28 10:02:32 +01004624 /* We don't have enough space to buffer the next expected handshake
4625 * message. Remove buffers used for future messages to gain space,
4626 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004627 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4628 offset >= 0; offset-- )
4629 {
4630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4631 offset ) );
4632
Hanno Beckerb309b922018-08-23 13:18:05 +01004633 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004634
4635 /* Check if we have enough space available now. */
4636 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4637 hs->buffering.total_bytes_buffered ) )
4638 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004640 return( 0 );
4641 }
4642 }
4643
4644 return( -1 );
4645}
4646
Hanno Becker40f50842018-08-15 14:48:01 +01004647static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4648{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004649 int ret = 0;
4650 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4651
4652 if( hs == NULL )
4653 return( 0 );
4654
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4656
4657 switch( ssl->in_msgtype )
4658 {
4659 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004661
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004662 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004663 break;
4664
4665 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004666 {
4667 unsigned recv_msg_seq_offset;
4668 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4669 mbedtls_ssl_hs_buffer *hs_buf;
4670 size_t msg_len = ssl->in_hslen - 12;
4671
4672 /* We should never receive an old handshake
4673 * message - double-check nonetheless. */
4674 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4675 {
4676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4678 }
4679
4680 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4681 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4682 {
4683 /* Silently ignore -- message too far in the future */
4684 MBEDTLS_SSL_DEBUG_MSG( 2,
4685 ( "Ignore future HS message with sequence number %u, "
4686 "buffering window %u - %u",
4687 recv_msg_seq, ssl->handshake->in_msg_seq,
4688 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4689
4690 goto exit;
4691 }
4692
4693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4694 recv_msg_seq, recv_msg_seq_offset ) );
4695
4696 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4697
4698 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004699 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004700 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004701 size_t reassembly_buf_sz;
4702
Hanno Becker37f95322018-08-16 13:55:32 +01004703 hs_buf->is_fragmented =
4704 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4705
4706 /* We copy the message back into the input buffer
4707 * after reassembly, so check that it's not too large.
4708 * This is an implementation-specific limitation
4709 * and not one from the standard, hence it is not
4710 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004711 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004712 {
4713 /* Ignore message */
4714 goto exit;
4715 }
4716
Hanno Beckere0b150f2018-08-21 15:51:03 +01004717 /* Check if we have enough space to buffer the message. */
4718 if( hs->buffering.total_bytes_buffered >
4719 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4723 }
4724
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004725 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4726 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004727
4728 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4729 hs->buffering.total_bytes_buffered ) )
4730 {
4731 if( recv_msg_seq_offset > 0 )
4732 {
4733 /* If we can't buffer a future message because
4734 * of space limitations -- ignore. */
4735 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",
4736 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4737 (unsigned) hs->buffering.total_bytes_buffered ) );
4738 goto exit;
4739 }
Hanno Beckere1801392018-08-21 16:51:05 +01004740 else
4741 {
4742 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",
4743 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4744 (unsigned) hs->buffering.total_bytes_buffered ) );
4745 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004746
Hanno Beckera02b0b42018-08-21 17:20:27 +01004747 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004748 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004749 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",
4750 (unsigned) msg_len,
4751 (unsigned) reassembly_buf_sz,
4752 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004753 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004754 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4755 goto exit;
4756 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004757 }
4758
4759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4760 msg_len ) );
4761
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004762 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4763 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004764 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004765 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004766 goto exit;
4767 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004768 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004769
4770 /* Prepare final header: copy msg_type, length and message_seq,
4771 * then add standardised fragment_offset and fragment_length */
4772 memcpy( hs_buf->data, ssl->in_msg, 6 );
4773 memset( hs_buf->data + 6, 0, 3 );
4774 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4775
4776 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004777
4778 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004779 }
4780 else
4781 {
4782 /* Make sure msg_type and length are consistent */
4783 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4784 {
4785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4786 /* Ignore */
4787 goto exit;
4788 }
4789 }
4790
Hanno Becker4422bbb2018-08-20 09:40:19 +01004791 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004792 {
4793 size_t frag_len, frag_off;
4794 unsigned char * const msg = hs_buf->data + 12;
4795
4796 /*
4797 * Check and copy current fragment
4798 */
4799
4800 /* Validation of header fields already done in
4801 * mbedtls_ssl_prepare_handshake_record(). */
4802 frag_off = ssl_get_hs_frag_off( ssl );
4803 frag_len = ssl_get_hs_frag_len( ssl );
4804
4805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4806 frag_off, frag_len ) );
4807 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4808
4809 if( hs_buf->is_fragmented )
4810 {
4811 unsigned char * const bitmask = msg + msg_len;
4812 ssl_bitmask_set( bitmask, frag_off, frag_len );
4813 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4814 msg_len ) == 0 );
4815 }
4816 else
4817 {
4818 hs_buf->is_complete = 1;
4819 }
4820
4821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4822 hs_buf->is_complete ? "" : "not yet " ) );
4823 }
4824
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004825 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004826 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004827
4828 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004829 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004830 break;
4831 }
4832
4833exit:
4834
4835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4836 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004837}
4838#endif /* MBEDTLS_SSL_PROTO_DTLS */
4839
Hanno Becker1097b342018-08-15 14:09:41 +01004840static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004841{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004842 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004843 * Consume last content-layer message and potentially
4844 * update in_msglen which keeps track of the contents'
4845 * consumption state.
4846 *
4847 * (1) Handshake messages:
4848 * Remove last handshake message, move content
4849 * and adapt in_msglen.
4850 *
4851 * (2) Alert messages:
4852 * Consume whole record content, in_msglen = 0.
4853 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004854 * (3) Change cipher spec:
4855 * Consume whole record content, in_msglen = 0.
4856 *
4857 * (4) Application data:
4858 * Don't do anything - the record layer provides
4859 * the application data as a stream transport
4860 * and consumes through mbedtls_ssl_read only.
4861 *
4862 */
4863
4864 /* Case (1): Handshake messages */
4865 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004866 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004867 /* Hard assertion to be sure that no application data
4868 * is in flight, as corrupting ssl->in_msglen during
4869 * ssl->in_offt != NULL is fatal. */
4870 if( ssl->in_offt != NULL )
4871 {
4872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4874 }
4875
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876 /*
4877 * Get next Handshake message in the current record
4878 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004879
Hanno Becker4a810fb2017-05-24 16:27:30 +01004880 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004881 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004882 * current handshake content: If DTLS handshake
4883 * fragmentation is used, that's the fragment
4884 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004885 * size here is faulty and should be changed at
4886 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004887 * (2) While it doesn't seem to cause problems, one
4888 * has to be very careful not to assume that in_hslen
4889 * is always <= in_msglen in a sensible communication.
4890 * Again, it's wrong for DTLS handshake fragmentation.
4891 * The following check is therefore mandatory, and
4892 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004893 * Additionally, ssl->in_hslen might be arbitrarily out of
4894 * bounds after handling a DTLS message with an unexpected
4895 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004896 */
4897 if( ssl->in_hslen < ssl->in_msglen )
4898 {
4899 ssl->in_msglen -= ssl->in_hslen;
4900 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4901 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004902
Hanno Becker4a810fb2017-05-24 16:27:30 +01004903 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4904 ssl->in_msg, ssl->in_msglen );
4905 }
4906 else
4907 {
4908 ssl->in_msglen = 0;
4909 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004910
Hanno Becker4a810fb2017-05-24 16:27:30 +01004911 ssl->in_hslen = 0;
4912 }
4913 /* Case (4): Application data */
4914 else if( ssl->in_offt != NULL )
4915 {
4916 return( 0 );
4917 }
4918 /* Everything else (CCS & Alerts) */
4919 else
4920 {
4921 ssl->in_msglen = 0;
4922 }
4923
Hanno Becker1097b342018-08-15 14:09:41 +01004924 return( 0 );
4925}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004926
Hanno Beckere74d5562018-08-15 14:26:08 +01004927static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4928{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004929 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004930 return( 1 );
4931
4932 return( 0 );
4933}
4934
Hanno Becker5f066e72018-08-16 14:56:31 +01004935#if defined(MBEDTLS_SSL_PROTO_DTLS)
4936
4937static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4938{
4939 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4940 if( hs == NULL )
4941 return;
4942
Hanno Becker01315ea2018-08-21 17:22:17 +01004943 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004944 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004945 hs->buffering.total_bytes_buffered -=
4946 hs->buffering.future_record.len;
4947
4948 mbedtls_free( hs->buffering.future_record.data );
4949 hs->buffering.future_record.data = NULL;
4950 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004951}
4952
4953static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4954{
4955 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4956 unsigned char * rec;
4957 size_t rec_len;
4958 unsigned rec_epoch;
4959
4960 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4961 return( 0 );
4962
4963 if( hs == NULL )
4964 return( 0 );
4965
Hanno Becker5f066e72018-08-16 14:56:31 +01004966 rec = hs->buffering.future_record.data;
4967 rec_len = hs->buffering.future_record.len;
4968 rec_epoch = hs->buffering.future_record.epoch;
4969
4970 if( rec == NULL )
4971 return( 0 );
4972
Hanno Becker4cb782d2018-08-20 11:19:05 +01004973 /* Only consider loading future records if the
4974 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004975 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004976 return( 0 );
4977
Hanno Becker5f066e72018-08-16 14:56:31 +01004978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4979
4980 if( rec_epoch != ssl->in_epoch )
4981 {
4982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4983 goto exit;
4984 }
4985
4986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4987
4988 /* Double-check that the record is not too large */
4989 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4990 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4991 {
4992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4994 }
4995
4996 memcpy( ssl->in_hdr, rec, rec_len );
4997 ssl->in_left = rec_len;
4998 ssl->next_record_offset = 0;
4999
5000 ssl_free_buffered_record( ssl );
5001
5002exit:
5003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5004 return( 0 );
5005}
5006
5007static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5008{
5009 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5010 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005011 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005012
5013 /* Don't buffer future records outside handshakes. */
5014 if( hs == NULL )
5015 return( 0 );
5016
5017 /* Only buffer handshake records (we are only interested
5018 * in Finished messages). */
5019 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5020 return( 0 );
5021
5022 /* Don't buffer more than one future epoch record. */
5023 if( hs->buffering.future_record.data != NULL )
5024 return( 0 );
5025
Hanno Becker01315ea2018-08-21 17:22:17 +01005026 /* Don't buffer record if there's not enough buffering space remaining. */
5027 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5028 hs->buffering.total_bytes_buffered ) )
5029 {
5030 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",
5031 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5032 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005033 return( 0 );
5034 }
5035
Hanno Becker5f066e72018-08-16 14:56:31 +01005036 /* Buffer record */
5037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5038 ssl->in_epoch + 1 ) );
5039 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5040 rec_hdr_len + ssl->in_msglen );
5041
5042 /* ssl_parse_record_header() only considers records
5043 * of the next epoch as candidates for buffering. */
5044 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005045 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005046
5047 hs->buffering.future_record.data =
5048 mbedtls_calloc( 1, hs->buffering.future_record.len );
5049 if( hs->buffering.future_record.data == NULL )
5050 {
5051 /* If we run out of RAM trying to buffer a
5052 * record from the next epoch, just ignore. */
5053 return( 0 );
5054 }
5055
Hanno Becker01315ea2018-08-21 17:22:17 +01005056 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005057
Hanno Becker01315ea2018-08-21 17:22:17 +01005058 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005059 return( 0 );
5060}
5061
5062#endif /* MBEDTLS_SSL_PROTO_DTLS */
5063
Hanno Beckere74d5562018-08-15 14:26:08 +01005064static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005065{
5066 int ret;
5067
Hanno Becker5f066e72018-08-16 14:56:31 +01005068#if defined(MBEDTLS_SSL_PROTO_DTLS)
5069 /* We might have buffered a future record; if so,
5070 * and if the epoch matches now, load it.
5071 * On success, this call will set ssl->in_left to
5072 * the length of the buffered record, so that
5073 * the calls to ssl_fetch_input() below will
5074 * essentially be no-ops. */
5075 ret = ssl_load_buffered_record( ssl );
5076 if( ret != 0 )
5077 return( ret );
5078#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005083 return( ret );
5084 }
5085
5086 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5090 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005091 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005092 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5093 {
5094 ret = ssl_buffer_future_record( ssl );
5095 if( ret != 0 )
5096 return( ret );
5097
5098 /* Fall through to handling of unexpected records */
5099 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5100 }
5101
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005102 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5103 {
5104 /* Skip unexpected record (but not whole datagram) */
5105 ssl->next_record_offset = ssl->in_msglen
5106 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005107
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5109 "(header)" ) );
5110 }
5111 else
5112 {
5113 /* Skip invalid record and the rest of the datagram */
5114 ssl->next_record_offset = 0;
5115 ssl->in_left = 0;
5116
5117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5118 "(header)" ) );
5119 }
5120
5121 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005122 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005123 }
5124#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005125 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005126 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005127
5128 /*
5129 * Read and optionally decrypt the message contents
5130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5132 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005135 return( ret );
5136 }
5137
5138 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005143 if( ssl->next_record_offset < ssl->in_left )
5144 {
5145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5146 }
5147 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005148 else
5149#endif
5150 ssl->in_left = 0;
5151
5152 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005156 {
5157 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5159 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005160 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005161 /* Except when waiting for Finished as a bad mac here
5162 * probably means something went wrong in the handshake
5163 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5164 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5165 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5166 {
5167#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5168 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5169 {
5170 mbedtls_ssl_send_alert_message( ssl,
5171 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5172 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5173 }
5174#endif
5175 return( ret );
5176 }
5177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005179 if( ssl->conf->badmac_limit != 0 &&
5180 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5183 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005184 }
5185#endif
5186
Hanno Becker4a810fb2017-05-24 16:27:30 +01005187 /* As above, invalid records cause
5188 * dismissal of the whole datagram. */
5189
5190 ssl->next_record_offset = 0;
5191 ssl->in_left = 0;
5192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005194 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005195 }
5196
5197 return( ret );
5198 }
5199 else
5200#endif
5201 {
5202 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5204 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005206 mbedtls_ssl_send_alert_message( ssl,
5207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5208 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005209 }
5210#endif
5211 return( ret );
5212 }
5213 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005214
Simon Butcher99000142016-10-13 17:21:01 +01005215 return( 0 );
5216}
5217
5218int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5219{
5220 int ret;
5221
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005222 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005223 * Handle particular types of records
5224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 {
Simon Butcher99000142016-10-13 17:21:01 +01005227 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5228 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005229 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 }
5232
Hanno Beckere678eaa2018-08-21 14:57:46 +01005233 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005234 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005235 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005236 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5238 ssl->in_msglen ) );
5239 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005240 }
5241
Hanno Beckere678eaa2018-08-21 14:57:46 +01005242 if( ssl->in_msg[0] != 1 )
5243 {
5244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5245 ssl->in_msg[0] ) );
5246 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5247 }
5248
5249#if defined(MBEDTLS_SSL_PROTO_DTLS)
5250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5251 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5252 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5253 {
5254 if( ssl->handshake == NULL )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5257 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5258 }
5259
5260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5261 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5262 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005263#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005264 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005268 if( ssl->in_msglen != 2 )
5269 {
5270 /* Note: Standard allows for more than one 2 byte alert
5271 to be packed in a single message, but Mbed TLS doesn't
5272 currently support this. */
5273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5274 ssl->in_msglen ) );
5275 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5276 }
5277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 ssl->in_msg[0], ssl->in_msg[1] ) );
5280
5281 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005282 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005287 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 }
5290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5292 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5295 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005297
5298#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5299 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5300 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5301 {
Hanno Becker90333da2017-10-10 11:27:13 +01005302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005303 /* Will be handled when trying to parse ServerHello */
5304 return( 0 );
5305 }
5306#endif
5307
5308#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5309 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5310 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5311 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5312 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5313 {
5314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5315 /* Will be handled in mbedtls_ssl_parse_certificate() */
5316 return( 0 );
5317 }
5318#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5319
5320 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005321 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 }
5323
Hanno Beckerc76c6192017-06-06 10:03:17 +01005324#if defined(MBEDTLS_SSL_PROTO_DTLS)
5325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5326 ssl->handshake != NULL &&
5327 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5328 {
5329 ssl_handshake_wrapup_free_hs_transform( ssl );
5330 }
5331#endif
5332
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 return( 0 );
5334}
5335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005337{
5338 int ret;
5339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5341 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5342 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005343 {
5344 return( ret );
5345 }
5346
5347 return( 0 );
5348}
5349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005351 unsigned char level,
5352 unsigned char message )
5353{
5354 int ret;
5355
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005356 if( ssl == NULL || ssl->conf == NULL )
5357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005363 ssl->out_msglen = 2;
5364 ssl->out_msg[0] = level;
5365 ssl->out_msg[1] = message;
5366
Hanno Becker67bc7c32018-08-06 11:33:50 +01005367 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005370 return( ret );
5371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005373
5374 return( 0 );
5375}
5376
Paul Bakker5121ce52009-01-03 21:22:43 +00005377/*
5378 * Handshake functions
5379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5381 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5382 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5383 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5384 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5385 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5386 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005387/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5395 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005396 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5397 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005400 ssl->state++;
5401 return( 0 );
5402 }
5403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005406}
5407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5415 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005416 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5417 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005420 ssl->state++;
5421 return( 0 );
5422 }
5423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005426}
Gilles Peskinef9828522017-05-03 12:28:43 +02005427
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005428#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005429/* Some certificate support -> implement write and parse */
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005434 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 const mbedtls_x509_crt *crt;
5436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5441 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005442 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5443 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005446 ssl->state++;
5447 return( 0 );
5448 }
5449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005451 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005452 {
5453 if( ssl->client_auth == 0 )
5454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 ssl->state++;
5457 return( 0 );
5458 }
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 /*
5462 * If using SSLv3 and got no cert, send an Alert message
5463 * (otherwise an empty Certificate message will be sent).
5464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5466 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 {
5468 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5470 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5471 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 goto write_msg;
5475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478#endif /* MBEDTLS_SSL_CLI_C */
5479#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005480 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5485 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 }
5487 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005488#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491
5492 /*
5493 * 0 . 0 handshake type
5494 * 1 . 3 handshake length
5495 * 4 . 6 length of all certs
5496 * 7 . 9 length of cert. 1
5497 * 10 . n-1 peer certificate
5498 * n . n+2 length of cert. 2
5499 * n+3 . ... upper level cert, etc.
5500 */
5501 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005503
Paul Bakker29087132010-03-21 21:03:34 +00005504 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 {
5506 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005507 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005510 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 }
5513
5514 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5515 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5516 ssl->out_msg[i + 2] = (unsigned char)( n );
5517
5518 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5519 i += n; crt = crt->next;
5520 }
5521
5522 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5523 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5524 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5525
5526 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5528 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005530#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005531write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005532#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
5534 ssl->state++;
5535
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005536 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005537 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 return( ret );
5540 }
5541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Paul Bakkered27a042013-04-18 22:46:23 +02005544 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545}
5546
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005547/*
5548 * Once the certificate message is read, parse it into a cert chain and
5549 * perform basic checks, but leave actual verification to the caller
5550 */
5551static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005552{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005553 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005554 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005555 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557#if defined(MBEDTLS_SSL_SRV_C)
5558#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 /*
5560 * Check if the client sent an empty certificate
5561 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005562 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005565 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5567 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5568 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005571
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005572 /* The client was asked for a certificate but didn't send
5573 one. The client should know what's going on, so we
5574 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005575 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005576 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 }
5578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5582 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005583 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5587 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5588 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5589 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005592
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005593 /* The client was asked for a certificate but didn't send
5594 one. The client should know what's going on, so we
5595 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005596 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005597 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005598 }
5599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5601 MBEDTLS_SSL_PROTO_TLS1_2 */
5602#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5608 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005610 }
5611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5613 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5617 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005622
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005625 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005626 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005627
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005628 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005632 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5633 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 }
5636
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005637 /* In case we tried to reuse a session but it failed */
5638 if( ssl->session_negotiate->peer_cert != NULL )
5639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5641 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005642 }
5643
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005644 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5650 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005651 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 }
5653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005655
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005656 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005657
5658 while( i < ssl->in_hslen )
5659 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005660 if ( i + 3 > ssl->in_hslen ) {
5661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5662 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5663 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5664 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 if( ssl->in_msg[i] != 0 )
5667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005669 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5670 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 }
5673
5674 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5675 | (unsigned int) ssl->in_msg[i + 2];
5676 i += 3;
5677
5678 if( n < 128 || i + n > ssl->in_hslen )
5679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005681 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5682 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 }
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005687 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005688 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005690 case 0: /*ok*/
5691 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5692 /* Ignore certificate with an unknown algorithm: maybe a
5693 prior certificate was already trusted. */
5694 break;
5695
5696 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5697 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5698 goto crt_parse_der_failed;
5699
5700 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5701 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5702 goto crt_parse_der_failed;
5703
5704 default:
5705 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5706 crt_parse_der_failed:
5707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 return( ret );
5710 }
5711
5712 i += n;
5713 }
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005716
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005717 /*
5718 * On client, make sure the server cert doesn't change during renego to
5719 * avoid "triple handshake" attack: https://secure-resumption.com/
5720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005722 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005724 {
5725 if( ssl->session->peer_cert == NULL )
5726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5729 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005731 }
5732
5733 if( ssl->session->peer_cert->raw.len !=
5734 ssl->session_negotiate->peer_cert->raw.len ||
5735 memcmp( ssl->session->peer_cert->raw.p,
5736 ssl->session_negotiate->peer_cert->raw.p,
5737 ssl->session->peer_cert->raw.len ) != 0 )
5738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5741 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005743 }
5744 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005746
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005747 return( 0 );
5748}
5749
5750int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5751{
5752 int ret;
5753 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5754 ssl->transform_negotiate->ciphersuite_info;
5755#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5756 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5757 ? ssl->handshake->sni_authmode
5758 : ssl->conf->authmode;
5759#else
5760 const int authmode = ssl->conf->authmode;
5761#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005762 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005763
5764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5765
5766 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5767 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5768 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5769 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5770 {
5771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5772 ssl->state++;
5773 return( 0 );
5774 }
5775
5776#if defined(MBEDTLS_SSL_SRV_C)
5777 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5778 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5779 {
5780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5781 ssl->state++;
5782 return( 0 );
5783 }
5784
5785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5786 authmode == MBEDTLS_SSL_VERIFY_NONE )
5787 {
5788 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005790
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005791 ssl->state++;
5792 return( 0 );
5793 }
5794#endif
5795
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005796#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5797 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005798 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005799 {
5800 goto crt_verify;
5801 }
5802#endif
5803
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005804 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005805 {
5806 /* mbedtls_ssl_read_record may have sent an alert already. We
5807 let it decide whether to alert. */
5808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5809 return( ret );
5810 }
5811
5812 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5813 {
5814#if defined(MBEDTLS_SSL_SRV_C)
5815 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5816 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5817 {
5818 ret = 0;
5819 }
5820#endif
5821
5822 ssl->state++;
5823 return( ret );
5824 }
5825
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005826#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5827 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005828 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005829
5830crt_verify:
5831 if( ssl->handshake->ecrs_enabled)
5832 rs_ctx = &ssl->handshake->ecrs_ctx;
5833#endif
5834
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005835 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005837 mbedtls_x509_crt *ca_chain;
5838 mbedtls_x509_crl *ca_crl;
5839
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005840#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005841 if( ssl->handshake->sni_ca_chain != NULL )
5842 {
5843 ca_chain = ssl->handshake->sni_ca_chain;
5844 ca_crl = ssl->handshake->sni_ca_crl;
5845 }
5846 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005847#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005848 {
5849 ca_chain = ssl->conf->ca_chain;
5850 ca_crl = ssl->conf->ca_crl;
5851 }
5852
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005853 /*
5854 * Main check: verify certificate
5855 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005856 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005857 ssl->session_negotiate->peer_cert,
5858 ca_chain, ca_crl,
5859 ssl->conf->cert_profile,
5860 ssl->hostname,
5861 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005862 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005863
5864 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005867 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005868
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005869#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5870 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005871 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005872#endif
5873
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005874 /*
5875 * Secondary checks: always done, but change 'ret' only if it was 0
5876 */
5877
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005878#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005881
5882 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005884 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005885 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005886 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005889 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005891 }
5892 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005893#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005896 ciphersuite_info,
5897 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005898 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005901 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005903 }
5904
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005905 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5906 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5907 * with details encoded in the verification flags. All other kinds
5908 * of error codes, including those from the user provided f_vrfy
5909 * functions, are treated as fatal and lead to a failure of
5910 * ssl_parse_certificate even if verification was optional. */
5911 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5912 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5913 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5914 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005916 }
5917
5918 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5919 {
5920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5921 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5922 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005923
5924 if( ret != 0 )
5925 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005926 uint8_t alert;
5927
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005928 /* The certificate may have been rejected for several reasons.
5929 Pick one and send the corresponding alert. Which alert to send
5930 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005931 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5932 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5933 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5934 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5935 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5936 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5937 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5938 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5939 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5940 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5941 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5942 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5943 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5944 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5945 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5946 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5947 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5948 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5949 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5950 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005951 else
5952 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005953 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5954 alert );
5955 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005956
Hanno Beckere6706e62017-05-15 16:05:15 +01005957#if defined(MBEDTLS_DEBUG_C)
5958 if( ssl->session_negotiate->verify_result != 0 )
5959 {
5960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5961 ssl->session_negotiate->verify_result ) );
5962 }
5963 else
5964 {
5965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5966 }
5967#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 }
5969
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005970 ssl->state++;
5971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005973
5974 return( ret );
5975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5977 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5978 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5979 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5980 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5981 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5982 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005985{
5986 int ret;
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 ssl->out_msglen = 1;
5992 ssl->out_msg[0] = 1;
5993
Paul Bakker5121ce52009-01-03 21:22:43 +00005994 ssl->state++;
5995
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005996 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005997 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005999 return( ret );
6000 }
6001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006003
6004 return( 0 );
6005}
6006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006008{
6009 int ret;
6010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012
Hanno Becker327c93b2018-08-15 13:56:18 +01006013 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 return( ret );
6017 }
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006022 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6023 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006025 }
6026
Hanno Beckere678eaa2018-08-21 14:57:46 +01006027 /* CCS records are only accepted if they have length 1 and content '1',
6028 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006030 /*
6031 * Switch to our negotiated transform and session parameters for inbound
6032 * data.
6033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006035 ssl->transform_in = ssl->transform_negotiate;
6036 ssl->session_in = ssl->session_negotiate;
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006042 ssl_dtls_replay_reset( ssl );
6043#endif
6044
6045 /* Increment epoch */
6046 if( ++ssl->in_epoch == 0 )
6047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006049 /* This is highly unlikely to happen for legitimate reasons, so
6050 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006052 }
6053 }
6054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006056 memset( ssl->in_ctr, 0, 8 );
6057
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006058 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6061 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6067 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006069 }
6070 }
6071#endif
6072
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 ssl->state++;
6074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006076
6077 return( 0 );
6078}
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6081 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006082{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006083 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6086 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6087 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006088 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006089 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6092#if defined(MBEDTLS_SHA512_C)
6093 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006094 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6095 else
6096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#if defined(MBEDTLS_SHA256_C)
6098 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006099 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006100 else
6101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006105 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006106 }
Paul Bakker380da532012-04-18 16:10:25 +00006107}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6112 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006113 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6114 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6117#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006118 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006121 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006124}
6125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006127 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6130 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006131 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6132 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006133#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6135#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006136 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006139 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006142}
6143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6145 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6146static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006147 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006148{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006149 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6150 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006151}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006152#endif
Paul Bakker380da532012-04-18 16:10:25 +00006153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6155#if defined(MBEDTLS_SHA256_C)
6156static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006157 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006158{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006159 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006160}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006161#endif
Paul Bakker380da532012-04-18 16:10:25 +00006162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163#if defined(MBEDTLS_SHA512_C)
6164static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006165 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006166{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006167 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006168}
Paul Bakker769075d2012-11-24 11:26:46 +01006169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006173static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006175{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006176 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006177 mbedtls_md5_context md5;
6178 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006179
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 unsigned char padbuf[48];
6181 unsigned char md5sum[16];
6182 unsigned char sha1sum[20];
6183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006185 if( !session )
6186 session = ssl->session;
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006189
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006190 mbedtls_md5_init( &md5 );
6191 mbedtls_sha1_init( &sha1 );
6192
6193 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6194 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195
6196 /*
6197 * SSLv3:
6198 * hash =
6199 * MD5( master + pad2 +
6200 * MD5( handshake + sender + master + pad1 ) )
6201 * + SHA1( master + pad2 +
6202 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 */
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006206 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6207 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006211 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6212 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006216 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006217
Paul Bakker1ef83d62012-04-11 12:09:53 +00006218 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006220 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6221 mbedtls_md5_update_ret( &md5, session->master, 48 );
6222 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6223 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006225 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6226 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6227 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6228 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229
Paul Bakker1ef83d62012-04-11 12:09:53 +00006230 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006232 mbedtls_md5_starts_ret( &md5 );
6233 mbedtls_md5_update_ret( &md5, session->master, 48 );
6234 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6235 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6236 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006237
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006238 mbedtls_sha1_starts_ret( &sha1 );
6239 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6240 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6241 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6242 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006245
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006246 mbedtls_md5_free( &md5 );
6247 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006249 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6250 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6251 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006258static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006261 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006262 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006263 mbedtls_md5_context md5;
6264 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006265 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006268 if( !session )
6269 session = ssl->session;
6270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006272
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006273 mbedtls_md5_init( &md5 );
6274 mbedtls_sha1_init( &sha1 );
6275
6276 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6277 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
Paul Bakker1ef83d62012-04-11 12:09:53 +00006279 /*
6280 * TLSv1:
6281 * hash = PRF( master, finished_label,
6282 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6283 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006286 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6287 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006288#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006291 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6292 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006293#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006296 ? "client finished"
6297 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006298
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006299 mbedtls_md5_finish_ret( &md5, padbuf );
6300 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006302 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006303 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006306
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006307 mbedtls_md5_free( &md5 );
6308 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006309
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006310 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6317#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006318static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006320{
6321 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006322 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006323 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006324 unsigned char padbuf[32];
6325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006327 if( !session )
6328 session = ssl->session;
6329
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006330 mbedtls_sha256_init( &sha256 );
6331
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006333
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006334 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006335
6336 /*
6337 * TLSv1.2:
6338 * hash = PRF( master, finished_label,
6339 * Hash( handshake ) )[0.11]
6340 */
6341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342#if !defined(MBEDTLS_SHA256_ALT)
6343 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006344 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006345#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006348 ? "client finished"
6349 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006351 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006352
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006353 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006354 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006357
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006358 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006359
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006360 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006367static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006369{
6370 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006371 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006372 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006373 unsigned char padbuf[48];
6374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006376 if( !session )
6377 session = ssl->session;
6378
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006379 mbedtls_sha512_init( &sha512 );
6380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006382
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006383 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006384
6385 /*
6386 * TLSv1.2:
6387 * hash = PRF( master, finished_label,
6388 * Hash( handshake ) )[0.11]
6389 */
6390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006392 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6393 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006394#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006397 ? "client finished"
6398 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006400 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006401
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006402 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006403 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006406
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006407 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006408
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006409 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#endif /* MBEDTLS_SHA512_C */
6414#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006419
6420 /*
6421 * Free our handshake params
6422 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006423 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006425 ssl->handshake = NULL;
6426
6427 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006428 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006429 */
6430 if( ssl->transform )
6431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 mbedtls_ssl_transform_free( ssl->transform );
6433 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006434 }
6435 ssl->transform = ssl->transform_negotiate;
6436 ssl->transform_negotiate = NULL;
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006439}
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006442{
6443 int resume = ssl->handshake->resume;
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447#if defined(MBEDTLS_SSL_RENEGOTIATION)
6448 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006451 ssl->renego_records_seen = 0;
6452 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006453#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006454
6455 /*
6456 * Free the previous session and switch in the current one
6457 */
Paul Bakker0a597072012-09-25 21:55:46 +00006458 if( ssl->session )
6459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006461 /* RFC 7366 3.1: keep the EtM state */
6462 ssl->session_negotiate->encrypt_then_mac =
6463 ssl->session->encrypt_then_mac;
6464#endif
6465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 mbedtls_ssl_session_free( ssl->session );
6467 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006468 }
6469 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006470 ssl->session_negotiate = NULL;
6471
Paul Bakker0a597072012-09-25 21:55:46 +00006472 /*
6473 * Add cache entry
6474 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006475 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006476 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006477 resume == 0 )
6478 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006479 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006481 }
Paul Bakker0a597072012-09-25 21:55:46 +00006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006484 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006485 ssl->handshake->flight != NULL )
6486 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006487 /* Cancel handshake timer */
6488 ssl_set_timer( ssl, 0 );
6489
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006490 /* Keep last flight around in case we need to resend it:
6491 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006493 }
6494 else
6495#endif
6496 ssl_handshake_wrapup_free_hs_transform( ssl );
6497
Paul Bakker48916f92012-09-16 19:57:18 +00006498 ssl->state++;
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006501}
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006504{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006505 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006508
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006509 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006510
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006511 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006512
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006513 /*
6514 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6515 * may define some other value. Currently (early 2016), no defined
6516 * ciphersuite does this (and this is unlikely to change as activity has
6517 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006522 ssl->verify_data_len = hash_len;
6523 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006524#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006525
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6528 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006529
6530 /*
6531 * In case of session resuming, invert the client and server
6532 * ChangeCipherSpec messages order.
6533 */
Paul Bakker0a597072012-09-25 21:55:46 +00006534 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 }
6545 else
6546 ssl->state++;
6547
Paul Bakker48916f92012-09-16 19:57:18 +00006548 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006549 * Switch to our negotiated transform and session parameters for outbound
6550 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006556 {
6557 unsigned char i;
6558
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006559 /* Remember current epoch settings for resending */
6560 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006561 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006562
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006563 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006564 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006565
6566 /* Increment epoch */
6567 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006568 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006569 break;
6570
6571 /* The loop goes to its end iff the counter is wrapping */
6572 if( i == 0 )
6573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6575 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006576 }
6577 }
6578 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006580 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006582 ssl->transform_out = ssl->transform_negotiate;
6583 ssl->session_out = ssl->session_negotiate;
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6586 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6591 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006592 }
6593 }
6594#endif
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006599#endif
6600
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006601 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 return( ret );
6605 }
6606
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006607#if defined(MBEDTLS_SSL_PROTO_DTLS)
6608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6609 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6610 {
6611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6612 return( ret );
6613 }
6614#endif
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617
6618 return( 0 );
6619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006622#define SSL_MAX_HASH_LEN 36
6623#else
6624#define SSL_MAX_HASH_LEN 12
6625#endif
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006628{
Paul Bakker23986e52011-04-24 08:57:21 +00006629 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006630 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006631 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006635 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Hanno Becker327c93b2018-08-15 13:56:18 +01006637 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 return( ret );
6641 }
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006646 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 }
6650
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006651 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SSL_PROTO_SSL3)
6653 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006654 hash_len = 36;
6655 else
6656#endif
6657 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6660 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6664 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 }
6667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006669 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006672 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6673 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 }
6676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006678 ssl->verify_data_len = hash_len;
6679 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006680#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006681
Paul Bakker0a597072012-09-25 21:55:46 +00006682 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006687#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006689 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006691#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 }
6693 else
6694 ssl->state++;
6695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006699#endif
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006702
6703 return( 0 );
6704}
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6711 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6712 mbedtls_md5_init( &handshake->fin_md5 );
6713 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006714 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6715 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6718#if defined(MBEDTLS_SHA256_C)
6719 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006720 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_SHA512_C)
6723 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006724 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006727
6728 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006729
6730#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6731 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6732 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6733#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735#if defined(MBEDTLS_DHM_C)
6736 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_ECDH_C)
6739 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006740#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006741#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006742 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006743#if defined(MBEDTLS_SSL_CLI_C)
6744 handshake->ecjpake_cache = NULL;
6745 handshake->ecjpake_cache_len = 0;
6746#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006747#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006748
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006749#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006750 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006751#endif
6752
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006753#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6754 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6755#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006756}
6757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6763 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 mbedtls_md_init( &transform->md_ctx_enc );
6766 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006767}
6768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006772}
6773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006775{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006776 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006777 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006779 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006781 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006782 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006783
6784 /*
6785 * Either the pointers are now NULL or cleared properly and can be freed.
6786 * Now allocate missing structures.
6787 */
6788 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006789 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006790 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006791 }
Paul Bakker48916f92012-09-16 19:57:18 +00006792
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006793 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006794 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006795 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006796 }
Paul Bakker48916f92012-09-16 19:57:18 +00006797
Paul Bakker82788fb2014-10-20 13:59:19 +02006798 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006799 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006800 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006801 }
Paul Bakker48916f92012-09-16 19:57:18 +00006802
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006803 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006804 if( ssl->handshake == NULL ||
6805 ssl->transform_negotiate == NULL ||
6806 ssl->session_negotiate == NULL )
6807 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 mbedtls_free( ssl->handshake );
6811 mbedtls_free( ssl->transform_negotiate );
6812 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006813
6814 ssl->handshake = NULL;
6815 ssl->transform_negotiate = NULL;
6816 ssl->session_negotiate = NULL;
6817
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006819 }
6820
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006821 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006823 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006824 ssl_handshake_params_init( ssl->handshake );
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6828 {
6829 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006830
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6832 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6833 else
6834 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006835
6836 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006837 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006838#endif
6839
Paul Bakker48916f92012-09-16 19:57:18 +00006840 return( 0 );
6841}
6842
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006843#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006844/* Dummy cookie callbacks for defaults */
6845static int ssl_cookie_write_dummy( void *ctx,
6846 unsigned char **p, unsigned char *end,
6847 const unsigned char *cli_id, size_t cli_id_len )
6848{
6849 ((void) ctx);
6850 ((void) p);
6851 ((void) end);
6852 ((void) cli_id);
6853 ((void) cli_id_len);
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006856}
6857
6858static int ssl_cookie_check_dummy( void *ctx,
6859 const unsigned char *cookie, size_t cookie_len,
6860 const unsigned char *cli_id, size_t cli_id_len )
6861{
6862 ((void) ctx);
6863 ((void) cookie);
6864 ((void) cookie_len);
6865 ((void) cli_id);
6866 ((void) cli_id_len);
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006869}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006870#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006871
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006872/* Once ssl->out_hdr as the address of the beginning of the
6873 * next outgoing record is set, deduce the other pointers.
6874 *
6875 * Note: For TLS, we save the implicit record sequence number
6876 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6877 * and the caller has to make sure there's space for this.
6878 */
6879
6880static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6881 mbedtls_ssl_transform *transform )
6882{
6883#if defined(MBEDTLS_SSL_PROTO_DTLS)
6884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6885 {
6886 ssl->out_ctr = ssl->out_hdr + 3;
6887 ssl->out_len = ssl->out_hdr + 11;
6888 ssl->out_iv = ssl->out_hdr + 13;
6889 }
6890 else
6891#endif
6892 {
6893 ssl->out_ctr = ssl->out_hdr - 8;
6894 ssl->out_len = ssl->out_hdr + 3;
6895 ssl->out_iv = ssl->out_hdr + 5;
6896 }
6897
6898 /* Adjust out_msg to make space for explicit IV, if used. */
6899 if( transform != NULL &&
6900 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6901 {
6902 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6903 }
6904 else
6905 ssl->out_msg = ssl->out_iv;
6906}
6907
6908/* Once ssl->in_hdr as the address of the beginning of the
6909 * next incoming record is set, deduce the other pointers.
6910 *
6911 * Note: For TLS, we save the implicit record sequence number
6912 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6913 * and the caller has to make sure there's space for this.
6914 */
6915
6916static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6917 mbedtls_ssl_transform *transform )
6918{
6919#if defined(MBEDTLS_SSL_PROTO_DTLS)
6920 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6921 {
6922 ssl->in_ctr = ssl->in_hdr + 3;
6923 ssl->in_len = ssl->in_hdr + 11;
6924 ssl->in_iv = ssl->in_hdr + 13;
6925 }
6926 else
6927#endif
6928 {
6929 ssl->in_ctr = ssl->in_hdr - 8;
6930 ssl->in_len = ssl->in_hdr + 3;
6931 ssl->in_iv = ssl->in_hdr + 5;
6932 }
6933
6934 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6935 if( transform != NULL &&
6936 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6937 {
6938 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6939 }
6940 else
6941 ssl->in_msg = ssl->in_iv;
6942}
6943
Paul Bakker5121ce52009-01-03 21:22:43 +00006944/*
6945 * Initialize an SSL context
6946 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006947void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6948{
6949 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6950}
6951
6952/*
6953 * Setup an SSL context
6954 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006955
6956static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6957{
6958 /* Set the incoming and outgoing record pointers. */
6959#if defined(MBEDTLS_SSL_PROTO_DTLS)
6960 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6961 {
6962 ssl->out_hdr = ssl->out_buf;
6963 ssl->in_hdr = ssl->in_buf;
6964 }
6965 else
6966#endif /* MBEDTLS_SSL_PROTO_DTLS */
6967 {
6968 ssl->out_hdr = ssl->out_buf + 8;
6969 ssl->in_hdr = ssl->in_buf + 8;
6970 }
6971
6972 /* Derive other internal pointers. */
6973 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6974 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6975}
6976
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006977int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006978 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979{
Paul Bakker48916f92012-09-16 19:57:18 +00006980 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006981
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006982 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006983
6984 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006985 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006986 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006987
6988 /* Set to NULL in case of an error condition */
6989 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006990
Angus Grattond8213d02016-05-25 20:56:48 +10006991 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6992 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006993 {
Angus Grattond8213d02016-05-25 20:56:48 +10006994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006995 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006996 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006997 }
6998
6999 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7000 if( ssl->out_buf == NULL )
7001 {
7002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007003 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007004 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007005 }
7006
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007007 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007008
Paul Bakker48916f92012-09-16 19:57:18 +00007009 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007010 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
7012 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007013
7014error:
7015 mbedtls_free( ssl->in_buf );
7016 mbedtls_free( ssl->out_buf );
7017
7018 ssl->conf = NULL;
7019
7020 ssl->in_buf = NULL;
7021 ssl->out_buf = NULL;
7022
7023 ssl->in_hdr = NULL;
7024 ssl->in_ctr = NULL;
7025 ssl->in_len = NULL;
7026 ssl->in_iv = NULL;
7027 ssl->in_msg = NULL;
7028
7029 ssl->out_hdr = NULL;
7030 ssl->out_ctr = NULL;
7031 ssl->out_len = NULL;
7032 ssl->out_iv = NULL;
7033 ssl->out_msg = NULL;
7034
k-stachowiak9f7798e2018-07-31 16:52:32 +02007035 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007036}
7037
7038/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007039 * Reset an initialized and used SSL context for re-use while retaining
7040 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007041 *
7042 * If partial is non-zero, keep data in the input buffer and client ID.
7043 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007044 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007045static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007046{
Paul Bakker48916f92012-09-16 19:57:18 +00007047 int ret;
7048
Hanno Becker7e772132018-08-10 12:38:21 +01007049#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7050 !defined(MBEDTLS_SSL_SRV_C)
7051 ((void) partial);
7052#endif
7053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007055
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007056 /* Cancel any possibly running timer */
7057 ssl_set_timer( ssl, 0 );
7058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#if defined(MBEDTLS_SSL_RENEGOTIATION)
7060 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007061 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007062
7063 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7065 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007068
Paul Bakker7eb013f2011-10-06 12:37:39 +00007069 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007070 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007071
7072 ssl->in_msgtype = 0;
7073 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007075 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007076 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007079 ssl_dtls_replay_reset( ssl );
7080#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007081
7082 ssl->in_hslen = 0;
7083 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007084
7085 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007086
7087 ssl->out_msgtype = 0;
7088 ssl->out_msglen = 0;
7089 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7091 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007092 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007093#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007094
Hanno Becker19859472018-08-06 09:40:20 +01007095 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7096
Paul Bakker48916f92012-09-16 19:57:18 +00007097 ssl->transform_in = NULL;
7098 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007099
Hanno Becker78640902018-08-13 16:35:15 +01007100 ssl->session_in = NULL;
7101 ssl->session_out = NULL;
7102
Angus Grattond8213d02016-05-25 20:56:48 +10007103 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007104
7105#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007106 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007107#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7108 {
7109 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007110 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007111 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7114 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7117 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7120 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007121 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007122 }
7123#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007124
Paul Bakker48916f92012-09-16 19:57:18 +00007125 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 mbedtls_ssl_transform_free( ssl->transform );
7128 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007129 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007130 }
Paul Bakker48916f92012-09-16 19:57:18 +00007131
Paul Bakkerc0463502013-02-14 11:19:38 +01007132 if( ssl->session )
7133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 mbedtls_ssl_session_free( ssl->session );
7135 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007136 ssl->session = NULL;
7137 }
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007140 ssl->alpn_chosen = NULL;
7141#endif
7142
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007143#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007144#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007145 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007146#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007147 {
7148 mbedtls_free( ssl->cli_id );
7149 ssl->cli_id = NULL;
7150 ssl->cli_id_len = 0;
7151 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007152#endif
7153
Paul Bakker48916f92012-09-16 19:57:18 +00007154 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7155 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007156
7157 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007158}
7159
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007160/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007161 * Reset an initialized and used SSL context for re-use while retaining
7162 * all application-set variables, function pointers and data.
7163 */
7164int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7165{
7166 return( ssl_session_reset_int( ssl, 0 ) );
7167}
7168
7169/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 * SSL set accessors
7171 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007172void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007174 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007175}
7176
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007177void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007179 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007180}
7181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007183void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007184{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007185 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007186}
7187#endif
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007190void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007191{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007192 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007193}
7194#endif
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007197
Hanno Becker1841b0a2018-08-24 11:13:57 +01007198void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7199 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007200{
7201 ssl->disable_datagram_packing = !allow_packing;
7202}
7203
7204void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7205 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007207 conf->hs_timeout_min = min;
7208 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007209}
7210#endif
7211
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007212void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007214 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007215}
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007218void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007219 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007220 void *p_vrfy )
7221{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007222 conf->f_vrfy = f_vrfy;
7223 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007226
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007227void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007228 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 void *p_rng )
7230{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007231 conf->f_rng = f_rng;
7232 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007233}
7234
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007235void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007236 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 void *p_dbg )
7238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007239 conf->f_dbg = f_dbg;
7240 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007241}
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007244 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007245 mbedtls_ssl_send_t *f_send,
7246 mbedtls_ssl_recv_t *f_recv,
7247 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007248{
7249 ssl->p_bio = p_bio;
7250 ssl->f_send = f_send;
7251 ssl->f_recv = f_recv;
7252 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007253}
7254
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007255#if defined(MBEDTLS_SSL_PROTO_DTLS)
7256void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7257{
7258 ssl->mtu = mtu;
7259}
7260#endif
7261
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007262void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007263{
7264 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007265}
7266
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007267void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7268 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007269 mbedtls_ssl_set_timer_t *f_set_timer,
7270 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007271{
7272 ssl->p_timer = p_timer;
7273 ssl->f_set_timer = f_set_timer;
7274 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007275
7276 /* Make sure we start with no timer running */
7277 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007278}
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007281void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007282 void *p_cache,
7283 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7284 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007285{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007286 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007287 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007288 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#if defined(MBEDTLS_SSL_CLI_C)
7293int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007294{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007295 int ret;
7296
7297 if( ssl == NULL ||
7298 session == NULL ||
7299 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007300 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007303 }
7304
7305 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7306 return( ret );
7307
Paul Bakker0a597072012-09-25 21:55:46 +00007308 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007309
7310 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007314void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007315 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007316{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007317 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7318 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7319 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7320 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007321}
7322
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007323void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007324 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007325 int major, int minor )
7326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007328 return;
7329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007331 return;
7332
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007333 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007334}
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007337void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007338 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007339{
7340 conf->cert_profile = profile;
7341}
7342
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007343/* Append a new keycert entry to a (possibly empty) list */
7344static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7345 mbedtls_x509_crt *cert,
7346 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007347{
niisato8ee24222018-06-25 19:05:48 +09007348 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007349
niisato8ee24222018-06-25 19:05:48 +09007350 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7351 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007352 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007353
niisato8ee24222018-06-25 19:05:48 +09007354 new_cert->cert = cert;
7355 new_cert->key = key;
7356 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007357
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007358 /* Update head is the list was null, else add to the end */
7359 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007360 {
niisato8ee24222018-06-25 19:05:48 +09007361 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007362 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007363 else
7364 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007365 mbedtls_ssl_key_cert *cur = *head;
7366 while( cur->next != NULL )
7367 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007368 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007369 }
7370
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007371 return( 0 );
7372}
7373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007374int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007375 mbedtls_x509_crt *own_cert,
7376 mbedtls_pk_context *pk_key )
7377{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007378 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007379}
7380
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007381void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007382 mbedtls_x509_crt *ca_chain,
7383 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007384{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007385 conf->ca_chain = ca_chain;
7386 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007389
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007390#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7391int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7392 mbedtls_x509_crt *own_cert,
7393 mbedtls_pk_context *pk_key )
7394{
7395 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7396 own_cert, pk_key ) );
7397}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007398
7399void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7400 mbedtls_x509_crt *ca_chain,
7401 mbedtls_x509_crl *ca_crl )
7402{
7403 ssl->handshake->sni_ca_chain = ca_chain;
7404 ssl->handshake->sni_ca_crl = ca_crl;
7405}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007406
7407void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7408 int authmode )
7409{
7410 ssl->handshake->sni_authmode = authmode;
7411}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007412#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7413
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007414#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007415/*
7416 * Set EC J-PAKE password for current handshake
7417 */
7418int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7419 const unsigned char *pw,
7420 size_t pw_len )
7421{
7422 mbedtls_ecjpake_role role;
7423
Janos Follath8eb64132016-06-03 15:40:57 +01007424 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7426
7427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7428 role = MBEDTLS_ECJPAKE_SERVER;
7429 else
7430 role = MBEDTLS_ECJPAKE_CLIENT;
7431
7432 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7433 role,
7434 MBEDTLS_MD_SHA256,
7435 MBEDTLS_ECP_DP_SECP256R1,
7436 pw, pw_len ) );
7437}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007438#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007441int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007442 const unsigned char *psk, size_t psk_len,
7443 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007444{
Paul Bakker6db455e2013-09-18 17:29:31 +02007445 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007450
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007451 /* Identity len will be encoded on two bytes */
7452 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007453 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007454 {
7455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7456 }
7457
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007458 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007459 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007460 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007461
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007462 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007463 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007464 conf->psk_len = 0;
7465 }
7466 if( conf->psk_identity != NULL )
7467 {
7468 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007469 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007470 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007471 }
7472
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007473 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7474 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007475 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007476 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007477 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007478 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007479 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007480 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007481 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007482
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007483 conf->psk_len = psk_len;
7484 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007485
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007486 memcpy( conf->psk, psk, conf->psk_len );
7487 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007488
7489 return( 0 );
7490}
7491
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007492int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7493 const unsigned char *psk, size_t psk_len )
7494{
7495 if( psk == NULL || ssl->handshake == NULL )
7496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7497
7498 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7500
7501 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007502 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007503 mbedtls_platform_zeroize( ssl->handshake->psk,
7504 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007505 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007506 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007507 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007508
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007509 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007510 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007511
7512 ssl->handshake->psk_len = psk_len;
7513 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7514
7515 return( 0 );
7516}
7517
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007518void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007520 size_t),
7521 void *p_psk )
7522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007523 conf->f_psk = f_psk;
7524 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007527
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007528#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007529
7530#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007531int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007532{
7533 int ret;
7534
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007535 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7536 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7537 {
7538 mbedtls_mpi_free( &conf->dhm_P );
7539 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007540 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
7543 return( 0 );
7544}
Hanno Becker470a8c42017-10-04 15:28:46 +01007545#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
Hanno Beckera90658f2017-10-04 15:29:08 +01007547int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7548 const unsigned char *dhm_P, size_t P_len,
7549 const unsigned char *dhm_G, size_t G_len )
7550{
7551 int ret;
7552
7553 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7554 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7555 {
7556 mbedtls_mpi_free( &conf->dhm_P );
7557 mbedtls_mpi_free( &conf->dhm_G );
7558 return( ret );
7559 }
7560
7561 return( 0 );
7562}
Paul Bakker5121ce52009-01-03 21:22:43 +00007563
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007564int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007565{
7566 int ret;
7567
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007568 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7569 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7570 {
7571 mbedtls_mpi_free( &conf->dhm_P );
7572 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007573 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007574 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007575
7576 return( 0 );
7577}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007578#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007579
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007580#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7581/*
7582 * Set the minimum length for Diffie-Hellman parameters
7583 */
7584void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7585 unsigned int bitlen )
7586{
7587 conf->dhm_min_bitlen = bitlen;
7588}
7589#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7590
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007591#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007592/*
7593 * Set allowed/preferred hashes for handshake signatures
7594 */
7595void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7596 const int *hashes )
7597{
7598 conf->sig_hashes = hashes;
7599}
Hanno Becker947194e2017-04-07 13:25:49 +01007600#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007601
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007602#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007603/*
7604 * Set the allowed elliptic curves
7605 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007606void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007607 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007609 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007610}
Hanno Becker947194e2017-04-07 13:25:49 +01007611#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007612
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007613#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007615{
Hanno Becker947194e2017-04-07 13:25:49 +01007616 /* Initialize to suppress unnecessary compiler warning */
7617 size_t hostname_len = 0;
7618
7619 /* Check if new hostname is valid before
7620 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007621 if( hostname != NULL )
7622 {
7623 hostname_len = strlen( hostname );
7624
7625 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7627 }
7628
7629 /* Now it's clear that we will overwrite the old hostname,
7630 * so we can free it safely */
7631
7632 if( ssl->hostname != NULL )
7633 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007634 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007635 mbedtls_free( ssl->hostname );
7636 }
7637
7638 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007639
Paul Bakker5121ce52009-01-03 21:22:43 +00007640 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007641 {
7642 ssl->hostname = NULL;
7643 }
7644 else
7645 {
7646 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007647 if( ssl->hostname == NULL )
7648 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007649
Hanno Becker947194e2017-04-07 13:25:49 +01007650 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007651
Hanno Becker947194e2017-04-07 13:25:49 +01007652 ssl->hostname[hostname_len] = '\0';
7653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007654
7655 return( 0 );
7656}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007657#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007658
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007659#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007660void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007662 const unsigned char *, size_t),
7663 void *p_sni )
7664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007665 conf->f_sni = f_sni;
7666 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007671int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007672{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007673 size_t cur_len, tot_len;
7674 const char **p;
7675
7676 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007677 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7678 * MUST NOT be truncated."
7679 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007680 */
7681 tot_len = 0;
7682 for( p = protos; *p != NULL; p++ )
7683 {
7684 cur_len = strlen( *p );
7685 tot_len += cur_len;
7686
Ronald Cron157cffe2020-04-23 16:41:44 +02007687 if( ( cur_len == 0 ) ||
7688 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7689 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007691 }
7692
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007693 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007694
7695 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007696}
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007699{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007700 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007703
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007704void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007706 conf->max_major_ver = major;
7707 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007708}
7709
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007710void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007712 conf->min_major_ver = major;
7713 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007714}
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007717void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007718{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007719 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007720}
7721#endif
7722
Janos Follath088ce432017-04-10 12:42:31 +01007723#if defined(MBEDTLS_SSL_SRV_C)
7724void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7725 char cert_req_ca_list )
7726{
7727 conf->cert_req_ca_list = cert_req_ca_list;
7728}
7729#endif
7730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007732void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007734 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007735}
7736#endif
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007739void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007740{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007741 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007742}
7743#endif
7744
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007745#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007746void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007748 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007749}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007750#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007753int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007756 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007759 }
7760
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007761 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007762
7763 return( 0 );
7764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007768void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007769{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007770 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007775void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007776{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007777 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007778}
7779#endif
7780
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007781void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007782{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007783 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007784}
7785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007787void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007788{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007789 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007790}
7791
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007792void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007793{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007794 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007795}
7796
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007797void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007798 const unsigned char period[8] )
7799{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007800 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007805#if defined(MBEDTLS_SSL_CLI_C)
7806void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007807{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007808 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007809}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007810#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007811
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007812#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007813void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7814 mbedtls_ssl_ticket_write_t *f_ticket_write,
7815 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7816 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007817{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007818 conf->f_ticket_write = f_ticket_write;
7819 conf->f_ticket_parse = f_ticket_parse;
7820 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007821}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007824
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007825#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7826void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7827 mbedtls_ssl_export_keys_t *f_export_keys,
7828 void *p_export_keys )
7829{
7830 conf->f_export_keys = f_export_keys;
7831 conf->p_export_keys = p_export_keys;
7832}
7833#endif
7834
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007835#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007836void mbedtls_ssl_conf_async_private_cb(
7837 mbedtls_ssl_config *conf,
7838 mbedtls_ssl_async_sign_t *f_async_sign,
7839 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7840 mbedtls_ssl_async_resume_t *f_async_resume,
7841 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007842 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007843{
7844 conf->f_async_sign_start = f_async_sign;
7845 conf->f_async_decrypt_start = f_async_decrypt;
7846 conf->f_async_resume = f_async_resume;
7847 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007848 conf->p_async_config_data = async_config_data;
7849}
7850
Gilles Peskine8f97af72018-04-26 11:46:10 +02007851void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7852{
7853 return( conf->p_async_config_data );
7854}
7855
Gilles Peskine1febfef2018-04-30 11:54:39 +02007856void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007857{
7858 if( ssl->handshake == NULL )
7859 return( NULL );
7860 else
7861 return( ssl->handshake->user_async_ctx );
7862}
7863
Gilles Peskine1febfef2018-04-30 11:54:39 +02007864void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007865 void *ctx )
7866{
7867 if( ssl->handshake != NULL )
7868 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007869}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007870#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007871
Paul Bakker5121ce52009-01-03 21:22:43 +00007872/*
7873 * SSL get accessors
7874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876{
7877 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7878}
7879
Hanno Becker8b170a02017-10-10 11:51:19 +01007880int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7881{
7882 /*
7883 * Case A: We're currently holding back
7884 * a message for further processing.
7885 */
7886
7887 if( ssl->keep_current_message == 1 )
7888 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007890 return( 1 );
7891 }
7892
7893 /*
7894 * Case B: Further records are pending in the current datagram.
7895 */
7896
7897#if defined(MBEDTLS_SSL_PROTO_DTLS)
7898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7899 ssl->in_left > ssl->next_record_offset )
7900 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007902 return( 1 );
7903 }
7904#endif /* MBEDTLS_SSL_PROTO_DTLS */
7905
7906 /*
7907 * Case C: A handshake message is being processed.
7908 */
7909
Hanno Becker8b170a02017-10-10 11:51:19 +01007910 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7911 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007913 return( 1 );
7914 }
7915
7916 /*
7917 * Case D: An application data message is being processed
7918 */
7919 if( ssl->in_offt != NULL )
7920 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007922 return( 1 );
7923 }
7924
7925 /*
7926 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007927 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007928 * we implement support for multiple alerts in single records.
7929 */
7930
7931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7932 return( 0 );
7933}
7934
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007935uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007936{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007937 if( ssl->session != NULL )
7938 return( ssl->session->verify_result );
7939
7940 if( ssl->session_negotiate != NULL )
7941 return( ssl->session_negotiate->verify_result );
7942
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007943 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007944}
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007947{
Paul Bakker926c8e42013-03-06 10:23:34 +01007948 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007949 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007952}
7953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007958 {
7959 switch( ssl->minor_ver )
7960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007962 return( "DTLSv1.0" );
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007965 return( "DTLSv1.2" );
7966
7967 default:
7968 return( "unknown (DTLS)" );
7969 }
7970 }
7971#endif
7972
Paul Bakker43ca69c2011-01-15 17:35:19 +00007973 switch( ssl->minor_ver )
7974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007976 return( "SSLv3.0" );
7977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007979 return( "TLSv1.0" );
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007982 return( "TLSv1.1" );
7983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007985 return( "TLSv1.2" );
7986
Paul Bakker43ca69c2011-01-15 17:35:19 +00007987 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007988 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007989 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007990}
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007993{
Hanno Becker3136ede2018-08-17 15:28:19 +01007994 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007996 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007997
Hanno Becker78640902018-08-13 16:35:15 +01007998 if( transform == NULL )
7999 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#if defined(MBEDTLS_ZLIB_SUPPORT)
8002 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8003 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008004#endif
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008 case MBEDTLS_MODE_GCM:
8009 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008010 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008012 transform_expansion = transform->minlen;
8013 break;
8014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008016
8017 block_size = mbedtls_cipher_get_block_size(
8018 &transform->cipher_ctx_enc );
8019
Hanno Becker3136ede2018-08-17 15:28:19 +01008020 /* Expansion due to the addition of the MAC. */
8021 transform_expansion += transform->maclen;
8022
8023 /* Expansion due to the addition of CBC padding;
8024 * Theoretically up to 256 bytes, but we never use
8025 * more than the block size of the underlying cipher. */
8026 transform_expansion += block_size;
8027
8028 /* For TLS 1.1 or higher, an explicit IV is added
8029 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008030#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8031 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008032 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008033#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008034
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008035 break;
8036
8037 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008040 }
8041
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008042 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008043}
8044
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008045#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8046size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8047{
8048 size_t max_len;
8049
8050 /*
8051 * Assume mfl_code is correct since it was checked when set
8052 */
Angus Grattond8213d02016-05-25 20:56:48 +10008053 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008054
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008055 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008056 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008057 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008058 {
Angus Grattond8213d02016-05-25 20:56:48 +10008059 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008060 }
8061
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008062 /* During a handshake, use the value being negotiated */
8063 if( ssl->session_negotiate != NULL &&
8064 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8065 {
8066 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8067 }
8068
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008069 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008070}
8071#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8072
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008073#if defined(MBEDTLS_SSL_PROTO_DTLS)
8074static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8075{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008076 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8077 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8078 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8079 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8080 return ( 0 );
8081
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008082 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8083 return( ssl->mtu );
8084
8085 if( ssl->mtu == 0 )
8086 return( ssl->handshake->mtu );
8087
8088 return( ssl->mtu < ssl->handshake->mtu ?
8089 ssl->mtu : ssl->handshake->mtu );
8090}
8091#endif /* MBEDTLS_SSL_PROTO_DTLS */
8092
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008093int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8094{
8095 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8096
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008097#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8098 !defined(MBEDTLS_SSL_PROTO_DTLS)
8099 (void) ssl;
8100#endif
8101
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008102#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8103 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8104
8105 if( max_len > mfl )
8106 max_len = mfl;
8107#endif
8108
8109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008110 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008111 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008112 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008113 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8114 const size_t overhead = (size_t) ret;
8115
8116 if( ret < 0 )
8117 return( ret );
8118
8119 if( mtu <= overhead )
8120 {
8121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8122 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8123 }
8124
8125 if( max_len > mtu - overhead )
8126 max_len = mtu - overhead;
8127 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008128#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008129
Hanno Becker0defedb2018-08-10 12:35:02 +01008130#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8131 !defined(MBEDTLS_SSL_PROTO_DTLS)
8132 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008133#endif
8134
8135 return( (int) max_len );
8136}
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_X509_CRT_PARSE_C)
8139const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008140{
8141 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008142 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008143
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008144 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#if defined(MBEDTLS_SSL_CLI_C)
8149int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008150{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008151 if( ssl == NULL ||
8152 dst == NULL ||
8153 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008154 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008157 }
8158
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008159 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008162
Paul Bakker5121ce52009-01-03 21:22:43 +00008163/*
Paul Bakker1961b702013-01-25 14:49:24 +01008164 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008169
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008170 if( ssl == NULL || ssl->conf == NULL )
8171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008174 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008180#endif
8181
Paul Bakker1961b702013-01-25 14:49:24 +01008182 return( ret );
8183}
8184
8185/*
8186 * Perform the SSL handshake
8187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008189{
8190 int ret = 0;
8191
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008192 if( ssl == NULL || ssl->conf == NULL )
8193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008200
8201 if( ret != 0 )
8202 break;
8203 }
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008206
8207 return( ret );
8208}
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_SSL_RENEGOTIATION)
8211#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008212/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008213 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008216{
8217 int ret;
8218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008220
8221 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8223 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008224
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008225 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008226 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008228 return( ret );
8229 }
8230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008232
8233 return( 0 );
8234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008236
8237/*
8238 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 * - any side: calling mbedtls_ssl_renegotiate(),
8240 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8241 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008242 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008243 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008247{
8248 int ret;
8249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008251
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008252 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8253 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008254
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008255 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8256 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008260 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008262 ssl->handshake->out_msg_seq = 1;
8263 else
8264 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008265 }
8266#endif
8267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8269 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008274 return( ret );
8275 }
8276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008278
8279 return( 0 );
8280}
8281
8282/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008283 * Renegotiate current connection on client,
8284 * or request renegotiation on server
8285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008289
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008290 if( ssl == NULL || ssl->conf == NULL )
8291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008294 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008295 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008301
8302 /* Did we already try/start sending HelloRequest? */
8303 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008305
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008306 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008311 /*
8312 * On client, either start the renegotiation process or,
8313 * if already in progress, continue the handshake
8314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008319
8320 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008323 return( ret );
8324 }
8325 }
8326 else
8327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008331 return( ret );
8332 }
8333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008335
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008336 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008337}
8338
8339/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008340 * Check record counters and renegotiate if they're above the limit.
8341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008343{
Andres AG2196c7f2016-12-15 17:01:16 +00008344 size_t ep_len = ssl_ep_len( ssl );
8345 int in_ctr_cmp;
8346 int out_ctr_cmp;
8347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8349 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008350 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008351 {
8352 return( 0 );
8353 }
8354
Andres AG2196c7f2016-12-15 17:01:16 +00008355 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8356 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008357 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008358 ssl->conf->renego_period + ep_len, 8 - ep_len );
8359
8360 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008361 {
8362 return( 0 );
8363 }
8364
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008369
8370/*
8371 * Receive application data decrypted from the SSL layer
8372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008374{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008375 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008376 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008377
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008378 if( ssl == NULL || ssl->conf == NULL )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008387 return( ret );
8388
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008389 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008391 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008392 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008393 return( ret );
8394 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008395 }
8396#endif
8397
Hanno Becker4a810fb2017-05-24 16:27:30 +01008398 /*
8399 * Check if renegotiation is necessary and/or handshake is
8400 * in process. If yes, perform/continue, and fall through
8401 * if an unexpected packet is received while the client
8402 * is waiting for the ServerHello.
8403 *
8404 * (There is no equivalent to the last condition on
8405 * the server-side as it is not treated as within
8406 * a handshake while waiting for the ClientHello
8407 * after a renegotiation request.)
8408 */
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008411 ret = ssl_check_ctr_renegotiate( ssl );
8412 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8413 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008416 return( ret );
8417 }
8418#endif
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008423 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8424 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008427 return( ret );
8428 }
8429 }
8430
Hanno Beckere41158b2017-10-23 13:30:32 +01008431 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008432 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008433 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008434 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008435 if( ssl->f_get_timer != NULL &&
8436 ssl->f_get_timer( ssl->p_timer ) == -1 )
8437 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008438 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008439 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008440
Hanno Becker327c93b2018-08-15 13:56:18 +01008441 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008442 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008443 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8444 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008445
Hanno Becker4a810fb2017-05-24 16:27:30 +01008446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8447 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008448 }
8449
8450 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008452 {
8453 /*
8454 * OpenSSL sends empty messages to randomize the IV
8455 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008456 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008459 return( 0 );
8460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008462 return( ret );
8463 }
8464 }
8465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008469
Hanno Becker4a810fb2017-05-24 16:27:30 +01008470 /*
8471 * - For client-side, expect SERVER_HELLO_REQUEST.
8472 * - For server-side, expect CLIENT_HELLO.
8473 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8474 */
8475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008477 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008479 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008482
8483 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008486 {
8487 continue;
8488 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008491 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008492#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008493
Hanno Becker4a810fb2017-05-24 16:27:30 +01008494#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008499
8500 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008503 {
8504 continue;
8505 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008508 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008509#endif /* MBEDTLS_SSL_SRV_C */
8510
Hanno Becker21df7f92017-10-17 11:03:26 +01008511#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008512 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008513 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8514 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8515 ssl->conf->allow_legacy_renegotiation ==
8516 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8517 {
8518 /*
8519 * Accept renegotiation request
8520 */
Paul Bakker48916f92012-09-16 19:57:18 +00008521
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008522 /* DTLS clients need to know renego is server-initiated */
8523#if defined(MBEDTLS_SSL_PROTO_DTLS)
8524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8525 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8526 {
8527 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8528 }
8529#endif
8530 ret = ssl_start_renegotiation( ssl );
8531 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8532 ret != 0 )
8533 {
8534 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8535 return( ret );
8536 }
8537 }
8538 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008539#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008540 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008541 /*
8542 * Refuse renegotiation
8543 */
8544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#if defined(MBEDTLS_SSL_PROTO_SSL3)
8548 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008549 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008550 /* SSLv3 does not have a "no_renegotiation" warning, so
8551 we send a fatal alert and abort the connection. */
8552 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8553 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8554 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008555 }
8556 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8559 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8560 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8563 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8564 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008565 {
8566 return( ret );
8567 }
Paul Bakker48916f92012-09-16 19:57:18 +00008568 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8571 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008575 }
Paul Bakker48916f92012-09-16 19:57:18 +00008576 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008577
Hanno Becker90333da2017-10-10 11:27:13 +01008578 /* At this point, we don't know whether the renegotiation has been
8579 * completed or not. The cases to consider are the following:
8580 * 1) The renegotiation is complete. In this case, no new record
8581 * has been read yet.
8582 * 2) The renegotiation is incomplete because the client received
8583 * an application data record while awaiting the ServerHello.
8584 * 3) The renegotiation is incomplete because the client received
8585 * a non-handshake, non-application data message while awaiting
8586 * the ServerHello.
8587 * In each of these case, looping will be the proper action:
8588 * - For 1), the next iteration will read a new record and check
8589 * if it's application data.
8590 * - For 2), the loop condition isn't satisfied as application data
8591 * is present, hence continue is the same as break
8592 * - For 3), the loop condition is satisfied and read_record
8593 * will re-deliver the message that was held back by the client
8594 * when expecting the ServerHello.
8595 */
8596 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008597 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008598#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008600 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008601 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008602 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008603 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008606 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008608 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008609 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8614 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008617 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008618 }
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8623 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008624 }
8625
8626 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008627
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008628 /* We're going to return something now, cancel timer,
8629 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008631 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008632
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008634 /* If we requested renego but received AppData, resend HelloRequest.
8635 * Do it now, after setting in_offt, to avoid taking this branch
8636 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008640 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008641 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008644 return( ret );
8645 }
8646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008649 }
8650
8651 n = ( len < ssl->in_msglen )
8652 ? len : ssl->in_msglen;
8653
8654 memcpy( buf, ssl->in_offt, n );
8655 ssl->in_msglen -= n;
8656
gabor-mezei-arm3eaa1032020-07-15 10:55:00 +02008657 /* Zeroising the plaintext buffer to erase unused application data
8658 from the memory. */
8659 mbedtls_platform_zeroize( ssl->in_offt, n );
8660
Paul Bakker5121ce52009-01-03 21:22:43 +00008661 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008662 {
8663 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008664 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008665 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008667 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008668 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008669 /* more data available */
8670 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008674
Paul Bakker23986e52011-04-24 08:57:21 +00008675 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008676}
8677
8678/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008679 * Send application data to be encrypted by the SSL layer, taking care of max
8680 * fragment length and buffer size.
8681 *
8682 * According to RFC 5246 Section 6.2.1:
8683 *
8684 * Zero-length fragments of Application data MAY be sent as they are
8685 * potentially useful as a traffic analysis countermeasure.
8686 *
8687 * Therefore, it is possible that the input message length is 0 and the
8688 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008689 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008690static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008691 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008692{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008693 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8694 const size_t max_len = (size_t) ret;
8695
8696 if( ret < 0 )
8697 {
8698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8699 return( ret );
8700 }
8701
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008702 if( len > max_len )
8703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008708 "maximum fragment length: %d > %d",
8709 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008711 }
8712 else
8713#endif
8714 len = max_len;
8715 }
Paul Bakker887bd502011-06-08 13:10:54 +00008716
Paul Bakker5121ce52009-01-03 21:22:43 +00008717 if( ssl->out_left != 0 )
8718 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008719 /*
8720 * The user has previously tried to send the data and
8721 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8722 * written. In this case, we expect the high-level write function
8723 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008728 return( ret );
8729 }
8730 }
Paul Bakker887bd502011-06-08 13:10:54 +00008731 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008732 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008733 /*
8734 * The user is trying to send a message the first time, so we need to
8735 * copy the data into the internal buffers and setup the data structure
8736 * to keep track of partial writes
8737 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008738 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008740 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008741
Hanno Becker67bc7c32018-08-06 11:33:50 +01008742 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008745 return( ret );
8746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008747 }
8748
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008749 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008750}
8751
8752/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008753 * Write application data, doing 1/n-1 splitting if necessary.
8754 *
8755 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008756 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008757 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008760static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008761 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008762{
8763 int ret;
8764
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008765 if( ssl->conf->cbc_record_splitting ==
8766 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008767 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8769 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8770 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008771 {
8772 return( ssl_write_real( ssl, buf, len ) );
8773 }
8774
8775 if( ssl->split_done == 0 )
8776 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008777 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008778 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008779 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008780 }
8781
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008782 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8783 return( ret );
8784 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008785
8786 return( ret + 1 );
8787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008789
8790/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008791 * Write application data (public-facing wrapper)
8792 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008793int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008794{
8795 int ret;
8796
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008798
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008799 if( ssl == NULL || ssl->conf == NULL )
8800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8801
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008803 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8804 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008805 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008806 return( ret );
8807 }
8808#endif
8809
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008810 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008811 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008812 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008813 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008815 return( ret );
8816 }
8817 }
8818
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008819#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008820 ret = ssl_write_split( ssl, buf, len );
8821#else
8822 ret = ssl_write_real( ssl, buf, len );
8823#endif
8824
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008826
8827 return( ret );
8828}
8829
8830/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008831 * Notify the peer that the connection is being closed
8832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008834{
8835 int ret;
8836
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008837 if( ssl == NULL || ssl->conf == NULL )
8838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008841
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008842 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8848 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8849 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 return( ret );
8853 }
8854 }
8855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008857
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008858 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008859}
8860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008862{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008863 if( transform == NULL )
8864 return;
8865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008867 deflateEnd( &transform->ctx_deflate );
8868 inflateEnd( &transform->ctx_inflate );
8869#endif
8870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8872 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 mbedtls_md_free( &transform->md_ctx_enc );
8875 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008876
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008877 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008878}
8879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880#if defined(MBEDTLS_X509_CRT_PARSE_C)
8881static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008882{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008884
8885 while( cur != NULL )
8886 {
8887 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008889 cur = next;
8890 }
8891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008893
Hanno Becker0271f962018-08-16 13:23:47 +01008894#if defined(MBEDTLS_SSL_PROTO_DTLS)
8895
8896static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8897{
8898 unsigned offset;
8899 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8900
8901 if( hs == NULL )
8902 return;
8903
Hanno Becker283f5ef2018-08-24 09:34:47 +01008904 ssl_free_buffered_record( ssl );
8905
Hanno Becker0271f962018-08-16 13:23:47 +01008906 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008907 ssl_buffering_free_slot( ssl, offset );
8908}
8909
8910static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8911 uint8_t slot )
8912{
8913 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8914 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008915
8916 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8917 return;
8918
Hanno Beckere605b192018-08-21 15:59:07 +01008919 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008920 {
Hanno Beckere605b192018-08-21 15:59:07 +01008921 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008922 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008923 mbedtls_free( hs_buf->data );
8924 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008925 }
8926}
8927
8928#endif /* MBEDTLS_SSL_PROTO_DTLS */
8929
Gilles Peskine9b562d52018-04-25 20:32:43 +02008930void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008931{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008932 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8933
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008934 if( handshake == NULL )
8935 return;
8936
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008937#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8938 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8939 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008940 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008941 handshake->async_in_progress = 0;
8942 }
8943#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8944
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008945#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8946 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8947 mbedtls_md5_free( &handshake->fin_md5 );
8948 mbedtls_sha1_free( &handshake->fin_sha1 );
8949#endif
8950#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8951#if defined(MBEDTLS_SHA256_C)
8952 mbedtls_sha256_free( &handshake->fin_sha256 );
8953#endif
8954#if defined(MBEDTLS_SHA512_C)
8955 mbedtls_sha512_free( &handshake->fin_sha512 );
8956#endif
8957#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959#if defined(MBEDTLS_DHM_C)
8960 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#if defined(MBEDTLS_ECDH_C)
8963 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008964#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008965#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008966 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008967#if defined(MBEDTLS_SSL_CLI_C)
8968 mbedtls_free( handshake->ecjpake_cache );
8969 handshake->ecjpake_cache = NULL;
8970 handshake->ecjpake_cache_len = 0;
8971#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008972#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008973
Janos Follath4ae5c292016-02-10 11:27:43 +00008974#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8975 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008976 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008978#endif
8979
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008980#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8981 if( handshake->psk != NULL )
8982 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008983 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008984 mbedtls_free( handshake->psk );
8985 }
8986#endif
8987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8989 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008990 /*
8991 * Free only the linked list wrapper, not the keys themselves
8992 * since the belong to the SNI callback
8993 */
8994 if( handshake->sni_key_cert != NULL )
8995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008997
8998 while( cur != NULL )
8999 {
9000 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009002 cur = next;
9003 }
9004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009006
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009007#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009008 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009009#endif
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_PROTO_DTLS)
9012 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009013 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009014 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009015#endif
9016
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009017 mbedtls_platform_zeroize( handshake,
9018 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009019}
9020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009022{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009023 if( session == NULL )
9024 return;
9025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009027 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 mbedtls_x509_crt_free( session->peer_cert );
9030 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009031 }
Paul Bakkered27a042013-04-18 22:46:23 +02009032#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009033
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009034#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009036#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009037
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009038 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009039}
9040
Paul Bakker5121ce52009-01-03 21:22:43 +00009041/*
9042 * Free an SSL context
9043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009045{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009046 if( ssl == NULL )
9047 return;
9048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009050
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009051 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009052 {
Angus Grattond8213d02016-05-25 20:56:48 +10009053 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009055 }
9056
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009057 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009058 {
Angus Grattond8213d02016-05-25 20:56:48 +10009059 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009061 }
9062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009064 if( ssl->compress_buf != NULL )
9065 {
Angus Grattond8213d02016-05-25 20:56:48 +10009066 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009068 }
9069#endif
9070
Paul Bakker48916f92012-09-16 19:57:18 +00009071 if( ssl->transform )
9072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073 mbedtls_ssl_transform_free( ssl->transform );
9074 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009075 }
9076
9077 if( ssl->handshake )
9078 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009079 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9081 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 mbedtls_free( ssl->handshake );
9084 mbedtls_free( ssl->transform_negotiate );
9085 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009086 }
9087
Paul Bakkerc0463502013-02-14 11:19:38 +01009088 if( ssl->session )
9089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 mbedtls_ssl_session_free( ssl->session );
9091 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009092 }
9093
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009094#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009095 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009096 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009097 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009099 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9103 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9106 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009107 }
9108#endif
9109
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009110#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009112#endif
9113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009115
Paul Bakker86f04f42013-02-14 11:20:09 +01009116 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009117 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009118}
9119
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009120/*
9121 * Initialze mbedtls_ssl_config
9122 */
9123void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9124{
9125 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9126}
9127
Simon Butcherc97b6972015-12-27 23:48:17 +00009128#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009129static int ssl_preset_default_hashes[] = {
9130#if defined(MBEDTLS_SHA512_C)
9131 MBEDTLS_MD_SHA512,
9132 MBEDTLS_MD_SHA384,
9133#endif
9134#if defined(MBEDTLS_SHA256_C)
9135 MBEDTLS_MD_SHA256,
9136 MBEDTLS_MD_SHA224,
9137#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009138#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009139 MBEDTLS_MD_SHA1,
9140#endif
9141 MBEDTLS_MD_NONE
9142};
Simon Butcherc97b6972015-12-27 23:48:17 +00009143#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009144
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009145static int ssl_preset_suiteb_ciphersuites[] = {
9146 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9147 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9148 0
9149};
9150
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009151#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009152static int ssl_preset_suiteb_hashes[] = {
9153 MBEDTLS_MD_SHA256,
9154 MBEDTLS_MD_SHA384,
9155 MBEDTLS_MD_NONE
9156};
9157#endif
9158
9159#if defined(MBEDTLS_ECP_C)
9160static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009161#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009162 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009163#endif
9164#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009165 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009166#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009167 MBEDTLS_ECP_DP_NONE
9168};
9169#endif
9170
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009171/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009172 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009173 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009174int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009175 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009176{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009177#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009178 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009179#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009180
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009181 /* Use the functions here so that they are covered in tests,
9182 * but otherwise access member directly for efficiency */
9183 mbedtls_ssl_conf_endpoint( conf, endpoint );
9184 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009185
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009186 /*
9187 * Things that are common to all presets
9188 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009189#if defined(MBEDTLS_SSL_CLI_C)
9190 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9191 {
9192 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9193#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9194 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9195#endif
9196 }
9197#endif
9198
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009199#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009200 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009201#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009202
9203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9204 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9205#endif
9206
9207#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9208 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9209#endif
9210
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009211#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9212 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9213#endif
9214
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009215#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009216 conf->f_cookie_write = ssl_cookie_write_dummy;
9217 conf->f_cookie_check = ssl_cookie_check_dummy;
9218#endif
9219
9220#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9221 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9222#endif
9223
Janos Follath088ce432017-04-10 12:42:31 +01009224#if defined(MBEDTLS_SSL_SRV_C)
9225 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9226#endif
9227
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009228#if defined(MBEDTLS_SSL_PROTO_DTLS)
9229 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9230 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9231#endif
9232
9233#if defined(MBEDTLS_SSL_RENEGOTIATION)
9234 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009235 memset( conf->renego_period, 0x00, 2 );
9236 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009237#endif
9238
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009239#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9240 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9241 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009242 const unsigned char dhm_p[] =
9243 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9244 const unsigned char dhm_g[] =
9245 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9246
Hanno Beckera90658f2017-10-04 15:29:08 +01009247 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9248 dhm_p, sizeof( dhm_p ),
9249 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009250 {
9251 return( ret );
9252 }
9253 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009254#endif
9255
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009256 /*
9257 * Preset-specific defaults
9258 */
9259 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009260 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009261 /*
9262 * NSA Suite B
9263 */
9264 case MBEDTLS_SSL_PRESET_SUITEB:
9265 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9266 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9267 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9268 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9269
9270 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9271 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9272 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9273 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9274 ssl_preset_suiteb_ciphersuites;
9275
9276#if defined(MBEDTLS_X509_CRT_PARSE_C)
9277 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009278#endif
9279
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009280#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009281 conf->sig_hashes = ssl_preset_suiteb_hashes;
9282#endif
9283
9284#if defined(MBEDTLS_ECP_C)
9285 conf->curve_list = ssl_preset_suiteb_curves;
9286#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009287 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009288
9289 /*
9290 * Default
9291 */
9292 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009293 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9294 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9295 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9296 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9297 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9298 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9299 MBEDTLS_SSL_MIN_MINOR_VERSION :
9300 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009301 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9302 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9303
9304#if defined(MBEDTLS_SSL_PROTO_DTLS)
9305 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9306 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9307#endif
9308
9309 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9310 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9311 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9312 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9313 mbedtls_ssl_list_ciphersuites();
9314
9315#if defined(MBEDTLS_X509_CRT_PARSE_C)
9316 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9317#endif
9318
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009319#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009320 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009321#endif
9322
9323#if defined(MBEDTLS_ECP_C)
9324 conf->curve_list = mbedtls_ecp_grp_id_list();
9325#endif
9326
9327#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9328 conf->dhm_min_bitlen = 1024;
9329#endif
9330 }
9331
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009332 return( 0 );
9333}
9334
9335/*
9336 * Free mbedtls_ssl_config
9337 */
9338void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9339{
9340#if defined(MBEDTLS_DHM_C)
9341 mbedtls_mpi_free( &conf->dhm_P );
9342 mbedtls_mpi_free( &conf->dhm_G );
9343#endif
9344
9345#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9346 if( conf->psk != NULL )
9347 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009348 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009349 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009350 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009351 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009352 }
9353
9354 if( conf->psk_identity != NULL )
9355 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009356 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009357 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009358 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009359 conf->psk_identity_len = 0;
9360 }
9361#endif
9362
9363#if defined(MBEDTLS_X509_CRT_PARSE_C)
9364 ssl_key_cert_free( conf->key_cert );
9365#endif
9366
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009367 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009368}
9369
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009370#if defined(MBEDTLS_PK_C) && \
9371 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009372/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_RSA_C)
9378 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9379 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_ECDSA_C)
9382 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9383 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009386}
9387
Hanno Becker7e5437a2017-04-28 17:15:26 +01009388unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9389{
9390 switch( type ) {
9391 case MBEDTLS_PK_RSA:
9392 return( MBEDTLS_SSL_SIG_RSA );
9393 case MBEDTLS_PK_ECDSA:
9394 case MBEDTLS_PK_ECKEY:
9395 return( MBEDTLS_SSL_SIG_ECDSA );
9396 default:
9397 return( MBEDTLS_SSL_SIG_ANON );
9398 }
9399}
9400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009402{
9403 switch( sig )
9404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#if defined(MBEDTLS_RSA_C)
9406 case MBEDTLS_SSL_SIG_RSA:
9407 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409#if defined(MBEDTLS_ECDSA_C)
9410 case MBEDTLS_SSL_SIG_ECDSA:
9411 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009412#endif
9413 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009415 }
9416}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009417#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009418
Hanno Becker7e5437a2017-04-28 17:15:26 +01009419#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9420 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9421
9422/* Find an entry in a signature-hash set matching a given hash algorithm. */
9423mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9424 mbedtls_pk_type_t sig_alg )
9425{
9426 switch( sig_alg )
9427 {
9428 case MBEDTLS_PK_RSA:
9429 return( set->rsa );
9430 case MBEDTLS_PK_ECDSA:
9431 return( set->ecdsa );
9432 default:
9433 return( MBEDTLS_MD_NONE );
9434 }
9435}
9436
9437/* Add a signature-hash-pair to a signature-hash set */
9438void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9439 mbedtls_pk_type_t sig_alg,
9440 mbedtls_md_type_t md_alg )
9441{
9442 switch( sig_alg )
9443 {
9444 case MBEDTLS_PK_RSA:
9445 if( set->rsa == MBEDTLS_MD_NONE )
9446 set->rsa = md_alg;
9447 break;
9448
9449 case MBEDTLS_PK_ECDSA:
9450 if( set->ecdsa == MBEDTLS_MD_NONE )
9451 set->ecdsa = md_alg;
9452 break;
9453
9454 default:
9455 break;
9456 }
9457}
9458
9459/* Allow exactly one hash algorithm for each signature. */
9460void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9461 mbedtls_md_type_t md_alg )
9462{
9463 set->rsa = md_alg;
9464 set->ecdsa = md_alg;
9465}
9466
9467#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9468 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9469
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009470/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009471 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009474{
9475 switch( hash )
9476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_MD5_C)
9478 case MBEDTLS_SSL_HASH_MD5:
9479 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481#if defined(MBEDTLS_SHA1_C)
9482 case MBEDTLS_SSL_HASH_SHA1:
9483 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SHA256_C)
9486 case MBEDTLS_SSL_HASH_SHA224:
9487 return( MBEDTLS_MD_SHA224 );
9488 case MBEDTLS_SSL_HASH_SHA256:
9489 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#if defined(MBEDTLS_SHA512_C)
9492 case MBEDTLS_SSL_HASH_SHA384:
9493 return( MBEDTLS_MD_SHA384 );
9494 case MBEDTLS_SSL_HASH_SHA512:
9495 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009496#endif
9497 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009499 }
9500}
9501
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009502/*
9503 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9504 */
9505unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9506{
9507 switch( md )
9508 {
9509#if defined(MBEDTLS_MD5_C)
9510 case MBEDTLS_MD_MD5:
9511 return( MBEDTLS_SSL_HASH_MD5 );
9512#endif
9513#if defined(MBEDTLS_SHA1_C)
9514 case MBEDTLS_MD_SHA1:
9515 return( MBEDTLS_SSL_HASH_SHA1 );
9516#endif
9517#if defined(MBEDTLS_SHA256_C)
9518 case MBEDTLS_MD_SHA224:
9519 return( MBEDTLS_SSL_HASH_SHA224 );
9520 case MBEDTLS_MD_SHA256:
9521 return( MBEDTLS_SSL_HASH_SHA256 );
9522#endif
9523#if defined(MBEDTLS_SHA512_C)
9524 case MBEDTLS_MD_SHA384:
9525 return( MBEDTLS_SSL_HASH_SHA384 );
9526 case MBEDTLS_MD_SHA512:
9527 return( MBEDTLS_SSL_HASH_SHA512 );
9528#endif
9529 default:
9530 return( MBEDTLS_SSL_HASH_NONE );
9531 }
9532}
9533
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009534#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009535/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009536 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009537 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009538 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009539int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009542
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009543 if( ssl->conf->curve_list == NULL )
9544 return( -1 );
9545
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009546 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009547 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009548 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009549
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009550 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009551}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009552#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009553
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009554#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009555/*
9556 * Check if a hash proposed by the peer is in our list.
9557 * Return 0 if we're willing to use it, -1 otherwise.
9558 */
9559int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9560 mbedtls_md_type_t md )
9561{
9562 const int *cur;
9563
9564 if( ssl->conf->sig_hashes == NULL )
9565 return( -1 );
9566
9567 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9568 if( *cur == (int) md )
9569 return( 0 );
9570
9571 return( -1 );
9572}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009573#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575#if defined(MBEDTLS_X509_CRT_PARSE_C)
9576int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9577 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009578 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009579 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009580{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009581 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009583 int usage = 0;
9584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009586 const char *ext_oid;
9587 size_t ext_len;
9588#endif
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9591 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009592 ((void) cert);
9593 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009594 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009595#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9598 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009599 {
9600 /* Server part of the key exchange */
9601 switch( ciphersuite->key_exchange )
9602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 case MBEDTLS_KEY_EXCHANGE_RSA:
9604 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009605 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009606 break;
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9609 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9610 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9611 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009612 break;
9613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9615 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009616 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009617 break;
9618
9619 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 case MBEDTLS_KEY_EXCHANGE_NONE:
9621 case MBEDTLS_KEY_EXCHANGE_PSK:
9622 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9623 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009624 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009625 usage = 0;
9626 }
9627 }
9628 else
9629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9631 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009632 }
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009635 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009636 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009637 ret = -1;
9638 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009639#else
9640 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9644 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9647 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009648 }
9649 else
9650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9652 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009653 }
9654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009656 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009657 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009658 ret = -1;
9659 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009661
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009662 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009663}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009665
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009666/*
9667 * Convert version numbers to/from wire format
9668 * and, for DTLS, to/from TLS equivalent.
9669 *
9670 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009671 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009672 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9673 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009676 unsigned char ver[2] )
9677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#if defined(MBEDTLS_SSL_PROTO_DTLS)
9679 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009682 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9683
9684 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9685 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9686 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009687 else
9688#else
9689 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009690#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009691 {
9692 ver[0] = (unsigned char) major;
9693 ver[1] = (unsigned char) minor;
9694 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009695}
9696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009698 const unsigned char ver[2] )
9699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700#if defined(MBEDTLS_SSL_PROTO_DTLS)
9701 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009702 {
9703 *major = 255 - ver[0] + 2;
9704 *minor = 255 - ver[1] + 1;
9705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009707 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9708 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009709 else
9710#else
9711 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009712#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009713 {
9714 *major = ver[0];
9715 *minor = ver[1];
9716 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009717}
9718
Simon Butcher99000142016-10-13 17:21:01 +01009719int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9720{
9721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9722 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9723 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9724
9725 switch( md )
9726 {
9727#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9728#if defined(MBEDTLS_MD5_C)
9729 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009730 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009731#endif
9732#if defined(MBEDTLS_SHA1_C)
9733 case MBEDTLS_SSL_HASH_SHA1:
9734 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9735 break;
9736#endif
9737#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9738#if defined(MBEDTLS_SHA512_C)
9739 case MBEDTLS_SSL_HASH_SHA384:
9740 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9741 break;
9742#endif
9743#if defined(MBEDTLS_SHA256_C)
9744 case MBEDTLS_SSL_HASH_SHA256:
9745 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9746 break;
9747#endif
9748 default:
9749 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9750 }
9751
9752 return 0;
9753#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9754 (void) ssl;
9755 (void) md;
9756
9757 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9759}
9760
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009761#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9762 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9763int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9764 unsigned char *output,
9765 unsigned char *data, size_t data_len )
9766{
9767 int ret = 0;
9768 mbedtls_md5_context mbedtls_md5;
9769 mbedtls_sha1_context mbedtls_sha1;
9770
9771 mbedtls_md5_init( &mbedtls_md5 );
9772 mbedtls_sha1_init( &mbedtls_sha1 );
9773
9774 /*
9775 * digitally-signed struct {
9776 * opaque md5_hash[16];
9777 * opaque sha_hash[20];
9778 * };
9779 *
9780 * md5_hash
9781 * MD5(ClientHello.random + ServerHello.random
9782 * + ServerParams);
9783 * sha_hash
9784 * SHA(ClientHello.random + ServerHello.random
9785 * + ServerParams);
9786 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009787 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009788 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009790 goto exit;
9791 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009792 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009793 ssl->handshake->randbytes, 64 ) ) != 0 )
9794 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009796 goto exit;
9797 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009798 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009799 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009801 goto exit;
9802 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009803 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009804 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009806 goto exit;
9807 }
9808
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009809 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009812 goto exit;
9813 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009814 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009815 ssl->handshake->randbytes, 64 ) ) != 0 )
9816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009818 goto exit;
9819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009820 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009821 data_len ) ) != 0 )
9822 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009824 goto exit;
9825 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009826 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009827 output + 16 ) ) != 0 )
9828 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009830 goto exit;
9831 }
9832
9833exit:
9834 mbedtls_md5_free( &mbedtls_md5 );
9835 mbedtls_sha1_free( &mbedtls_sha1 );
9836
9837 if( ret != 0 )
9838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9839 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9840
9841 return( ret );
9842
9843}
9844#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9845 MBEDTLS_SSL_PROTO_TLS1_1 */
9846
9847#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9848 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9849int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009850 unsigned char *hash, size_t *hashlen,
9851 unsigned char *data, size_t data_len,
9852 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009853{
9854 int ret = 0;
9855 mbedtls_md_context_t ctx;
9856 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009857 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009858
9859 mbedtls_md_init( &ctx );
9860
9861 /*
9862 * digitally-signed struct {
9863 * opaque client_random[32];
9864 * opaque server_random[32];
9865 * ServerDHParams params;
9866 * };
9867 */
9868 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9869 {
9870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9871 goto exit;
9872 }
9873 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9874 {
9875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9876 goto exit;
9877 }
9878 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9879 {
9880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9881 goto exit;
9882 }
9883 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9884 {
9885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9886 goto exit;
9887 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009888 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009889 {
9890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9891 goto exit;
9892 }
9893
9894exit:
9895 mbedtls_md_free( &ctx );
9896
9897 if( ret != 0 )
9898 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9899 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9900
9901 return( ret );
9902}
9903#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9904 MBEDTLS_SSL_PROTO_TLS1_2 */
9905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906#endif /* MBEDTLS_SSL_TLS_C */