blob: 21682bea72e9b98645500f253e3d61758bcbba08 [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) || \
1434 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001435 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001436#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001437#endif
1438
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001439/* The function below is only used in the Lucky 13 counter-measure in
1440 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1441#if defined(SSL_SOME_MODES_USE_MAC) && \
1442 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1443 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1444 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1445/* This function makes sure every byte in the memory region is accessed
1446 * (in ascending addresses order) */
1447static void ssl_read_memory( unsigned char *p, size_t len )
1448{
1449 unsigned char acc = 0;
1450 volatile unsigned char force;
1451
1452 for( ; len != 0; p++, len-- )
1453 acc ^= *p;
1454
1455 force = acc;
1456 (void) force;
1457}
1458#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1459
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001460/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001466 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001470 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001474 }
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001479 ssl->out_msg, ssl->out_msglen );
1480
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001482 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001484#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( mode == MBEDTLS_MODE_STREAM ||
1486 ( mode == MBEDTLS_MODE_CBC
1487#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1488 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001489#endif
1490 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_PROTO_SSL3)
1493 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001494 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001495 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001496
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001497 ssl_mac( &ssl->transform_out->md_ctx_enc,
1498 ssl->transform_out->mac_enc,
1499 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001500 ssl->out_ctr, ssl->out_msgtype,
1501 mac );
1502
1503 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001504 }
1505 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1508 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1509 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001510 {
Hanno Becker992b6872017-11-09 18:57:39 +00001511 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1514 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1516 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001517 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001518 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001520
1521 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001522 }
1523 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001524#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001528 }
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001531 ssl->out_msg + ssl->out_msglen,
1532 ssl->transform_out->maclen );
1533
1534 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001535 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001536 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001537#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001539 /*
1540 * Encrypt
1541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1543 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001545 int ret;
1546 size_t olen = 0;
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 "including %d bytes of padding",
1550 ssl->out_msglen, 0 ) );
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001553 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001554 ssl->transform_out->ivlen,
1555 ssl->out_msg, ssl->out_msglen,
1556 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001559 return( ret );
1560 }
1561
1562 if( ssl->out_msglen != olen )
1563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 }
Paul Bakker68884e32013-01-07 18:20:04 +01001568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570#if defined(MBEDTLS_GCM_C) || \
1571 defined(MBEDTLS_CCM_C) || \
1572 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 mode == MBEDTLS_MODE_CCM ||
1575 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001576 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001577 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001578 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001579 unsigned char *enc_msg;
1580 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001581 unsigned char iv[12];
1582 mbedtls_ssl_transform *transform = ssl->transform_out;
1583 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001585 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001586
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001587 /*
1588 * Prepare additional authenticated data
1589 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001590 memcpy( add_data, ssl->out_ctr, 8 );
1591 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001593 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001594 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1595 add_data[12] = ssl->out_msglen & 0xFF;
1596
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001597 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001598
Paul Bakker68884e32013-01-07 18:20:04 +01001599 /*
1600 * Generate IV
1601 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001602 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1603 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001604 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001605 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1606 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1607 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1608
1609 }
1610 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1611 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001612 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001613 unsigned char i;
1614
1615 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1616
1617 for( i = 0; i < 8; i++ )
1618 iv[i+4] ^= ssl->out_ctr[i];
1619 }
1620 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001621 {
1622 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001625 }
1626
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001627 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1628 iv, transform->ivlen );
1629 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1630 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001631
Paul Bakker68884e32013-01-07 18:20:04 +01001632 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001633 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001634 */
1635 enc_msg = ssl->out_msg;
1636 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001637 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001640 "including 0 bytes of padding",
1641 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001642
Paul Bakker68884e32013-01-07 18:20:04 +01001643 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001644 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001645 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001646 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1647 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001648 add_data, 13,
1649 enc_msg, enc_msglen,
1650 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001651 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001654 return( ret );
1655 }
1656
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001657 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001661 }
1662
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001663 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001664 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1670#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001671 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001674 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001676 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001677
Paul Bakker48916f92012-09-16 19:57:18 +00001678 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1679 ssl->transform_out->ivlen;
1680 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 padlen = 0;
1682
1683 for( i = 0; i <= padlen; i++ )
1684 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1685
1686 ssl->out_msglen += padlen + 1;
1687
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001688 enc_msglen = ssl->out_msglen;
1689 enc_msg = ssl->out_msg;
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001692 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001693 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1694 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 {
1698 /*
1699 * Generate IV
1700 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001701 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001702 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001703 if( ret != 0 )
1704 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001705
Paul Bakker92be97b2013-01-02 17:30:03 +01001706 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001707 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708
1709 /*
1710 * Fix pointer positions and message length with added IV
1711 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001712 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001714 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001719 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001720 ssl->out_msglen, ssl->transform_out->ivlen,
1721 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001724 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001725 ssl->transform_out->ivlen,
1726 enc_msg, enc_msglen,
1727 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001730 return( ret );
1731 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001732
Paul Bakkercca5b812013-08-31 17:40:26 +02001733 if( enc_msglen != olen )
1734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1740 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001741 {
1742 /*
1743 * Save IV in SSL3 and TLS1
1744 */
1745 memcpy( ssl->transform_out->iv_enc,
1746 ssl->transform_out->cipher_ctx_enc.iv,
1747 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001749#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001753 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001754 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1755
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001756 /*
1757 * MAC(MAC_write_key, seq_num +
1758 * TLSCipherText.type +
1759 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001760 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761 * IV + // except for TLS 1.0
1762 * ENC(content + padding + padding_length));
1763 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 unsigned char pseudo_hdr[13];
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1769 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001770 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1771 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1776 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001778 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780
Hanno Becker3d8c9072018-01-05 16:24:22 +00001781 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1782 ssl->transform_out->maclen );
1783
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001789 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001791 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001797 /* Make extra sure authentication was performed, exactly once */
1798 if( auth_done != 1 )
1799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001802 }
1803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
1806 return( 0 );
1807}
1808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001812 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001813#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001814 size_t padlen = 0, correct = 1;
1815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001819 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001823 }
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001830 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 }
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1835 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001836 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001837 int ret;
1838 size_t olen = 0;
1839
Paul Bakker68884e32013-01-07 18:20:04 +01001840 padlen = 0;
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001843 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001844 ssl->transform_in->ivlen,
1845 ssl->in_msg, ssl->in_msglen,
1846 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001849 return( ret );
1850 }
1851
1852 if( ssl->in_msglen != olen )
1853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
Paul Bakker68884e32013-01-07 18:20:04 +01001858 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001860#if defined(MBEDTLS_GCM_C) || \
1861 defined(MBEDTLS_CCM_C) || \
1862 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001864 mode == MBEDTLS_MODE_CCM ||
1865 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001866 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001867 int ret;
1868 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001869 unsigned char *dec_msg;
1870 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001871 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001872 unsigned char iv[12];
1873 mbedtls_ssl_transform *transform = ssl->transform_in;
1874 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001876 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 /*
1879 * Compute and update sizes
1880 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001881 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001884 "+ taglen (%d)", ssl->in_msglen,
1885 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001887 }
1888 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1889
Paul Bakker68884e32013-01-07 18:20:04 +01001890 dec_msg = ssl->in_msg;
1891 dec_msg_result = ssl->in_msg;
1892 ssl->in_msglen = dec_msglen;
1893
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001894 /*
1895 * Prepare additional authenticated data
1896 */
Paul Bakker68884e32013-01-07 18:20:04 +01001897 memcpy( add_data, ssl->in_ctr, 8 );
1898 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001900 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001901 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1902 add_data[12] = ssl->in_msglen & 0xFF;
1903
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001904 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001905
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001906 /*
1907 * Prepare IV
1908 */
1909 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1910 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001911 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001912 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1913 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001914
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001915 }
1916 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1917 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001918 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001919 unsigned char i;
1920
1921 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1922
1923 for( i = 0; i < 8; i++ )
1924 iv[i+4] ^= ssl->in_ctr[i];
1925 }
1926 else
1927 {
1928 /* Reminder if we ever add an AEAD mode with a different size */
1929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1931 }
1932
1933 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001935
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001936 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001937 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001940 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001941 add_data, 13,
1942 dec_msg, dec_msglen,
1943 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001944 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1949 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001950
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001951 return( ret );
1952 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001953 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001954
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001955 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1958 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001959 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1963#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001964 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 {
Paul Bakker45829992013-01-03 14:52:21 +01001967 /*
1968 * Decrypt and check the padding
1969 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001970 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001971 unsigned char *dec_msg;
1972 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001973 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001974 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001975 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 /*
Paul Bakker45829992013-01-03 14:52:21 +01001978 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1981 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001982 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001983#endif
Paul Bakker45829992013-01-03 14:52:21 +01001984
1985 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1986 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001989 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1990 ssl->transform_in->ivlen,
1991 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001993 }
1994
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001995 dec_msglen = ssl->in_msglen;
1996 dec_msg = ssl->in_msg;
1997 dec_msg_result = ssl->in_msg;
1998
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 /*
2000 * Authenticate before decrypt if enabled
2001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2003 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002004 {
Hanno Becker992b6872017-11-09 18:57:39 +00002005 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002006 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002009
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002010 dec_msglen -= ssl->transform_in->maclen;
2011 ssl->in_msglen -= ssl->transform_in->maclen;
2012
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002013 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2014 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2015 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2016 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2021 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002022 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002023 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002028 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002029 ssl->transform_in->maclen );
2030
Hanno Becker992b6872017-11-09 18:57:39 +00002031 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2032 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002038 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002041
2042 /*
2043 * Check length sanity
2044 */
2045 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002048 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002050 }
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002053 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002054 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002057 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002059 dec_msglen -= ssl->transform_in->ivlen;
2060 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002061
Paul Bakker48916f92012-09-16 19:57:18 +00002062 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002063 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002064 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002068 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002069 ssl->transform_in->ivlen,
2070 dec_msg, dec_msglen,
2071 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002074 return( ret );
2075 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002076
Paul Bakkercca5b812013-08-31 17:40:26 +02002077 if( dec_msglen != olen )
2078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002081 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2084 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002085 {
2086 /*
2087 * Save IV in SSL3 and TLS1
2088 */
2089 memcpy( ssl->transform_in->iv_dec,
2090 ssl->transform_in->cipher_ctx_dec.iv,
2091 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002093#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
2095 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002096
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002097 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002098 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_SSL_DEBUG_ALL)
2101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002102 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002103#endif
Paul Bakker45829992013-01-03 14:52:21 +01002104 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002105 correct = 0;
2106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_SSL_PROTO_SSL3)
2109 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Paul Bakker48916f92012-09-16 19:57:18 +00002111 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_SSL_DEBUG_ALL)
2114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002116 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002117#endif
Paul Bakker45829992013-01-03 14:52:21 +01002118 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
2120 }
2121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2123#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2124 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2125 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 {
2127 /*
Paul Bakker45829992013-01-03 14:52:21 +01002128 * TLSv1+: always check the padding up to the first failure
2129 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002131 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002132 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002133 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002134
Paul Bakker956c9e02013-12-19 14:42:28 +01002135 /*
2136 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002137 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002138 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002139 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002140 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002141 *
2142 * In both cases we reset padding_idx to a safe value (0) to
2143 * prevent out-of-buffer reads.
2144 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002145 correct &= ( padlen <= ssl->in_msglen );
2146 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002147 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002148
2149 padding_idx *= correct;
2150
Angus Grattonb512bc12018-06-19 15:57:50 +10002151 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002152 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002153 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002154 pad_count += real_count *
2155 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2156 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002157
2158 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002161 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002163#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002164 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002166 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2168 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002172 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002173
2174 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002178 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002184#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002187#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
2189 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002190 * Authenticate if not done yet.
2191 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002193#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002194 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002195 {
Hanno Becker992b6872017-11-09 18:57:39 +00002196 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002197
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002198 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002200 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2201 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_SSL_PROTO_SSL3)
2204 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002205 {
2206 ssl_mac( &ssl->transform_in->md_ctx_dec,
2207 ssl->transform_in->mac_dec,
2208 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002209 ssl->in_ctr, ssl->in_msgtype,
2210 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002211 }
2212 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2214#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2215 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2216 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002217 {
2218 /*
2219 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002220 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 *
2222 * Known timing attacks:
2223 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2224 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002225 * To compensate for different timings for the MAC calculation
2226 * depending on how much padding was removed (which is determined
2227 * by padlen), process extra_run more blocks through the hash
2228 * function.
2229 *
2230 * The formula in the paper is
2231 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2232 * where L1 is the size of the header plus the decrypted message
2233 * plus CBC padding and L2 is the size of the header plus the
2234 * decrypted message. This is for an underlying hash function
2235 * with 64-byte blocks.
2236 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2237 * correctly. We round down instead of up, so -56 is the correct
2238 * value for our calculations instead of -55.
2239 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002240 * Repeat the formula rather than defining a block_size variable.
2241 * This avoids requiring division by a variable at runtime
2242 * (which would be marginally less efficient and would require
2243 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 */
2245 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002246
2247 /*
2248 * The next two sizes are the minimum and maximum values of
2249 * in_msglen over all padlen values.
2250 *
2251 * They're independent of padlen, since we previously did
2252 * in_msglen -= padlen.
2253 *
2254 * Note that max_len + maclen is never more than the buffer
2255 * length, as we previously did in_msglen -= maclen too.
2256 */
2257 const size_t max_len = ssl->in_msglen + padlen;
2258 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2259
Gilles Peskine20b44082018-05-29 14:06:49 +02002260 switch( ssl->transform_in->ciphersuite_info->mac )
2261 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002262#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2263 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002264 case MBEDTLS_MD_MD5:
2265 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002266 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002267 /* 8 bytes of message size, 64-byte compression blocks */
2268 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2269 ( 13 + ssl->in_msglen + 8 ) / 64;
2270 break;
2271#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002272#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002273 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002274 /* 16 bytes of message size, 128-byte compression blocks */
2275 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2276 ( 13 + ssl->in_msglen + 16 ) / 128;
2277 break;
2278#endif
2279 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2282 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002283
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002284 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2287 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2288 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2289 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002290 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002291 /* Make sure we access everything even when padlen > 0. This
2292 * makes the synchronisation requirements for just-in-time
2293 * Prime+Probe attacks much tighter and hopefully impractical. */
2294 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002295 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002296
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002297 /* Dummy calls to compression function.
2298 * Call mbedtls_md_process at least once due to cache attacks
2299 * that observe whether md_process() was called of not.
2300 * Respect the usual start-(process|update)-finish sequence for
2301 * the sake of hardware accelerators that might require it. */
2302 mbedtls_md_starts( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002303 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Manuel Pégourié-Gonnard20cd85c2020-06-18 11:30:40 +02002305 {
2306 /* The switch statement above already checks that we're using
2307 * one of MD-5, SHA-1, SHA-256 or SHA-384. */
2308 unsigned char tmp[384 / 8];
2309 mbedtls_md_finish( &ssl->transform_in->md_ctx_dec, tmp );
2310 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002313
2314 /* Make sure we access all the memory that could contain the MAC,
2315 * before we check it in the next code block. This makes the
2316 * synchronisation requirements for just-in-time Prime+Probe
2317 * attacks much tighter and hopefully impractical. */
2318 ssl_read_memory( ssl->in_msg + min_len,
2319 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002320 }
2321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2323 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002329#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002330 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2331 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2332 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
Hanno Becker992b6872017-11-09 18:57:39 +00002335 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2336 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_SSL_DEBUG_ALL)
2339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002340#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002341 correct = 0;
2342 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002343 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002344 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002345
2346 /*
2347 * Finally check the correct flag
2348 */
2349 if( correct == 0 )
2350 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002351#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002352
2353 /* Make extra sure authentication was performed, exactly once */
2354 if( auth_done != 1 )
2355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2357 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
2360 if( ssl->in_msglen == 0 )
2361 {
Angus Gratton34817922018-06-19 15:58:22 +10002362#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2364 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2365 {
2366 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2368 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2369 }
2370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2371
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 ssl->nb_zero++;
2373
2374 /*
2375 * Three or more empty messages may be a DoS attack
2376 * (excessive CPU consumption).
2377 */
2378 if( ssl->nb_zero > 3 )
2379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
2384 }
2385 else
2386 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002389 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002390 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002391 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002392 }
2393 else
2394#endif
2395 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002396 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002397 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2398 if( ++ssl->in_ctr[i - 1] != 0 )
2399 break;
2400
2401 /* The loop goes to its end iff the counter is wrapping */
2402 if( i == ssl_ep_len( ssl ) )
2403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2405 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002406 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002407 }
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 return( 0 );
2412}
2413
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002414#undef MAC_NONE
2415#undef MAC_PLAINTEXT
2416#undef MAC_CIPHERTEXT
2417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002419/*
2420 * Compression/decompression functions
2421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002423{
2424 int ret;
2425 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002426 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002428 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002432 if( len_pre == 0 )
2433 return( 0 );
2434
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435 memcpy( msg_pre, ssl->out_msg, len_pre );
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438 ssl->out_msglen ) );
2439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002441 ssl->out_msg, ssl->out_msglen );
2442
Paul Bakker48916f92012-09-16 19:57:18 +00002443 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2444 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2445 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002446 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002447
Paul Bakker48916f92012-09-16 19:57:18 +00002448 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002449 if( ret != Z_OK )
2450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2452 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002453 }
2454
Angus Grattond8213d02016-05-25 20:56:48 +10002455 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002456 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002459 ssl->out_msglen ) );
2460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002462 ssl->out_msg, ssl->out_msglen );
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002465
2466 return( 0 );
2467}
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002470{
2471 int ret;
2472 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002473 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002474 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002475 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002478
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002479 if( len_pre == 0 )
2480 return( 0 );
2481
Paul Bakker2770fbd2012-07-03 13:30:23 +00002482 memcpy( msg_pre, ssl->in_msg, len_pre );
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485 ssl->in_msglen ) );
2486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002488 ssl->in_msg, ssl->in_msglen );
2489
Paul Bakker48916f92012-09-16 19:57:18 +00002490 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2491 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2492 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002493 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002494 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002495
Paul Bakker48916f92012-09-16 19:57:18 +00002496 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002497 if( ret != Z_OK )
2498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2500 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002501 }
2502
Angus Grattond8213d02016-05-25 20:56:48 +10002503 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002504 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002507 ssl->in_msglen ) );
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002510 ssl->in_msg, ssl->in_msglen );
2511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002513
2514 return( 0 );
2515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2519static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SSL_PROTO_DTLS)
2522static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002523{
2524 /* If renegotiation is not enforced, retransmit until we would reach max
2525 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002526 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002527 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002528 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002529 unsigned char doublings = 1;
2530
2531 while( ratio != 0 )
2532 {
2533 ++doublings;
2534 ratio >>= 1;
2535 }
2536
2537 if( ++ssl->renego_records_seen > doublings )
2538 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002540 return( 0 );
2541 }
2542 }
2543
2544 return( ssl_write_hello_request( ssl ) );
2545}
2546#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002548
Paul Bakker5121ce52009-01-03 21:22:43 +00002549/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002550 * Fill the input message buffer by appending data to it.
2551 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002552 *
2553 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2554 * available (from this read and/or a previous one). Otherwise, an error code
2555 * is returned (possibly EOF or WANT_READ).
2556 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002557 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2558 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2559 * since we always read a whole datagram at once.
2560 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002561 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002562 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002565{
Paul Bakker23986e52011-04-24 08:57:21 +00002566 int ret;
2567 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002571 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002574 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002576 }
2577
Angus Grattond8213d02016-05-25 20:56:48 +10002578 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002582 }
2583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002585 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002587 uint32_t timeout;
2588
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002589 /* Just to be sure */
2590 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2593 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2595 }
2596
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002597 /*
2598 * The point is, we need to always read a full datagram at once, so we
2599 * sometimes read more then requested, and handle the additional data.
2600 * It could be the rest of the current record (while fetching the
2601 * header) and/or some other records in the same datagram.
2602 */
2603
2604 /*
2605 * Move to the next record in the already read datagram if applicable
2606 */
2607 if( ssl->next_record_offset != 0 )
2608 {
2609 if( ssl->in_left < ssl->next_record_offset )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002613 }
2614
2615 ssl->in_left -= ssl->next_record_offset;
2616
2617 if( ssl->in_left != 0 )
2618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002620 ssl->next_record_offset ) );
2621 memmove( ssl->in_hdr,
2622 ssl->in_hdr + ssl->next_record_offset,
2623 ssl->in_left );
2624 }
2625
2626 ssl->next_record_offset = 0;
2627 }
2628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002631
2632 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002633 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002634 */
2635 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002638 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002639 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002640
2641 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002642 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002643 * are not at the beginning of a new record, the caller did something
2644 * wrong.
2645 */
2646 if( ssl->in_left != 0 )
2647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002650 }
2651
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002652 /*
2653 * Don't even try to read if time's out already.
2654 * This avoids by-passing the timer when repeatedly receiving messages
2655 * that will end up being dropped.
2656 */
2657 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002658 {
2659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002660 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002661 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002662 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002663 {
Angus Grattond8213d02016-05-25 20:56:48 +10002664 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002667 timeout = ssl->handshake->retransmit_timeout;
2668 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002669 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002672
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002673 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002674 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2675 timeout );
2676 else
2677 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002680
2681 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002683 }
2684
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002685 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002688 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002691 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002692 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002695 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002696 }
2697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002701 return( ret );
2702 }
2703
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002704 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002707 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002709 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002710 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002713 return( ret );
2714 }
2715
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002716 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002719 }
2720
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 if( ret < 0 )
2722 return( ret );
2723
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002724 ssl->in_left = ret;
2725 }
2726 else
2727#endif
2728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002730 ssl->in_left, nb_want ) );
2731
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002732 while( ssl->in_left < nb_want )
2733 {
2734 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002735
2736 if( ssl_check_timer( ssl ) != 0 )
2737 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2738 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002739 {
2740 if( ssl->f_recv_timeout != NULL )
2741 {
2742 ret = ssl->f_recv_timeout( ssl->p_bio,
2743 ssl->in_hdr + ssl->in_left, len,
2744 ssl->conf->read_timeout );
2745 }
2746 else
2747 {
2748 ret = ssl->f_recv( ssl->p_bio,
2749 ssl->in_hdr + ssl->in_left, len );
2750 }
2751 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002754 ssl->in_left, nb_want ) );
2755 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002756
2757 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002759
2760 if( ret < 0 )
2761 return( ret );
2762
mohammad160352aecb92018-03-28 23:41:40 -07002763 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002764 {
Darryl Green11999bb2018-03-13 15:22:58 +00002765 MBEDTLS_SSL_DEBUG_MSG( 1,
2766 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002767 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2769 }
2770
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002771 ssl->in_left += ret;
2772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 }
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
2777 return( 0 );
2778}
2779
2780/*
2781 * Flush any data not yet written
2782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002785 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002786 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002790 if( ssl->f_send == NULL )
2791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002793 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002795 }
2796
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002797 /* Avoid incrementing counter if data is flushed */
2798 if( ssl->out_left == 0 )
2799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002801 return( 0 );
2802 }
2803
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 while( ssl->out_left > 0 )
2805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2807 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Hanno Becker2b1e3542018-08-06 11:19:13 +01002809 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002810 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
2814 if( ret <= 0 )
2815 return( ret );
2816
mohammad160352aecb92018-03-28 23:41:40 -07002817 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002818 {
Darryl Green11999bb2018-03-13 15:22:58 +00002819 MBEDTLS_SSL_DEBUG_MSG( 1,
2820 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002821 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2823 }
2824
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 ssl->out_left -= ret;
2826 }
2827
Hanno Becker2b1e3542018-08-06 11:19:13 +01002828#if defined(MBEDTLS_SSL_PROTO_DTLS)
2829 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002830 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002831 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002832 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002833 else
2834#endif
2835 {
2836 ssl->out_hdr = ssl->out_buf + 8;
2837 }
2838 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
2842 return( 0 );
2843}
2844
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845/*
2846 * Functions to handle the DTLS retransmission state machine
2847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002849/*
2850 * Append current handshake message to current outgoing flight
2851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2856 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2857 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858
2859 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002860 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002861 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002864 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865 }
2866
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002867 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002868 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002871 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002872 }
2873
2874 /* Copy current handshake message with headers */
2875 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2876 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002877 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002878 msg->next = NULL;
2879
2880 /* Append to the current flight */
2881 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002882 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002883 else
2884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002886 while( cur->next != NULL )
2887 cur = cur->next;
2888 cur->next = msg;
2889 }
2890
Hanno Becker3b235902018-08-06 09:54:53 +01002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002892 return( 0 );
2893}
2894
2895/*
2896 * Free the current flight of handshake messages
2897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 mbedtls_ssl_flight_item *cur = flight;
2901 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002902
2903 while( cur != NULL )
2904 {
2905 next = cur->next;
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 mbedtls_free( cur->p );
2908 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002909
2910 cur = next;
2911 }
2912}
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2915static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002916#endif
2917
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002918/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919 * Swap transform_out and out_ctr with the alternative ones
2920 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002921static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002924 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002925#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2926 int ret;
2927#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002928
2929 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002932 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002933 }
2934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002936
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002937 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002938 tmp_transform = ssl->transform_out;
2939 ssl->transform_out = ssl->handshake->alt_transform_out;
2940 ssl->handshake->alt_transform_out = tmp_transform;
2941
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002942 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002943 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2944 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002945 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002946
2947 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002948 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2951 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2956 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002957 }
2958 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002959#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
2960
2961 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002962}
2963
2964/*
2965 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002966 */
2967int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2968{
2969 int ret = 0;
2970
2971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2972
2973 ret = mbedtls_ssl_flight_transmit( ssl );
2974
2975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2976
2977 return( ret );
2978}
2979
2980/*
2981 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002982 *
2983 * Need to remember the current message in case flush_output returns
2984 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002985 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002986 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002987int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002988{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002989 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002993 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002995
2996 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002997 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002998 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
2999 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003002 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003003
3004 while( ssl->handshake->cur_msg != NULL )
3005 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003006 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003007 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003008
Hanno Beckere1dcb032018-08-17 16:47:58 +01003009 int const is_finished =
3010 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3011 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3012
Hanno Becker04da1892018-08-14 13:22:10 +01003013 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3014 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3015
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003016 /* Swap epochs before sending Finished: we can't do it after
3017 * sending ChangeCipherSpec, in case write returns WANT_READ.
3018 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003019 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003020 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003022 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3023 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003024 }
3025
Hanno Becker67bc7c32018-08-06 11:33:50 +01003026 ret = ssl_get_remaining_payload_in_datagram( ssl );
3027 if( ret < 0 )
3028 return( ret );
3029 max_frag_len = (size_t) ret;
3030
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003031 /* CCS is copied as is, while HS messages may need fragmentation */
3032 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3033 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003034 if( max_frag_len == 0 )
3035 {
3036 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3037 return( ret );
3038
3039 continue;
3040 }
3041
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003042 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003043 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003044 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003045
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003046 /* Update position inside current message */
3047 ssl->handshake->cur_msg_p += cur->len;
3048 }
3049 else
3050 {
3051 const unsigned char * const p = ssl->handshake->cur_msg_p;
3052 const size_t hs_len = cur->len - 12;
3053 const size_t frag_off = p - ( cur->p + 12 );
3054 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003055 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003056
Hanno Beckere1dcb032018-08-17 16:47:58 +01003057 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003058 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003059 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003060 {
3061 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3062 return( ret );
3063 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003064
Hanno Becker67bc7c32018-08-06 11:33:50 +01003065 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3066 return( ret );
3067
3068 continue;
3069 }
3070 max_hs_frag_len = max_frag_len - 12;
3071
3072 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3073 max_hs_frag_len : rem_len;
3074
3075 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003076 {
3077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003078 (unsigned) cur_hs_frag_len,
3079 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003080 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003081
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003082 /* Messages are stored with handshake headers as if not fragmented,
3083 * copy beginning of headers then fill fragmentation fields.
3084 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3085 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003086
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003087 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3088 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3089 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3090
Hanno Becker67bc7c32018-08-06 11:33:50 +01003091 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3092 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3093 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003094
3095 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3096
Hanno Becker3f7b9732018-08-28 09:53:25 +01003097 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003098 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3099 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003100 ssl->out_msgtype = cur->type;
3101
3102 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003103 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003104 }
3105
3106 /* If done with the current message move to the next one if any */
3107 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3108 {
3109 if( cur->next != NULL )
3110 {
3111 ssl->handshake->cur_msg = cur->next;
3112 ssl->handshake->cur_msg_p = cur->next->p + 12;
3113 }
3114 else
3115 {
3116 ssl->handshake->cur_msg = NULL;
3117 ssl->handshake->cur_msg_p = NULL;
3118 }
3119 }
3120
3121 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003122 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125 return( ret );
3126 }
3127 }
3128
Hanno Becker67bc7c32018-08-06 11:33:50 +01003129 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3130 return( ret );
3131
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003132 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3134 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003135 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003138 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3139 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003140
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003142
3143 return( 0 );
3144}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003145
3146/*
3147 * To be called when the last message of an incoming flight is received.
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003150{
3151 /* We won't need to resend that one any more */
3152 ssl_flight_free( ssl->handshake->flight );
3153 ssl->handshake->flight = NULL;
3154 ssl->handshake->cur_msg = NULL;
3155
3156 /* The next incoming flight will start with this msg_seq */
3157 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3158
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003159 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003160 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003161
Hanno Becker0271f962018-08-16 13:23:47 +01003162 /* Clear future message buffering structure. */
3163 ssl_buffering_free( ssl );
3164
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003165 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003166 ssl_set_timer( ssl, 0 );
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3169 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003172 }
3173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003175}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003176
3177/*
3178 * To be called when the last message of an outgoing flight is send.
3179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003181{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003182 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003183 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3186 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003189 }
3190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003194
Paul Bakker5121ce52009-01-03 21:22:43 +00003195/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003196 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003198
3199/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003200 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003201 *
3202 * - fill in handshake headers
3203 * - update handshake checksum
3204 * - DTLS: save message for resending
3205 * - then pass to the record layer
3206 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003207 * DTLS: except for HelloRequest, messages are only queued, and will only be
3208 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003209 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003210 * Inputs:
3211 * - ssl->out_msglen: 4 + actual handshake message len
3212 * (4 is the size of handshake headers for TLS)
3213 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3214 * - ssl->out_msg + 4: the handshake message body
3215 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003216 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003217 * - ssl->out_msglen: the length of the record contents
3218 * (including handshake headers but excluding record headers)
3219 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003220 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003221int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003222{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223 int ret;
3224 const size_t hs_len = ssl->out_msglen - 4;
3225 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3228
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003229 /*
3230 * Sanity checks
3231 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003232 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003233 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3234 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003235 /* In SSLv3, the client might send a NoCertificate alert. */
3236#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3237 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3238 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3239 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3240#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3241 {
3242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3244 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003247 /* Whenever we send anything different from a
3248 * HelloRequest we should be in a handshake - double check. */
3249 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3250 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003251 ssl->handshake == NULL )
3252 {
3253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003259 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003264 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003265#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003266
Hanno Beckerb50a2532018-08-06 11:52:54 +01003267 /* Double-check that we did not exceed the bounds
3268 * of the outgoing record buffer.
3269 * This should never fail as the various message
3270 * writing functions must obey the bounds of the
3271 * outgoing record buffer, but better be safe.
3272 *
3273 * Note: We deliberately do not check for the MTU or MFL here.
3274 */
3275 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3276 {
3277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3278 "size %u, maximum %u",
3279 (unsigned) ssl->out_msglen,
3280 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3282 }
3283
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003284 /*
3285 * Fill handshake headers
3286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003289 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3290 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3291 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003293 /*
3294 * DTLS has additional fields in the Handshake layer,
3295 * between the length field and the actual payload:
3296 * uint16 message_seq;
3297 * uint24 fragment_offset;
3298 * uint24 fragment_length;
3299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003302 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003303 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003304 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003305 {
3306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3307 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003308 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003309 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3311 }
3312
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003313 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003314 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003315
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003316 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003317 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003318 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003319 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3320 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3321 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003322 }
3323 else
3324 {
3325 ssl->out_msg[4] = 0;
3326 ssl->out_msg[5] = 0;
3327 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003328
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003329 /* Handshake hashes are computed without fragmentation,
3330 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003331 memset( ssl->out_msg + 6, 0x00, 3 );
3332 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003335
Hanno Becker0207e532018-08-28 10:28:28 +01003336 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003337 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3338 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 }
3340
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003341 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003344 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3345 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346 {
3347 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350 return( ret );
3351 }
3352 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003353 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003355 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003356 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003357 {
3358 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3359 return( ret );
3360 }
3361 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003362
3363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3364
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003365 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003366}
3367
3368/*
3369 * Record layer functions
3370 */
3371
3372/*
3373 * Write current record.
3374 *
3375 * Uses:
3376 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3377 * - ssl->out_msglen: length of the record content (excl headers)
3378 * - ssl->out_msg: record content
3379 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003380int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003381{
3382 int ret, done = 0;
3383 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003384 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003385
3386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003389 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391 {
3392 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395 return( ret );
3396 }
3397
3398 len = ssl->out_msglen;
3399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3403 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 ret = mbedtls_ssl_hw_record_write( ssl );
3408 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3411 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003412 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003413
3414 if( ret == 0 )
3415 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003418 if( !done )
3419 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003420 unsigned i;
3421 size_t protected_record_size;
3422
Paul Bakker05ef8352012-05-08 09:17:57 +00003423 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003425 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003426
Hanno Becker19859472018-08-06 09:40:20 +01003427 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003428 ssl->out_len[0] = (unsigned char)( len >> 8 );
3429 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003430
Paul Bakker48916f92012-09-16 19:57:18 +00003431 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003432 {
3433 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003436 return( ret );
3437 }
3438
3439 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003440 ssl->out_len[0] = (unsigned char)( len >> 8 );
3441 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003442 }
3443
Hanno Becker2b1e3542018-08-06 11:19:13 +01003444 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3445
3446#if defined(MBEDTLS_SSL_PROTO_DTLS)
3447 /* In case of DTLS, double-check that we don't exceed
3448 * the remaining space in the datagram. */
3449 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3450 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003451 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003452 if( ret < 0 )
3453 return( ret );
3454
3455 if( protected_record_size > (size_t) ret )
3456 {
3457 /* Should never happen */
3458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3459 }
3460 }
3461#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003464 "version = [%d:%d], msglen = %d",
3465 ssl->out_hdr[0], ssl->out_hdr[1],
3466 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003469 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003470
3471 ssl->out_left += protected_record_size;
3472 ssl->out_hdr += protected_record_size;
3473 ssl_update_out_pointers( ssl, ssl->transform_out );
3474
Hanno Becker04484622018-08-06 09:49:38 +01003475 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3476 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3477 break;
3478
3479 /* The loop goes to its end iff the counter is wrapping */
3480 if( i == ssl_ep_len( ssl ) )
3481 {
3482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3483 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 }
3486
Hanno Becker67bc7c32018-08-06 11:33:50 +01003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3489 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003490 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003491 size_t remaining;
3492 ret = ssl_get_remaining_payload_in_datagram( ssl );
3493 if( ret < 0 )
3494 {
3495 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3496 ret );
3497 return( ret );
3498 }
3499
3500 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003502 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003503 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003504 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 else
3506 {
Hanno Becker513815a2018-08-20 11:56:09 +01003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003508 }
3509 }
3510#endif /* MBEDTLS_SSL_PROTO_DTLS */
3511
3512 if( ( flush == SSL_FORCE_FLUSH ) &&
3513 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 return( ret );
3517 }
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
3521 return( 0 );
3522}
3523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003525
3526static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3527{
3528 if( ssl->in_msglen < ssl->in_hslen ||
3529 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3530 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3531 {
3532 return( 1 );
3533 }
3534 return( 0 );
3535}
Hanno Becker44650b72018-08-16 12:51:11 +01003536
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003537static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003538{
3539 return( ( ssl->in_msg[9] << 16 ) |
3540 ( ssl->in_msg[10] << 8 ) |
3541 ssl->in_msg[11] );
3542}
3543
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003544static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003545{
3546 return( ( ssl->in_msg[6] << 16 ) |
3547 ( ssl->in_msg[7] << 8 ) |
3548 ssl->in_msg[8] );
3549}
3550
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003551static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003552{
3553 uint32_t msg_len, frag_off, frag_len;
3554
3555 msg_len = ssl_get_hs_total_len( ssl );
3556 frag_off = ssl_get_hs_frag_off( ssl );
3557 frag_len = ssl_get_hs_frag_len( ssl );
3558
3559 if( frag_off > msg_len )
3560 return( -1 );
3561
3562 if( frag_len > msg_len - frag_off )
3563 return( -1 );
3564
3565 if( frag_len + 12 > ssl->in_msglen )
3566 return( -1 );
3567
3568 return( 0 );
3569}
3570
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003571/*
3572 * Mark bits in bitmask (used for DTLS HS reassembly)
3573 */
3574static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3575{
3576 unsigned int start_bits, end_bits;
3577
3578 start_bits = 8 - ( offset % 8 );
3579 if( start_bits != 8 )
3580 {
3581 size_t first_byte_idx = offset / 8;
3582
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003583 /* Special case */
3584 if( len <= start_bits )
3585 {
3586 for( ; len != 0; len-- )
3587 mask[first_byte_idx] |= 1 << ( start_bits - len );
3588
3589 /* Avoid potential issues with offset or len becoming invalid */
3590 return;
3591 }
3592
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003593 offset += start_bits; /* Now offset % 8 == 0 */
3594 len -= start_bits;
3595
3596 for( ; start_bits != 0; start_bits-- )
3597 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3598 }
3599
3600 end_bits = len % 8;
3601 if( end_bits != 0 )
3602 {
3603 size_t last_byte_idx = ( offset + len ) / 8;
3604
3605 len -= end_bits; /* Now len % 8 == 0 */
3606
3607 for( ; end_bits != 0; end_bits-- )
3608 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3609 }
3610
3611 memset( mask + offset / 8, 0xFF, len / 8 );
3612}
3613
3614/*
3615 * Check that bitmask is full
3616 */
3617static int ssl_bitmask_check( unsigned char *mask, size_t len )
3618{
3619 size_t i;
3620
3621 for( i = 0; i < len / 8; i++ )
3622 if( mask[i] != 0xFF )
3623 return( -1 );
3624
3625 for( i = 0; i < len % 8; i++ )
3626 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3627 return( -1 );
3628
3629 return( 0 );
3630}
3631
Hanno Becker56e205e2018-08-16 09:06:12 +01003632/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003633static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003634 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003635{
Hanno Becker56e205e2018-08-16 09:06:12 +01003636 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003637
Hanno Becker56e205e2018-08-16 09:06:12 +01003638 alloc_len = 12; /* Handshake header */
3639 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003640
Hanno Beckerd07df862018-08-16 09:14:58 +01003641 if( add_bitmap )
3642 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003643
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003644 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003645}
Hanno Becker56e205e2018-08-16 09:06:12 +01003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003648
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003649static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003650{
3651 return( ( ssl->in_msg[1] << 16 ) |
3652 ( ssl->in_msg[2] << 8 ) |
3653 ssl->in_msg[3] );
3654}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003655
Simon Butcher99000142016-10-13 17:21:01 +01003656int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003657{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003661 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003663 }
3664
Hanno Becker12555c62018-08-16 12:47:53 +01003665 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003668 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003669 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003672 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003673 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003674 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003675 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003676
Hanno Becker44650b72018-08-16 12:51:11 +01003677 if( ssl_check_hs_header( ssl ) != 0 )
3678 {
3679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3681 }
3682
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003683 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003684 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3685 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3686 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3687 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003688 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003689 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3690 {
3691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3692 recv_msg_seq,
3693 ssl->handshake->in_msg_seq ) );
3694 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3695 }
3696
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003697 /* Retransmit only on last message from previous flight, to avoid
3698 * too many retransmissions.
3699 * Besides, No sane server ever retransmits HelloVerifyRequest */
3700 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003704 "message_seq = %d, start_of_flight = %d",
3705 recv_msg_seq,
3706 ssl->handshake->in_flight_start_seq ) );
3707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003711 return( ret );
3712 }
3713 }
3714 else
3715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003717 "message_seq = %d, expected = %d",
3718 recv_msg_seq,
3719 ssl->handshake->in_msg_seq ) );
3720 }
3721
Hanno Becker90333da2017-10-10 11:27:13 +01003722 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003723 }
3724 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003725
Hanno Becker6d97ef52018-08-16 13:09:04 +01003726 /* Message reassembly is handled alongside buffering of future
3727 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003728 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003729 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003730 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003733 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003734 }
3735 }
3736 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003738 /* With TLS we don't handle fragmentation (for now) */
3739 if( ssl->in_msglen < ssl->in_hslen )
3740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3742 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003743 }
3744
Simon Butcher99000142016-10-13 17:21:01 +01003745 return( 0 );
3746}
3747
3748void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3749{
Hanno Becker0271f962018-08-16 13:23:47 +01003750 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003751
Hanno Becker0271f962018-08-16 13:23:47 +01003752 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003753 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003754 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003755 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003756
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003757 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003760 ssl->handshake != NULL )
3761 {
Hanno Becker0271f962018-08-16 13:23:47 +01003762 unsigned offset;
3763 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003764
Hanno Becker0271f962018-08-16 13:23:47 +01003765 /* Increment handshake sequence number */
3766 hs->in_msg_seq++;
3767
3768 /*
3769 * Clear up handshake buffering and reassembly structure.
3770 */
3771
3772 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003773 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003774
3775 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003776 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3777 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003778 offset++, hs_buf++ )
3779 {
3780 *hs_buf = *(hs_buf + 1);
3781 }
3782
3783 /* Create a fresh last entry */
3784 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003785 }
3786#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003787}
3788
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003789/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003790 * DTLS anti-replay: RFC 6347 4.1.2.6
3791 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003792 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3793 * Bit n is set iff record number in_window_top - n has been seen.
3794 *
3795 * Usually, in_window_top is the last record number seen and the lsb of
3796 * in_window is set. The only exception is the initial state (record number 0
3797 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3800static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801{
3802 ssl->in_window_top = 0;
3803 ssl->in_window = 0;
3804}
3805
3806static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3807{
3808 return( ( (uint64_t) buf[0] << 40 ) |
3809 ( (uint64_t) buf[1] << 32 ) |
3810 ( (uint64_t) buf[2] << 24 ) |
3811 ( (uint64_t) buf[3] << 16 ) |
3812 ( (uint64_t) buf[4] << 8 ) |
3813 ( (uint64_t) buf[5] ) );
3814}
3815
3816/*
3817 * Return 0 if sequence number is acceptable, -1 otherwise
3818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003820{
3821 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3822 uint64_t bit;
3823
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003824 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003825 return( 0 );
3826
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003827 if( rec_seqnum > ssl->in_window_top )
3828 return( 0 );
3829
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003830 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003831
3832 if( bit >= 64 )
3833 return( -1 );
3834
3835 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3836 return( -1 );
3837
3838 return( 0 );
3839}
3840
3841/*
3842 * Update replay window on new validated record
3843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003845{
3846 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3847
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003848 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003849 return;
3850
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003851 if( rec_seqnum > ssl->in_window_top )
3852 {
3853 /* Update window_top and the contents of the window */
3854 uint64_t shift = rec_seqnum - ssl->in_window_top;
3855
3856 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003857 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003858 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003859 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003860 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003861 ssl->in_window |= 1;
3862 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003863
3864 ssl->in_window_top = rec_seqnum;
3865 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003866 else
3867 {
3868 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003869 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003870
3871 if( bit < 64 ) /* Always true, but be extra sure */
3872 ssl->in_window |= (uint64_t) 1 << bit;
3873 }
3874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003876
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003877#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003878/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003879static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3880
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003881/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003882 * Without any SSL context, check if a datagram looks like a ClientHello with
3883 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003884 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003885 *
3886 * - if cookie is valid, return 0
3887 * - if ClientHello looks superficially valid but cookie is not,
3888 * fill obuf and set olen, then
3889 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3890 * - otherwise return a specific error code
3891 */
3892static int ssl_check_dtls_clihlo_cookie(
3893 mbedtls_ssl_cookie_write_t *f_cookie_write,
3894 mbedtls_ssl_cookie_check_t *f_cookie_check,
3895 void *p_cookie,
3896 const unsigned char *cli_id, size_t cli_id_len,
3897 const unsigned char *in, size_t in_len,
3898 unsigned char *obuf, size_t buf_len, size_t *olen )
3899{
3900 size_t sid_len, cookie_len;
3901 unsigned char *p;
3902
3903 if( f_cookie_write == NULL || f_cookie_check == NULL )
3904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3905
3906 /*
3907 * Structure of ClientHello with record and handshake headers,
3908 * and expected values. We don't need to check a lot, more checks will be
3909 * done when actually parsing the ClientHello - skipping those checks
3910 * avoids code duplication and does not make cookie forging any easier.
3911 *
3912 * 0-0 ContentType type; copied, must be handshake
3913 * 1-2 ProtocolVersion version; copied
3914 * 3-4 uint16 epoch; copied, must be 0
3915 * 5-10 uint48 sequence_number; copied
3916 * 11-12 uint16 length; (ignored)
3917 *
3918 * 13-13 HandshakeType msg_type; (ignored)
3919 * 14-16 uint24 length; (ignored)
3920 * 17-18 uint16 message_seq; copied
3921 * 19-21 uint24 fragment_offset; copied, must be 0
3922 * 22-24 uint24 fragment_length; (ignored)
3923 *
3924 * 25-26 ProtocolVersion client_version; (ignored)
3925 * 27-58 Random random; (ignored)
3926 * 59-xx SessionID session_id; 1 byte len + sid_len content
3927 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3928 * ...
3929 *
3930 * Minimum length is 61 bytes.
3931 */
3932 if( in_len < 61 ||
3933 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3934 in[3] != 0 || in[4] != 0 ||
3935 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3936 {
3937 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3938 }
3939
3940 sid_len = in[59];
3941 if( sid_len > in_len - 61 )
3942 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3943
3944 cookie_len = in[60 + sid_len];
3945 if( cookie_len > in_len - 60 )
3946 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3947
3948 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3949 cli_id, cli_id_len ) == 0 )
3950 {
3951 /* Valid cookie */
3952 return( 0 );
3953 }
3954
3955 /*
3956 * If we get here, we've got an invalid cookie, let's prepare HVR.
3957 *
3958 * 0-0 ContentType type; copied
3959 * 1-2 ProtocolVersion version; copied
3960 * 3-4 uint16 epoch; copied
3961 * 5-10 uint48 sequence_number; copied
3962 * 11-12 uint16 length; olen - 13
3963 *
3964 * 13-13 HandshakeType msg_type; hello_verify_request
3965 * 14-16 uint24 length; olen - 25
3966 * 17-18 uint16 message_seq; copied
3967 * 19-21 uint24 fragment_offset; copied
3968 * 22-24 uint24 fragment_length; olen - 25
3969 *
3970 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3971 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3972 *
3973 * Minimum length is 28.
3974 */
3975 if( buf_len < 28 )
3976 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3977
3978 /* Copy most fields and adapt others */
3979 memcpy( obuf, in, 25 );
3980 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3981 obuf[25] = 0xfe;
3982 obuf[26] = 0xff;
3983
3984 /* Generate and write actual cookie */
3985 p = obuf + 28;
3986 if( f_cookie_write( p_cookie,
3987 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3988 {
3989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3990 }
3991
3992 *olen = p - obuf;
3993
3994 /* Go back and fill length fields */
3995 obuf[27] = (unsigned char)( *olen - 28 );
3996
3997 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3998 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3999 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4000
4001 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4002 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4003
4004 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4005}
4006
4007/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004008 * Handle possible client reconnect with the same UDP quadruplet
4009 * (RFC 6347 Section 4.2.8).
4010 *
4011 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4012 * that looks like a ClientHello.
4013 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004014 * - if the input looks like a ClientHello without cookies,
4015 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004016 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004017 * - if the input looks like a ClientHello with a valid cookie,
4018 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004019 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004020 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004021 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004022 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004023 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4024 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004025 */
4026static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4027{
4028 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004029 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004030
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004031 ret = ssl_check_dtls_clihlo_cookie(
4032 ssl->conf->f_cookie_write,
4033 ssl->conf->f_cookie_check,
4034 ssl->conf->p_cookie,
4035 ssl->cli_id, ssl->cli_id_len,
4036 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004037 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004038
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004039 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4040
4041 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004042 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004043 int send_ret;
4044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4045 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4046 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004047 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004048 * If the error is permanent we'll catch it later,
4049 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004050 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4051 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4052 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004053
4054 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004055 }
4056
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004057 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004058 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004060 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4061 {
4062 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4063 return( ret );
4064 }
4065
4066 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004067 }
4068
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004069 return( ret );
4070}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004071#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004072
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004073/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004074 * ContentType type;
4075 * ProtocolVersion version;
4076 * uint16 epoch; // DTLS only
4077 * uint48 sequence_number; // DTLS only
4078 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004079 *
4080 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004081 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004082 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4083 *
4084 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004085 * 1. proceed with the record if this function returns 0
4086 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4087 * 3. return CLIENT_RECONNECT if this function return that value
4088 * 4. drop the whole datagram if this function returns anything else.
4089 * Point 2 is needed when the peer is resending, and we have already received
4090 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004093{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004094 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 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 +02004097
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004099 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004100 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004104 ssl->in_msgtype,
4105 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004106
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004107 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4109 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4110 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4111 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004114
4115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004116 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4117 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004118 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4119#endif /* MBEDTLS_SSL_PROTO_DTLS */
4120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4121 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004124 }
4125
4126 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004127 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4130 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 }
4132
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004133 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4136 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004137 }
4138
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004139 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004140 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004141 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4144 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004145 }
4146
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004147 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004148 * DTLS-related tests.
4149 * Check epoch before checking length constraint because
4150 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4151 * message gets duplicated before the corresponding Finished message,
4152 * the second ChangeCipherSpec should be discarded because it belongs
4153 * to an old epoch, but not because its length is shorter than
4154 * the minimum record length for packets using the new record transform.
4155 * Note that these two kinds of failures are handled differently,
4156 * as an unexpected record is silently skipped but an invalid
4157 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004158 */
4159#if defined(MBEDTLS_SSL_PROTO_DTLS)
4160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4161 {
4162 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4163
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004164 /* Check epoch (and sequence number) with DTLS */
4165 if( rec_epoch != ssl->in_epoch )
4166 {
4167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4168 "expected %d, received %d",
4169 ssl->in_epoch, rec_epoch ) );
4170
4171#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4172 /*
4173 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4174 * access the first byte of record content (handshake type), as we
4175 * have an active transform (possibly iv_len != 0), so use the
4176 * fact that the record header len is 13 instead.
4177 */
4178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4179 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4180 rec_epoch == 0 &&
4181 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4182 ssl->in_left > 13 &&
4183 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4184 {
4185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4186 "from the same port" ) );
4187 return( ssl_handle_possible_reconnect( ssl ) );
4188 }
4189 else
4190#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004191 {
4192 /* Consider buffering the record. */
4193 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4194 {
4195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4196 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4197 }
4198
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004199 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004200 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004201 }
4202
4203#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4204 /* Replay detection only works for the current epoch */
4205 if( rec_epoch == ssl->in_epoch &&
4206 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4207 {
4208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4209 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4210 }
4211#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004212
Hanno Becker52c6dc62017-05-26 16:07:36 +01004213 /* Drop unexpected ApplicationData records,
4214 * except at the beginning of renegotiations */
4215 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4216 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4217#if defined(MBEDTLS_SSL_RENEGOTIATION)
4218 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4219 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4220#endif
4221 )
4222 {
4223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4224 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4225 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004226 }
4227#endif /* MBEDTLS_SSL_PROTO_DTLS */
4228
Hanno Becker52c6dc62017-05-26 16:07:36 +01004229
4230 /* Check length against bounds of the current transform and version */
4231 if( ssl->transform_in == NULL )
4232 {
4233 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004234 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004235 {
4236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4237 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4238 }
4239 }
4240 else
4241 {
4242 if( ssl->in_msglen < ssl->transform_in->minlen )
4243 {
4244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4245 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4246 }
4247
4248#if defined(MBEDTLS_SSL_PROTO_SSL3)
4249 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004250 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004251 {
4252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4253 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4254 }
4255#endif
4256#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4257 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4258 /*
4259 * TLS encrypted messages can have up to 256 bytes of padding
4260 */
4261 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4262 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004263 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004264 {
4265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4266 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4267 }
4268#endif
4269 }
4270
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004271 return( 0 );
4272}
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004274/*
4275 * If applicable, decrypt (and decompress) record content
4276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004278{
4279 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4282 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4285 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 ret = mbedtls_ssl_hw_record_read( ssl );
4290 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4293 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004294 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004295
4296 if( ret == 0 )
4297 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004300 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 {
4302 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305 return( ret );
4306 }
4307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 ssl->in_msg, ssl->in_msglen );
4310
Angus Grattond8213d02016-05-25 20:56:48 +10004311 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4314 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 }
4316 }
4317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004319 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004321 {
4322 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004325 return( ret );
4326 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004327 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004334 }
4335#endif
4336
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004337 return( 0 );
4338}
4339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004341
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004342/*
4343 * Read a record.
4344 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004345 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4346 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4347 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004348 */
Hanno Becker1097b342018-08-15 14:09:41 +01004349
4350/* Helper functions for mbedtls_ssl_read_record(). */
4351static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004352static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4353static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004354
Hanno Becker327c93b2018-08-15 13:56:18 +01004355int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004356 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004357{
4358 int ret;
4359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004361
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004362 if( ssl->keep_current_message == 0 )
4363 {
4364 do {
Simon Butcher99000142016-10-13 17:21:01 +01004365
Hanno Becker26994592018-08-15 14:14:59 +01004366 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004367 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004368 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004369
Hanno Beckere74d5562018-08-15 14:26:08 +01004370 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004371 {
Hanno Becker40f50842018-08-15 14:48:01 +01004372#if defined(MBEDTLS_SSL_PROTO_DTLS)
4373 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004374
Hanno Becker40f50842018-08-15 14:48:01 +01004375 /* We only check for buffered messages if the
4376 * current datagram is fully consumed. */
4377 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004378 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004379 {
Hanno Becker40f50842018-08-15 14:48:01 +01004380 if( ssl_load_buffered_message( ssl ) == 0 )
4381 have_buffered = 1;
4382 }
4383
4384 if( have_buffered == 0 )
4385#endif /* MBEDTLS_SSL_PROTO_DTLS */
4386 {
4387 ret = ssl_get_next_record( ssl );
4388 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4389 continue;
4390
4391 if( ret != 0 )
4392 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004393 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004394 return( ret );
4395 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004396 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004397 }
4398
4399 ret = mbedtls_ssl_handle_message_type( ssl );
4400
Hanno Becker40f50842018-08-15 14:48:01 +01004401#if defined(MBEDTLS_SSL_PROTO_DTLS)
4402 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4403 {
4404 /* Buffer future message */
4405 ret = ssl_buffer_message( ssl );
4406 if( ret != 0 )
4407 return( ret );
4408
4409 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4410 }
4411#endif /* MBEDTLS_SSL_PROTO_DTLS */
4412
Hanno Becker90333da2017-10-10 11:27:13 +01004413 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4414 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004415
4416 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004417 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004418 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004419 return( ret );
4420 }
4421
Hanno Becker327c93b2018-08-15 13:56:18 +01004422 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004423 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004424 {
4425 mbedtls_ssl_update_handshake_status( ssl );
4426 }
Simon Butcher99000142016-10-13 17:21:01 +01004427 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004428 else
Simon Butcher99000142016-10-13 17:21:01 +01004429 {
Hanno Becker02f59072018-08-15 14:00:24 +01004430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004431 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004432 }
4433
4434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4435
4436 return( 0 );
4437}
4438
Hanno Becker40f50842018-08-15 14:48:01 +01004439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004440static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004441{
Hanno Becker40f50842018-08-15 14:48:01 +01004442 if( ssl->in_left > ssl->next_record_offset )
4443 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004444
Hanno Becker40f50842018-08-15 14:48:01 +01004445 return( 0 );
4446}
4447
4448static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4449{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004450 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004451 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004452 int ret = 0;
4453
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004454 if( hs == NULL )
4455 return( -1 );
4456
Hanno Beckere00ae372018-08-20 09:39:42 +01004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4458
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004459 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4460 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4461 {
4462 /* Check if we have seen a ChangeCipherSpec before.
4463 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004464 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004465 {
4466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4467 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004468 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004469 }
4470
Hanno Becker39b8bc92018-08-28 17:17:13 +01004471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004472 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4473 ssl->in_msglen = 1;
4474 ssl->in_msg[0] = 1;
4475
4476 /* As long as they are equal, the exact value doesn't matter. */
4477 ssl->in_left = 0;
4478 ssl->next_record_offset = 0;
4479
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004480 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004481 goto exit;
4482 }
Hanno Becker37f95322018-08-16 13:55:32 +01004483
Hanno Beckerb8f50142018-08-28 10:01:34 +01004484#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004485 /* Debug only */
4486 {
4487 unsigned offset;
4488 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4489 {
4490 hs_buf = &hs->buffering.hs[offset];
4491 if( hs_buf->is_valid == 1 )
4492 {
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4494 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004495 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004496 }
4497 }
4498 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004499#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004500
4501 /* Check if we have buffered and/or fully reassembled the
4502 * next handshake message. */
4503 hs_buf = &hs->buffering.hs[0];
4504 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4505 {
4506 /* Synthesize a record containing the buffered HS message. */
4507 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4508 ( hs_buf->data[2] << 8 ) |
4509 hs_buf->data[3];
4510
4511 /* Double-check that we haven't accidentally buffered
4512 * a message that doesn't fit into the input buffer. */
4513 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4517 }
4518
4519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4520 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4521 hs_buf->data, msg_len + 12 );
4522
4523 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4524 ssl->in_hslen = msg_len + 12;
4525 ssl->in_msglen = msg_len + 12;
4526 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4527
4528 ret = 0;
4529 goto exit;
4530 }
4531 else
4532 {
4533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4534 hs->in_msg_seq ) );
4535 }
4536
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004537 ret = -1;
4538
4539exit:
4540
4541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4542 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004543}
4544
Hanno Beckera02b0b42018-08-21 17:20:27 +01004545static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4546 size_t desired )
4547{
4548 int offset;
4549 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4551 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004552
Hanno Becker01315ea2018-08-21 17:22:17 +01004553 /* Get rid of future records epoch first, if such exist. */
4554 ssl_free_buffered_record( ssl );
4555
4556 /* Check if we have enough space available now. */
4557 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4558 hs->buffering.total_bytes_buffered ) )
4559 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004561 return( 0 );
4562 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004563
Hanno Becker4f432ad2018-08-28 10:02:32 +01004564 /* We don't have enough space to buffer the next expected handshake
4565 * message. Remove buffers used for future messages to gain space,
4566 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004567 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4568 offset >= 0; offset-- )
4569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4571 offset ) );
4572
Hanno Beckerb309b922018-08-23 13:18:05 +01004573 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004574
4575 /* Check if we have enough space available now. */
4576 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4577 hs->buffering.total_bytes_buffered ) )
4578 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004580 return( 0 );
4581 }
4582 }
4583
4584 return( -1 );
4585}
4586
Hanno Becker40f50842018-08-15 14:48:01 +01004587static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4588{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004589 int ret = 0;
4590 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4591
4592 if( hs == NULL )
4593 return( 0 );
4594
4595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4596
4597 switch( ssl->in_msgtype )
4598 {
4599 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004601
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004602 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004603 break;
4604
4605 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004606 {
4607 unsigned recv_msg_seq_offset;
4608 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4609 mbedtls_ssl_hs_buffer *hs_buf;
4610 size_t msg_len = ssl->in_hslen - 12;
4611
4612 /* We should never receive an old handshake
4613 * message - double-check nonetheless. */
4614 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4615 {
4616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4618 }
4619
4620 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4621 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4622 {
4623 /* Silently ignore -- message too far in the future */
4624 MBEDTLS_SSL_DEBUG_MSG( 2,
4625 ( "Ignore future HS message with sequence number %u, "
4626 "buffering window %u - %u",
4627 recv_msg_seq, ssl->handshake->in_msg_seq,
4628 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4629
4630 goto exit;
4631 }
4632
4633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4634 recv_msg_seq, recv_msg_seq_offset ) );
4635
4636 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4637
4638 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004639 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004640 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004641 size_t reassembly_buf_sz;
4642
Hanno Becker37f95322018-08-16 13:55:32 +01004643 hs_buf->is_fragmented =
4644 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4645
4646 /* We copy the message back into the input buffer
4647 * after reassembly, so check that it's not too large.
4648 * This is an implementation-specific limitation
4649 * and not one from the standard, hence it is not
4650 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004651 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004652 {
4653 /* Ignore message */
4654 goto exit;
4655 }
4656
Hanno Beckere0b150f2018-08-21 15:51:03 +01004657 /* Check if we have enough space to buffer the message. */
4658 if( hs->buffering.total_bytes_buffered >
4659 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4660 {
4661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4663 }
4664
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004665 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4666 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004667
4668 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4669 hs->buffering.total_bytes_buffered ) )
4670 {
4671 if( recv_msg_seq_offset > 0 )
4672 {
4673 /* If we can't buffer a future message because
4674 * of space limitations -- ignore. */
4675 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",
4676 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4677 (unsigned) hs->buffering.total_bytes_buffered ) );
4678 goto exit;
4679 }
Hanno Beckere1801392018-08-21 16:51:05 +01004680 else
4681 {
4682 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",
4683 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4684 (unsigned) hs->buffering.total_bytes_buffered ) );
4685 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004686
Hanno Beckera02b0b42018-08-21 17:20:27 +01004687 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004688 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004689 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",
4690 (unsigned) msg_len,
4691 (unsigned) reassembly_buf_sz,
4692 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004693 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004694 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4695 goto exit;
4696 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004697 }
4698
4699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4700 msg_len ) );
4701
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004702 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4703 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004704 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004705 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004706 goto exit;
4707 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004708 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004709
4710 /* Prepare final header: copy msg_type, length and message_seq,
4711 * then add standardised fragment_offset and fragment_length */
4712 memcpy( hs_buf->data, ssl->in_msg, 6 );
4713 memset( hs_buf->data + 6, 0, 3 );
4714 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4715
4716 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004717
4718 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004719 }
4720 else
4721 {
4722 /* Make sure msg_type and length are consistent */
4723 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4724 {
4725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4726 /* Ignore */
4727 goto exit;
4728 }
4729 }
4730
Hanno Becker4422bbb2018-08-20 09:40:19 +01004731 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004732 {
4733 size_t frag_len, frag_off;
4734 unsigned char * const msg = hs_buf->data + 12;
4735
4736 /*
4737 * Check and copy current fragment
4738 */
4739
4740 /* Validation of header fields already done in
4741 * mbedtls_ssl_prepare_handshake_record(). */
4742 frag_off = ssl_get_hs_frag_off( ssl );
4743 frag_len = ssl_get_hs_frag_len( ssl );
4744
4745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4746 frag_off, frag_len ) );
4747 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4748
4749 if( hs_buf->is_fragmented )
4750 {
4751 unsigned char * const bitmask = msg + msg_len;
4752 ssl_bitmask_set( bitmask, frag_off, frag_len );
4753 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4754 msg_len ) == 0 );
4755 }
4756 else
4757 {
4758 hs_buf->is_complete = 1;
4759 }
4760
4761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4762 hs_buf->is_complete ? "" : "not yet " ) );
4763 }
4764
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004765 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004766 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004767
4768 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004769 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004770 break;
4771 }
4772
4773exit:
4774
4775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4776 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004777}
4778#endif /* MBEDTLS_SSL_PROTO_DTLS */
4779
Hanno Becker1097b342018-08-15 14:09:41 +01004780static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004781{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004782 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004783 * Consume last content-layer message and potentially
4784 * update in_msglen which keeps track of the contents'
4785 * consumption state.
4786 *
4787 * (1) Handshake messages:
4788 * Remove last handshake message, move content
4789 * and adapt in_msglen.
4790 *
4791 * (2) Alert messages:
4792 * Consume whole record content, in_msglen = 0.
4793 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004794 * (3) Change cipher spec:
4795 * Consume whole record content, in_msglen = 0.
4796 *
4797 * (4) Application data:
4798 * Don't do anything - the record layer provides
4799 * the application data as a stream transport
4800 * and consumes through mbedtls_ssl_read only.
4801 *
4802 */
4803
4804 /* Case (1): Handshake messages */
4805 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004807 /* Hard assertion to be sure that no application data
4808 * is in flight, as corrupting ssl->in_msglen during
4809 * ssl->in_offt != NULL is fatal. */
4810 if( ssl->in_offt != NULL )
4811 {
4812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4814 }
4815
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004816 /*
4817 * Get next Handshake message in the current record
4818 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004819
Hanno Becker4a810fb2017-05-24 16:27:30 +01004820 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004821 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004822 * current handshake content: If DTLS handshake
4823 * fragmentation is used, that's the fragment
4824 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004825 * size here is faulty and should be changed at
4826 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004827 * (2) While it doesn't seem to cause problems, one
4828 * has to be very careful not to assume that in_hslen
4829 * is always <= in_msglen in a sensible communication.
4830 * Again, it's wrong for DTLS handshake fragmentation.
4831 * The following check is therefore mandatory, and
4832 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004833 * Additionally, ssl->in_hslen might be arbitrarily out of
4834 * bounds after handling a DTLS message with an unexpected
4835 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004836 */
4837 if( ssl->in_hslen < ssl->in_msglen )
4838 {
4839 ssl->in_msglen -= ssl->in_hslen;
4840 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4841 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004842
Hanno Becker4a810fb2017-05-24 16:27:30 +01004843 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4844 ssl->in_msg, ssl->in_msglen );
4845 }
4846 else
4847 {
4848 ssl->in_msglen = 0;
4849 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004850
Hanno Becker4a810fb2017-05-24 16:27:30 +01004851 ssl->in_hslen = 0;
4852 }
4853 /* Case (4): Application data */
4854 else if( ssl->in_offt != NULL )
4855 {
4856 return( 0 );
4857 }
4858 /* Everything else (CCS & Alerts) */
4859 else
4860 {
4861 ssl->in_msglen = 0;
4862 }
4863
Hanno Becker1097b342018-08-15 14:09:41 +01004864 return( 0 );
4865}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004866
Hanno Beckere74d5562018-08-15 14:26:08 +01004867static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4868{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004869 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004870 return( 1 );
4871
4872 return( 0 );
4873}
4874
Hanno Becker5f066e72018-08-16 14:56:31 +01004875#if defined(MBEDTLS_SSL_PROTO_DTLS)
4876
4877static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4878{
4879 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4880 if( hs == NULL )
4881 return;
4882
Hanno Becker01315ea2018-08-21 17:22:17 +01004883 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004884 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004885 hs->buffering.total_bytes_buffered -=
4886 hs->buffering.future_record.len;
4887
4888 mbedtls_free( hs->buffering.future_record.data );
4889 hs->buffering.future_record.data = NULL;
4890 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004891}
4892
4893static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4894{
4895 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4896 unsigned char * rec;
4897 size_t rec_len;
4898 unsigned rec_epoch;
4899
4900 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4901 return( 0 );
4902
4903 if( hs == NULL )
4904 return( 0 );
4905
Hanno Becker5f066e72018-08-16 14:56:31 +01004906 rec = hs->buffering.future_record.data;
4907 rec_len = hs->buffering.future_record.len;
4908 rec_epoch = hs->buffering.future_record.epoch;
4909
4910 if( rec == NULL )
4911 return( 0 );
4912
Hanno Becker4cb782d2018-08-20 11:19:05 +01004913 /* Only consider loading future records if the
4914 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004915 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004916 return( 0 );
4917
Hanno Becker5f066e72018-08-16 14:56:31 +01004918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4919
4920 if( rec_epoch != ssl->in_epoch )
4921 {
4922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4923 goto exit;
4924 }
4925
4926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4927
4928 /* Double-check that the record is not too large */
4929 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4930 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4931 {
4932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4934 }
4935
4936 memcpy( ssl->in_hdr, rec, rec_len );
4937 ssl->in_left = rec_len;
4938 ssl->next_record_offset = 0;
4939
4940 ssl_free_buffered_record( ssl );
4941
4942exit:
4943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4944 return( 0 );
4945}
4946
4947static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4948{
4949 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4950 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004951 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004952
4953 /* Don't buffer future records outside handshakes. */
4954 if( hs == NULL )
4955 return( 0 );
4956
4957 /* Only buffer handshake records (we are only interested
4958 * in Finished messages). */
4959 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4960 return( 0 );
4961
4962 /* Don't buffer more than one future epoch record. */
4963 if( hs->buffering.future_record.data != NULL )
4964 return( 0 );
4965
Hanno Becker01315ea2018-08-21 17:22:17 +01004966 /* Don't buffer record if there's not enough buffering space remaining. */
4967 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4968 hs->buffering.total_bytes_buffered ) )
4969 {
4970 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",
4971 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4972 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973 return( 0 );
4974 }
4975
Hanno Becker5f066e72018-08-16 14:56:31 +01004976 /* Buffer record */
4977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4978 ssl->in_epoch + 1 ) );
4979 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4980 rec_hdr_len + ssl->in_msglen );
4981
4982 /* ssl_parse_record_header() only considers records
4983 * of the next epoch as candidates for buffering. */
4984 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004985 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004986
4987 hs->buffering.future_record.data =
4988 mbedtls_calloc( 1, hs->buffering.future_record.len );
4989 if( hs->buffering.future_record.data == NULL )
4990 {
4991 /* If we run out of RAM trying to buffer a
4992 * record from the next epoch, just ignore. */
4993 return( 0 );
4994 }
4995
Hanno Becker01315ea2018-08-21 17:22:17 +01004996 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004997
Hanno Becker01315ea2018-08-21 17:22:17 +01004998 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004999 return( 0 );
5000}
5001
5002#endif /* MBEDTLS_SSL_PROTO_DTLS */
5003
Hanno Beckere74d5562018-08-15 14:26:08 +01005004static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005005{
5006 int ret;
5007
Hanno Becker5f066e72018-08-16 14:56:31 +01005008#if defined(MBEDTLS_SSL_PROTO_DTLS)
5009 /* We might have buffered a future record; if so,
5010 * and if the epoch matches now, load it.
5011 * On success, this call will set ssl->in_left to
5012 * the length of the buffered record, so that
5013 * the calls to ssl_fetch_input() below will
5014 * essentially be no-ops. */
5015 ret = ssl_load_buffered_record( ssl );
5016 if( ret != 0 )
5017 return( ret );
5018#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005023 return( ret );
5024 }
5025
5026 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5030 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005031 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005032 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5033 {
5034 ret = ssl_buffer_future_record( ssl );
5035 if( ret != 0 )
5036 return( ret );
5037
5038 /* Fall through to handling of unexpected records */
5039 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5040 }
5041
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005042 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5043 {
5044 /* Skip unexpected record (but not whole datagram) */
5045 ssl->next_record_offset = ssl->in_msglen
5046 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005047
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5049 "(header)" ) );
5050 }
5051 else
5052 {
5053 /* Skip invalid record and the rest of the datagram */
5054 ssl->next_record_offset = 0;
5055 ssl->in_left = 0;
5056
5057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5058 "(header)" ) );
5059 }
5060
5061 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005062 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005063 }
5064#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005065 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005066 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005067
5068 /*
5069 * Read and optionally decrypt the message contents
5070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5072 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005075 return( ret );
5076 }
5077
5078 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005080 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005083 if( ssl->next_record_offset < ssl->in_left )
5084 {
5085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5086 }
5087 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005088 else
5089#endif
5090 ssl->in_left = 0;
5091
5092 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005096 {
5097 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5099 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005100 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005101 /* Except when waiting for Finished as a bad mac here
5102 * probably means something went wrong in the handshake
5103 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5104 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5105 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5106 {
5107#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5108 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5109 {
5110 mbedtls_ssl_send_alert_message( ssl,
5111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5112 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5113 }
5114#endif
5115 return( ret );
5116 }
5117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005119 if( ssl->conf->badmac_limit != 0 &&
5120 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5123 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005124 }
5125#endif
5126
Hanno Becker4a810fb2017-05-24 16:27:30 +01005127 /* As above, invalid records cause
5128 * dismissal of the whole datagram. */
5129
5130 ssl->next_record_offset = 0;
5131 ssl->in_left = 0;
5132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005134 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005135 }
5136
5137 return( ret );
5138 }
5139 else
5140#endif
5141 {
5142 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5144 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 mbedtls_ssl_send_alert_message( ssl,
5147 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5148 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005149 }
5150#endif
5151 return( ret );
5152 }
5153 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005154
Simon Butcher99000142016-10-13 17:21:01 +01005155 return( 0 );
5156}
5157
5158int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5159{
5160 int ret;
5161
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005162 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005163 * Handle particular types of records
5164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005166 {
Simon Butcher99000142016-10-13 17:21:01 +01005167 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5168 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005169 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 }
5172
Hanno Beckere678eaa2018-08-21 14:57:46 +01005173 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005174 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005175 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5178 ssl->in_msglen ) );
5179 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005180 }
5181
Hanno Beckere678eaa2018-08-21 14:57:46 +01005182 if( ssl->in_msg[0] != 1 )
5183 {
5184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5185 ssl->in_msg[0] ) );
5186 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5187 }
5188
5189#if defined(MBEDTLS_SSL_PROTO_DTLS)
5190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5191 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5192 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5193 {
5194 if( ssl->handshake == NULL )
5195 {
5196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5197 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5198 }
5199
5200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5201 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5202 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005203#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005204 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005206 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005207 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005208 if( ssl->in_msglen != 2 )
5209 {
5210 /* Note: Standard allows for more than one 2 byte alert
5211 to be packed in a single message, but Mbed TLS doesn't
5212 currently support this. */
5213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5214 ssl->in_msglen ) );
5215 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5216 }
5217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005219 ssl->in_msg[0], ssl->in_msg[1] ) );
5220
5221 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005222 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005227 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 }
5230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5232 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5235 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005236 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005237
5238#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5239 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5240 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5241 {
Hanno Becker90333da2017-10-10 11:27:13 +01005242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005243 /* Will be handled when trying to parse ServerHello */
5244 return( 0 );
5245 }
5246#endif
5247
5248#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5249 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5250 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5251 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5252 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5253 {
5254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5255 /* Will be handled in mbedtls_ssl_parse_certificate() */
5256 return( 0 );
5257 }
5258#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5259
5260 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005261 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005262 }
5263
Hanno Beckerc76c6192017-06-06 10:03:17 +01005264#if defined(MBEDTLS_SSL_PROTO_DTLS)
5265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5266 ssl->handshake != NULL &&
5267 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5268 {
5269 ssl_handshake_wrapup_free_hs_transform( ssl );
5270 }
5271#endif
5272
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 return( 0 );
5274}
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005277{
5278 int ret;
5279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5282 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005283 {
5284 return( ret );
5285 }
5286
5287 return( 0 );
5288}
5289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005291 unsigned char level,
5292 unsigned char message )
5293{
5294 int ret;
5295
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005296 if( ssl == NULL || ssl->conf == NULL )
5297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005302 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005303 ssl->out_msglen = 2;
5304 ssl->out_msg[0] = level;
5305 ssl->out_msg[1] = message;
5306
Hanno Becker67bc7c32018-08-06 11:33:50 +01005307 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005310 return( ret );
5311 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005313
5314 return( 0 );
5315}
5316
Paul Bakker5121ce52009-01-03 21:22:43 +00005317/*
5318 * Handshake functions
5319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5321 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5322 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5323 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5324 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5325 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5326 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005327/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5335 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005336 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5337 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005340 ssl->state++;
5341 return( 0 );
5342 }
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005346}
5347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5355 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005356 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5357 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005360 ssl->state++;
5361 return( 0 );
5362 }
5363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005366}
Gilles Peskinef9828522017-05-03 12:28:43 +02005367
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005368#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005369/* Some certificate support -> implement write and parse */
5370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005374 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 const mbedtls_x509_crt *crt;
5376 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5381 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005382 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5383 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005386 ssl->state++;
5387 return( 0 );
5388 }
5389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005391 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005392 {
5393 if( ssl->client_auth == 0 )
5394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 ssl->state++;
5397 return( 0 );
5398 }
5399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 /*
5402 * If using SSLv3 and got no cert, send an Alert message
5403 * (otherwise an empty Certificate message will be sent).
5404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5406 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005407 {
5408 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5410 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5411 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 goto write_msg;
5415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#endif /* MBEDTLS_SSL_CLI_C */
5419#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005420 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5425 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005431
5432 /*
5433 * 0 . 0 handshake type
5434 * 1 . 3 handshake length
5435 * 4 . 6 length of all certs
5436 * 7 . 9 length of cert. 1
5437 * 10 . n-1 peer certificate
5438 * n . n+2 length of cert. 2
5439 * n+3 . ... upper level cert, etc.
5440 */
5441 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005443
Paul Bakker29087132010-03-21 21:03:34 +00005444 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 {
5446 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005447 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005450 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005452 }
5453
5454 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5455 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5456 ssl->out_msg[i + 2] = (unsigned char)( n );
5457
5458 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5459 i += n; crt = crt->next;
5460 }
5461
5462 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5463 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5464 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5465
5466 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5468 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005469
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005470#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005471write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005473
5474 ssl->state++;
5475
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005476 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 return( ret );
5480 }
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005483
Paul Bakkered27a042013-04-18 22:46:23 +02005484 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005485}
5486
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005487/*
5488 * Once the certificate message is read, parse it into a cert chain and
5489 * perform basic checks, but leave actual verification to the caller
5490 */
5491static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005493 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005494 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005495 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#if defined(MBEDTLS_SSL_SRV_C)
5498#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 /*
5500 * Check if the client sent an empty certificate
5501 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005502 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005505 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5507 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5508 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005511
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005512 /* The client was asked for a certificate but didn't send
5513 one. The client should know what's going on, so we
5514 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005515 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005516 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 }
5518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5522 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5527 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5528 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5529 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005532
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005533 /* The client was asked for a certificate but didn't send
5534 one. The client should know what's going on, so we
5535 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005536 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005537 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005538 }
5539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5541 MBEDTLS_SSL_PROTO_TLS1_2 */
5542#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005547 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5548 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 }
5551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5553 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5557 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 }
5560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005562
Paul Bakker5121ce52009-01-03 21:22:43 +00005563 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005566 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005567
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005568 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005572 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5573 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 }
5576
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005577 /* In case we tried to reuse a session but it failed */
5578 if( ssl->session_negotiate->peer_cert != NULL )
5579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5581 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005582 }
5583
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005584 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005589 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5590 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005592 }
5593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005595
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005596 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
5598 while( i < ssl->in_hslen )
5599 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005600 if ( i + 3 > ssl->in_hslen ) {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5603 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5604 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 if( ssl->in_msg[i] != 0 )
5607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5610 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 }
5613
5614 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5615 | (unsigned int) ssl->in_msg[i + 2];
5616 i += 3;
5617
5618 if( n < 128 || i + n > ssl->in_hslen )
5619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005621 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5622 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 }
5625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005627 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005628 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005630 case 0: /*ok*/
5631 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5632 /* Ignore certificate with an unknown algorithm: maybe a
5633 prior certificate was already trusted. */
5634 break;
5635
5636 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5637 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5638 goto crt_parse_der_failed;
5639
5640 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5641 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5642 goto crt_parse_der_failed;
5643
5644 default:
5645 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5646 crt_parse_der_failed:
5647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 return( ret );
5650 }
5651
5652 i += n;
5653 }
5654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005656
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005657 /*
5658 * On client, make sure the server cert doesn't change during renego to
5659 * avoid "triple handshake" attack: https://secure-resumption.com/
5660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005662 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005664 {
5665 if( ssl->session->peer_cert == NULL )
5666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005668 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5669 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005671 }
5672
5673 if( ssl->session->peer_cert->raw.len !=
5674 ssl->session_negotiate->peer_cert->raw.len ||
5675 memcmp( ssl->session->peer_cert->raw.p,
5676 ssl->session_negotiate->peer_cert->raw.p,
5677 ssl->session->peer_cert->raw.len ) != 0 )
5678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005680 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5681 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005683 }
5684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005686
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005687 return( 0 );
5688}
5689
5690int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5691{
5692 int ret;
5693 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5694 ssl->transform_negotiate->ciphersuite_info;
5695#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5696 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5697 ? ssl->handshake->sni_authmode
5698 : ssl->conf->authmode;
5699#else
5700 const int authmode = ssl->conf->authmode;
5701#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005702 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005703
5704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5705
5706 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5707 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5708 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5709 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5712 ssl->state++;
5713 return( 0 );
5714 }
5715
5716#if defined(MBEDTLS_SSL_SRV_C)
5717 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5718 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5719 {
5720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5721 ssl->state++;
5722 return( 0 );
5723 }
5724
5725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5726 authmode == MBEDTLS_SSL_VERIFY_NONE )
5727 {
5728 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005730
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005731 ssl->state++;
5732 return( 0 );
5733 }
5734#endif
5735
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005736#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5737 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005738 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005739 {
5740 goto crt_verify;
5741 }
5742#endif
5743
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005744 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005745 {
5746 /* mbedtls_ssl_read_record may have sent an alert already. We
5747 let it decide whether to alert. */
5748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5749 return( ret );
5750 }
5751
5752 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5753 {
5754#if defined(MBEDTLS_SSL_SRV_C)
5755 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5756 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5757 {
5758 ret = 0;
5759 }
5760#endif
5761
5762 ssl->state++;
5763 return( ret );
5764 }
5765
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005766#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5767 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005768 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005769
5770crt_verify:
5771 if( ssl->handshake->ecrs_enabled)
5772 rs_ctx = &ssl->handshake->ecrs_ctx;
5773#endif
5774
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005775 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005776 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005777 mbedtls_x509_crt *ca_chain;
5778 mbedtls_x509_crl *ca_crl;
5779
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005780#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005781 if( ssl->handshake->sni_ca_chain != NULL )
5782 {
5783 ca_chain = ssl->handshake->sni_ca_chain;
5784 ca_crl = ssl->handshake->sni_ca_crl;
5785 }
5786 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005787#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005788 {
5789 ca_chain = ssl->conf->ca_chain;
5790 ca_crl = ssl->conf->ca_crl;
5791 }
5792
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005793 /*
5794 * Main check: verify certificate
5795 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005796 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005797 ssl->session_negotiate->peer_cert,
5798 ca_chain, ca_crl,
5799 ssl->conf->cert_profile,
5800 ssl->hostname,
5801 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005802 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005803
5804 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005807 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005808
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005809#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5810 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005811 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005812#endif
5813
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005814 /*
5815 * Secondary checks: always done, but change 'ret' only if it was 0
5816 */
5817
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005818#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005821
5822 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005824 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005825 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005826 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005829 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005831 }
5832 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005833#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005836 ciphersuite_info,
5837 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005838 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005841 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005843 }
5844
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005845 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5846 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5847 * with details encoded in the verification flags. All other kinds
5848 * of error codes, including those from the user provided f_vrfy
5849 * functions, are treated as fatal and lead to a failure of
5850 * ssl_parse_certificate even if verification was optional. */
5851 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5852 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5853 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5854 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005856 }
5857
5858 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5859 {
5860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5861 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5862 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005863
5864 if( ret != 0 )
5865 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005866 uint8_t alert;
5867
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005868 /* The certificate may have been rejected for several reasons.
5869 Pick one and send the corresponding alert. Which alert to send
5870 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005871 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5872 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5873 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5874 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5875 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5876 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5877 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5878 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5879 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5880 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5881 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5882 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5883 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5884 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5885 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5886 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5887 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5888 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5889 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5890 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005891 else
5892 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5894 alert );
5895 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005896
Hanno Beckere6706e62017-05-15 16:05:15 +01005897#if defined(MBEDTLS_DEBUG_C)
5898 if( ssl->session_negotiate->verify_result != 0 )
5899 {
5900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5901 ssl->session_negotiate->verify_result ) );
5902 }
5903 else
5904 {
5905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5906 }
5907#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 }
5909
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005910 ssl->state++;
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913
5914 return( ret );
5915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5917 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5918 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5919 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5920 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5921 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5922 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005925{
5926 int ret;
5927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005931 ssl->out_msglen = 1;
5932 ssl->out_msg[0] = 1;
5933
Paul Bakker5121ce52009-01-03 21:22:43 +00005934 ssl->state++;
5935
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005936 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 return( ret );
5940 }
5941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005943
5944 return( 0 );
5945}
5946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005948{
5949 int ret;
5950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005952
Hanno Becker327c93b2018-08-15 13:56:18 +01005953 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956 return( ret );
5957 }
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005962 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5963 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 }
5966
Hanno Beckere678eaa2018-08-21 14:57:46 +01005967 /* CCS records are only accepted if they have length 1 and content '1',
5968 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005970 /*
5971 * Switch to our negotiated transform and session parameters for inbound
5972 * data.
5973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005975 ssl->transform_in = ssl->transform_negotiate;
5976 ssl->session_in = ssl->session_negotiate;
5977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005982 ssl_dtls_replay_reset( ssl );
5983#endif
5984
5985 /* Increment epoch */
5986 if( ++ssl->in_epoch == 0 )
5987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005989 /* This is highly unlikely to happen for legitimate reasons, so
5990 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005992 }
5993 }
5994 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005996 memset( ssl->in_ctr, 0, 8 );
5997
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005998 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6001 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006006 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6007 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006009 }
6010 }
6011#endif
6012
Paul Bakker5121ce52009-01-03 21:22:43 +00006013 ssl->state++;
6014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006016
6017 return( 0 );
6018}
6019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6021 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006022{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006023 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6026 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6027 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006028 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006029 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6032#if defined(MBEDTLS_SHA512_C)
6033 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006034 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6035 else
6036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037#if defined(MBEDTLS_SHA256_C)
6038 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006039 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006040 else
6041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006045 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006046 }
Paul Bakker380da532012-04-18 16:10:25 +00006047}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6052 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006053 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6054 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6057#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006058 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006061 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006064}
6065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006067 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6070 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006071 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6072 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6075#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006076 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006079 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006082}
6083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6085 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6086static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006087 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006088{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006089 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6090 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006091}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006092#endif
Paul Bakker380da532012-04-18 16:10:25 +00006093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6095#if defined(MBEDTLS_SHA256_C)
6096static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006097 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006098{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006099 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006100}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006101#endif
Paul Bakker380da532012-04-18 16:10:25 +00006102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103#if defined(MBEDTLS_SHA512_C)
6104static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006105 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006106{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006107 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006108}
Paul Bakker769075d2012-11-24 11:26:46 +01006109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006113static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006116 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006117 mbedtls_md5_context md5;
6118 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006119
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 unsigned char padbuf[48];
6121 unsigned char md5sum[16];
6122 unsigned char sha1sum[20];
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006125 if( !session )
6126 session = ssl->session;
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006129
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006130 mbedtls_md5_init( &md5 );
6131 mbedtls_sha1_init( &sha1 );
6132
6133 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6134 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135
6136 /*
6137 * SSLv3:
6138 * hash =
6139 * MD5( master + pad2 +
6140 * MD5( handshake + sender + master + pad1 ) )
6141 * + SHA1( master + pad2 +
6142 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 */
6144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006146 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6147 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006148#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006151 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6152 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006156 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006157
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006160 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6161 mbedtls_md5_update_ret( &md5, session->master, 48 );
6162 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6163 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006164
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006165 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6166 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6167 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6168 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006169
Paul Bakker1ef83d62012-04-11 12:09:53 +00006170 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006172 mbedtls_md5_starts_ret( &md5 );
6173 mbedtls_md5_update_ret( &md5, session->master, 48 );
6174 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6175 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6176 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006177
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006178 mbedtls_sha1_starts_ret( &sha1 );
6179 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6180 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6181 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6182 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006186 mbedtls_md5_free( &md5 );
6187 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006188
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006189 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6190 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6191 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006198static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006200{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006201 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006202 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006203 mbedtls_md5_context md5;
6204 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006205 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006208 if( !session )
6209 session = ssl->session;
6210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006213 mbedtls_md5_init( &md5 );
6214 mbedtls_sha1_init( &sha1 );
6215
6216 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6217 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006218
Paul Bakker1ef83d62012-04-11 12:09:53 +00006219 /*
6220 * TLSv1:
6221 * hash = PRF( master, finished_label,
6222 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6223 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006226 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6227 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006228#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006231 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6232 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006233#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006236 ? "client finished"
6237 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006238
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006239 mbedtls_md5_finish_ret( &md5, padbuf );
6240 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006241
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006242 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006243 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006246
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006247 mbedtls_md5_free( &md5 );
6248 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006249
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006250 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6257#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006258static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006260{
6261 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_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006264 unsigned char padbuf[32];
6265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006267 if( !session )
6268 session = ssl->session;
6269
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006270 mbedtls_sha256_init( &sha256 );
6271
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006273
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006274 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006275
6276 /*
6277 * TLSv1.2:
6278 * hash = PRF( master, finished_label,
6279 * Hash( handshake ) )[0.11]
6280 */
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282#if !defined(MBEDTLS_SHA256_ALT)
6283 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006284 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006285#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006288 ? "client finished"
6289 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006290
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006291 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006292
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006293 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006294 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006297
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006298 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006299
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006300 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006307static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006309{
6310 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006311 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006312 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006313 unsigned char padbuf[48];
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006316 if( !session )
6317 session = ssl->session;
6318
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006319 mbedtls_sha512_init( &sha512 );
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006322
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006323 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006324
6325 /*
6326 * TLSv1.2:
6327 * hash = PRF( master, finished_label,
6328 * Hash( handshake ) )[0.11]
6329 */
6330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006332 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6333 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006334#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006337 ? "client finished"
6338 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006340 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006341
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006342 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006343 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006346
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006347 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006348
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006349 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353#endif /* MBEDTLS_SHA512_C */
6354#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006359
6360 /*
6361 * Free our handshake params
6362 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006363 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006365 ssl->handshake = NULL;
6366
6367 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006368 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006369 */
6370 if( ssl->transform )
6371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 mbedtls_ssl_transform_free( ssl->transform );
6373 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006374 }
6375 ssl->transform = ssl->transform_negotiate;
6376 ssl->transform_negotiate = NULL;
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006379}
6380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006382{
6383 int resume = ssl->handshake->resume;
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#if defined(MBEDTLS_SSL_RENEGOTIATION)
6388 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006391 ssl->renego_records_seen = 0;
6392 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006393#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006394
6395 /*
6396 * Free the previous session and switch in the current one
6397 */
Paul Bakker0a597072012-09-25 21:55:46 +00006398 if( ssl->session )
6399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006401 /* RFC 7366 3.1: keep the EtM state */
6402 ssl->session_negotiate->encrypt_then_mac =
6403 ssl->session->encrypt_then_mac;
6404#endif
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 mbedtls_ssl_session_free( ssl->session );
6407 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006408 }
6409 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006410 ssl->session_negotiate = NULL;
6411
Paul Bakker0a597072012-09-25 21:55:46 +00006412 /*
6413 * Add cache entry
6414 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006415 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006416 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006417 resume == 0 )
6418 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006419 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006421 }
Paul Bakker0a597072012-09-25 21:55:46 +00006422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006425 ssl->handshake->flight != NULL )
6426 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006427 /* Cancel handshake timer */
6428 ssl_set_timer( ssl, 0 );
6429
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006430 /* Keep last flight around in case we need to resend it:
6431 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006433 }
6434 else
6435#endif
6436 ssl_handshake_wrapup_free_hs_transform( ssl );
6437
Paul Bakker48916f92012-09-16 19:57:18 +00006438 ssl->state++;
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006441}
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006444{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006445 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006448
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006449 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006450
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006451 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006452
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006453 /*
6454 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6455 * may define some other value. Currently (early 2016), no defined
6456 * ciphersuite does this (and this is unlikely to change as activity has
6457 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006462 ssl->verify_data_len = hash_len;
6463 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006464#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006465
Paul Bakker5121ce52009-01-03 21:22:43 +00006466 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6468 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
6470 /*
6471 * In case of session resuming, invert the client and server
6472 * ChangeCipherSpec messages order.
6473 */
Paul Bakker0a597072012-09-25 21:55:46 +00006474 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006477 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006483#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006484 }
6485 else
6486 ssl->state++;
6487
Paul Bakker48916f92012-09-16 19:57:18 +00006488 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006489 * Switch to our negotiated transform and session parameters for outbound
6490 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006496 {
6497 unsigned char i;
6498
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006499 /* Remember current epoch settings for resending */
6500 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006501 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006502
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006503 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006504 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006505
6506 /* Increment epoch */
6507 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006508 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006509 break;
6510
6511 /* The loop goes to its end iff the counter is wrapping */
6512 if( i == 0 )
6513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6515 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006516 }
6517 }
6518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006520 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006521
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006522 ssl->transform_out = ssl->transform_negotiate;
6523 ssl->session_out = ssl->session_negotiate;
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6526 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006532 }
6533 }
6534#endif
6535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006539#endif
6540
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006541 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 return( ret );
6545 }
6546
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006547#if defined(MBEDTLS_SSL_PROTO_DTLS)
6548 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6549 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6550 {
6551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6552 return( ret );
6553 }
6554#endif
6555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
6558 return( 0 );
6559}
6560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006562#define SSL_MAX_HASH_LEN 36
6563#else
6564#define SSL_MAX_HASH_LEN 12
6565#endif
6566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006568{
Paul Bakker23986e52011-04-24 08:57:21 +00006569 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006570 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006571 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006575 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006576
Hanno Becker327c93b2018-08-15 13:56:18 +01006577 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 return( ret );
6581 }
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6587 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 }
6590
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006591 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592#if defined(MBEDTLS_SSL_PROTO_SSL3)
6593 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006594 hash_len = 36;
6595 else
6596#endif
6597 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6600 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6604 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 }
6607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006609 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6613 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 }
6616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006618 ssl->verify_data_len = hash_len;
6619 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006620#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006621
Paul Bakker0a597072012-09-25 21:55:46 +00006622 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006625 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006629 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006631#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 }
6633 else
6634 ssl->state++;
6635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006637 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006639#endif
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642
6643 return( 0 );
6644}
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6651 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6652 mbedtls_md5_init( &handshake->fin_md5 );
6653 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006654 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6655 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006656#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6658#if defined(MBEDTLS_SHA256_C)
6659 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006660 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006661#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662#if defined(MBEDTLS_SHA512_C)
6663 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006664 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006667
6668 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006669
6670#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6671 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6672 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6673#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_DHM_C)
6676 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678#if defined(MBEDTLS_ECDH_C)
6679 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006680#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006681#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006682 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006683#if defined(MBEDTLS_SSL_CLI_C)
6684 handshake->ecjpake_cache = NULL;
6685 handshake->ecjpake_cache_len = 0;
6686#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006687#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006688
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006689#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006690 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006691#endif
6692
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006693#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6694 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6695#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006696}
6697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6703 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 mbedtls_md_init( &transform->md_ctx_enc );
6706 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707}
6708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006712}
6713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006715{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006716 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006717 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006719 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006721 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006722 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006723
6724 /*
6725 * Either the pointers are now NULL or cleared properly and can be freed.
6726 * Now allocate missing structures.
6727 */
6728 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006729 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006730 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006731 }
Paul Bakker48916f92012-09-16 19:57:18 +00006732
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006733 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006734 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006735 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006736 }
Paul Bakker48916f92012-09-16 19:57:18 +00006737
Paul Bakker82788fb2014-10-20 13:59:19 +02006738 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006739 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006740 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006741 }
Paul Bakker48916f92012-09-16 19:57:18 +00006742
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006743 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006744 if( ssl->handshake == NULL ||
6745 ssl->transform_negotiate == NULL ||
6746 ssl->session_negotiate == NULL )
6747 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 mbedtls_free( ssl->handshake );
6751 mbedtls_free( ssl->transform_negotiate );
6752 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006753
6754 ssl->handshake = NULL;
6755 ssl->transform_negotiate = NULL;
6756 ssl->session_negotiate = NULL;
6757
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006759 }
6760
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006761 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006763 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006764 ssl_handshake_params_init( ssl->handshake );
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6768 {
6769 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006770
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6772 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6773 else
6774 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006775
6776 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006777 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006778#endif
6779
Paul Bakker48916f92012-09-16 19:57:18 +00006780 return( 0 );
6781}
6782
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006783#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006784/* Dummy cookie callbacks for defaults */
6785static int ssl_cookie_write_dummy( void *ctx,
6786 unsigned char **p, unsigned char *end,
6787 const unsigned char *cli_id, size_t cli_id_len )
6788{
6789 ((void) ctx);
6790 ((void) p);
6791 ((void) end);
6792 ((void) cli_id);
6793 ((void) cli_id_len);
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006796}
6797
6798static int ssl_cookie_check_dummy( void *ctx,
6799 const unsigned char *cookie, size_t cookie_len,
6800 const unsigned char *cli_id, size_t cli_id_len )
6801{
6802 ((void) ctx);
6803 ((void) cookie);
6804 ((void) cookie_len);
6805 ((void) cli_id);
6806 ((void) cli_id_len);
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006809}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006810#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006811
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006812/* Once ssl->out_hdr as the address of the beginning of the
6813 * next outgoing record is set, deduce the other pointers.
6814 *
6815 * Note: For TLS, we save the implicit record sequence number
6816 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6817 * and the caller has to make sure there's space for this.
6818 */
6819
6820static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6821 mbedtls_ssl_transform *transform )
6822{
6823#if defined(MBEDTLS_SSL_PROTO_DTLS)
6824 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6825 {
6826 ssl->out_ctr = ssl->out_hdr + 3;
6827 ssl->out_len = ssl->out_hdr + 11;
6828 ssl->out_iv = ssl->out_hdr + 13;
6829 }
6830 else
6831#endif
6832 {
6833 ssl->out_ctr = ssl->out_hdr - 8;
6834 ssl->out_len = ssl->out_hdr + 3;
6835 ssl->out_iv = ssl->out_hdr + 5;
6836 }
6837
6838 /* Adjust out_msg to make space for explicit IV, if used. */
6839 if( transform != NULL &&
6840 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6841 {
6842 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6843 }
6844 else
6845 ssl->out_msg = ssl->out_iv;
6846}
6847
6848/* Once ssl->in_hdr as the address of the beginning of the
6849 * next incoming record is set, deduce the other pointers.
6850 *
6851 * Note: For TLS, we save the implicit record sequence number
6852 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6853 * and the caller has to make sure there's space for this.
6854 */
6855
6856static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6857 mbedtls_ssl_transform *transform )
6858{
6859#if defined(MBEDTLS_SSL_PROTO_DTLS)
6860 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6861 {
6862 ssl->in_ctr = ssl->in_hdr + 3;
6863 ssl->in_len = ssl->in_hdr + 11;
6864 ssl->in_iv = ssl->in_hdr + 13;
6865 }
6866 else
6867#endif
6868 {
6869 ssl->in_ctr = ssl->in_hdr - 8;
6870 ssl->in_len = ssl->in_hdr + 3;
6871 ssl->in_iv = ssl->in_hdr + 5;
6872 }
6873
6874 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6875 if( transform != NULL &&
6876 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6877 {
6878 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6879 }
6880 else
6881 ssl->in_msg = ssl->in_iv;
6882}
6883
Paul Bakker5121ce52009-01-03 21:22:43 +00006884/*
6885 * Initialize an SSL context
6886 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006887void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6888{
6889 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6890}
6891
6892/*
6893 * Setup an SSL context
6894 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006895
6896static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6897{
6898 /* Set the incoming and outgoing record pointers. */
6899#if defined(MBEDTLS_SSL_PROTO_DTLS)
6900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6901 {
6902 ssl->out_hdr = ssl->out_buf;
6903 ssl->in_hdr = ssl->in_buf;
6904 }
6905 else
6906#endif /* MBEDTLS_SSL_PROTO_DTLS */
6907 {
6908 ssl->out_hdr = ssl->out_buf + 8;
6909 ssl->in_hdr = ssl->in_buf + 8;
6910 }
6911
6912 /* Derive other internal pointers. */
6913 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6914 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6915}
6916
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006917int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006918 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006919{
Paul Bakker48916f92012-09-16 19:57:18 +00006920 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006921
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006922 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006923
6924 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006925 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006926 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006927
6928 /* Set to NULL in case of an error condition */
6929 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006930
Angus Grattond8213d02016-05-25 20:56:48 +10006931 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6932 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006933 {
Angus Grattond8213d02016-05-25 20:56:48 +10006934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006935 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006936 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006937 }
6938
6939 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6940 if( ssl->out_buf == NULL )
6941 {
6942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006943 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006944 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006945 }
6946
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006947 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006948
Paul Bakker48916f92012-09-16 19:57:18 +00006949 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006950 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006951
6952 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006953
6954error:
6955 mbedtls_free( ssl->in_buf );
6956 mbedtls_free( ssl->out_buf );
6957
6958 ssl->conf = NULL;
6959
6960 ssl->in_buf = NULL;
6961 ssl->out_buf = NULL;
6962
6963 ssl->in_hdr = NULL;
6964 ssl->in_ctr = NULL;
6965 ssl->in_len = NULL;
6966 ssl->in_iv = NULL;
6967 ssl->in_msg = NULL;
6968
6969 ssl->out_hdr = NULL;
6970 ssl->out_ctr = NULL;
6971 ssl->out_len = NULL;
6972 ssl->out_iv = NULL;
6973 ssl->out_msg = NULL;
6974
k-stachowiak9f7798e2018-07-31 16:52:32 +02006975 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976}
6977
6978/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006979 * Reset an initialized and used SSL context for re-use while retaining
6980 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006981 *
6982 * If partial is non-zero, keep data in the input buffer and client ID.
6983 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006984 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006985static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006986{
Paul Bakker48916f92012-09-16 19:57:18 +00006987 int ret;
6988
Hanno Becker7e772132018-08-10 12:38:21 +01006989#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6990 !defined(MBEDTLS_SSL_SRV_C)
6991 ((void) partial);
6992#endif
6993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006995
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006996 /* Cancel any possibly running timer */
6997 ssl_set_timer( ssl, 0 );
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999#if defined(MBEDTLS_SSL_RENEGOTIATION)
7000 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007001 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007002
7003 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7005 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007008
Paul Bakker7eb013f2011-10-06 12:37:39 +00007009 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007010 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007011
7012 ssl->in_msgtype = 0;
7013 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007015 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007016 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007017#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007019 ssl_dtls_replay_reset( ssl );
7020#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007021
7022 ssl->in_hslen = 0;
7023 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007024
7025 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007026
7027 ssl->out_msgtype = 0;
7028 ssl->out_msglen = 0;
7029 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7031 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007032 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007033#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007034
Hanno Becker19859472018-08-06 09:40:20 +01007035 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7036
Paul Bakker48916f92012-09-16 19:57:18 +00007037 ssl->transform_in = NULL;
7038 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007039
Hanno Becker78640902018-08-13 16:35:15 +01007040 ssl->session_in = NULL;
7041 ssl->session_out = NULL;
7042
Angus Grattond8213d02016-05-25 20:56:48 +10007043 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007044
7045#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007046 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007047#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7048 {
7049 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007050 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007051 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7054 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7057 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7060 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007061 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007062 }
7063#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007064
Paul Bakker48916f92012-09-16 19:57:18 +00007065 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 mbedtls_ssl_transform_free( ssl->transform );
7068 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007069 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007070 }
Paul Bakker48916f92012-09-16 19:57:18 +00007071
Paul Bakkerc0463502013-02-14 11:19:38 +01007072 if( ssl->session )
7073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 mbedtls_ssl_session_free( ssl->session );
7075 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007076 ssl->session = NULL;
7077 }
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007080 ssl->alpn_chosen = NULL;
7081#endif
7082
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007083#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007084#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007085 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007086#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007087 {
7088 mbedtls_free( ssl->cli_id );
7089 ssl->cli_id = NULL;
7090 ssl->cli_id_len = 0;
7091 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007092#endif
7093
Paul Bakker48916f92012-09-16 19:57:18 +00007094 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7095 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007096
7097 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007098}
7099
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007100/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007101 * Reset an initialized and used SSL context for re-use while retaining
7102 * all application-set variables, function pointers and data.
7103 */
7104int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7105{
7106 return( ssl_session_reset_int( ssl, 0 ) );
7107}
7108
7109/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 * SSL set accessors
7111 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007112void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007114 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007115}
7116
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007117void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007118{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007119 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007120}
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007123void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007125 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007126}
7127#endif
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007130void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007132 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007133}
7134#endif
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007137
Hanno Becker1841b0a2018-08-24 11:13:57 +01007138void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7139 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007140{
7141 ssl->disable_datagram_packing = !allow_packing;
7142}
7143
7144void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7145 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007147 conf->hs_timeout_min = min;
7148 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007149}
7150#endif
7151
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007152void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007154 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007155}
7156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007158void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007159 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007160 void *p_vrfy )
7161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007162 conf->f_vrfy = f_vrfy;
7163 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007167void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007168 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007169 void *p_rng )
7170{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007171 conf->f_rng = f_rng;
7172 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007173}
7174
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007175void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007176 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 void *p_dbg )
7178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007179 conf->f_dbg = f_dbg;
7180 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007181}
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007184 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007185 mbedtls_ssl_send_t *f_send,
7186 mbedtls_ssl_recv_t *f_recv,
7187 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007188{
7189 ssl->p_bio = p_bio;
7190 ssl->f_send = f_send;
7191 ssl->f_recv = f_recv;
7192 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007193}
7194
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007195#if defined(MBEDTLS_SSL_PROTO_DTLS)
7196void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7197{
7198 ssl->mtu = mtu;
7199}
7200#endif
7201
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007202void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007203{
7204 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007205}
7206
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007207void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7208 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007209 mbedtls_ssl_set_timer_t *f_set_timer,
7210 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007211{
7212 ssl->p_timer = p_timer;
7213 ssl->f_set_timer = f_set_timer;
7214 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007215
7216 /* Make sure we start with no timer running */
7217 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007218}
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007221void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007222 void *p_cache,
7223 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7224 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007225{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007226 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007227 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007228 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if defined(MBEDTLS_SSL_CLI_C)
7233int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007234{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007235 int ret;
7236
7237 if( ssl == NULL ||
7238 session == NULL ||
7239 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007240 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007243 }
7244
7245 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7246 return( ret );
7247
Paul Bakker0a597072012-09-25 21:55:46 +00007248 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007249
7250 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007253
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007254void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007255 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7258 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7259 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7260 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007261}
7262
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007263void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007264 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007265 int major, int minor )
7266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007268 return;
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007271 return;
7272
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007273 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007274}
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007277void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007278 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007279{
7280 conf->cert_profile = profile;
7281}
7282
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007283/* Append a new keycert entry to a (possibly empty) list */
7284static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7285 mbedtls_x509_crt *cert,
7286 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007287{
niisato8ee24222018-06-25 19:05:48 +09007288 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007289
niisato8ee24222018-06-25 19:05:48 +09007290 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7291 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007292 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007293
niisato8ee24222018-06-25 19:05:48 +09007294 new_cert->cert = cert;
7295 new_cert->key = key;
7296 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007297
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007298 /* Update head is the list was null, else add to the end */
7299 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007300 {
niisato8ee24222018-06-25 19:05:48 +09007301 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007302 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007303 else
7304 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007305 mbedtls_ssl_key_cert *cur = *head;
7306 while( cur->next != NULL )
7307 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007308 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007309 }
7310
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007311 return( 0 );
7312}
7313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007314int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007315 mbedtls_x509_crt *own_cert,
7316 mbedtls_pk_context *pk_key )
7317{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007318 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007319}
7320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007321void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007322 mbedtls_x509_crt *ca_chain,
7323 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007324{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007325 conf->ca_chain = ca_chain;
7326 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007329
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007330#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7331int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7332 mbedtls_x509_crt *own_cert,
7333 mbedtls_pk_context *pk_key )
7334{
7335 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7336 own_cert, pk_key ) );
7337}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007338
7339void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7340 mbedtls_x509_crt *ca_chain,
7341 mbedtls_x509_crl *ca_crl )
7342{
7343 ssl->handshake->sni_ca_chain = ca_chain;
7344 ssl->handshake->sni_ca_crl = ca_crl;
7345}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007346
7347void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7348 int authmode )
7349{
7350 ssl->handshake->sni_authmode = authmode;
7351}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007352#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7353
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007354#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007355/*
7356 * Set EC J-PAKE password for current handshake
7357 */
7358int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7359 const unsigned char *pw,
7360 size_t pw_len )
7361{
7362 mbedtls_ecjpake_role role;
7363
Janos Follath8eb64132016-06-03 15:40:57 +01007364 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7366
7367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7368 role = MBEDTLS_ECJPAKE_SERVER;
7369 else
7370 role = MBEDTLS_ECJPAKE_CLIENT;
7371
7372 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7373 role,
7374 MBEDTLS_MD_SHA256,
7375 MBEDTLS_ECP_DP_SECP256R1,
7376 pw, pw_len ) );
7377}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007378#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007381int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007382 const unsigned char *psk, size_t psk_len,
7383 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007384{
Paul Bakker6db455e2013-09-18 17:29:31 +02007385 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007390
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007391 /* Identity len will be encoded on two bytes */
7392 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007393 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007394 {
7395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7396 }
7397
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007398 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007399 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007400 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007401
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007402 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007403 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007404 conf->psk_len = 0;
7405 }
7406 if( conf->psk_identity != NULL )
7407 {
7408 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007409 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007410 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007411 }
7412
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007413 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7414 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007415 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007416 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007417 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007418 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007419 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007421 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007422
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007423 conf->psk_len = psk_len;
7424 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007425
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007426 memcpy( conf->psk, psk, conf->psk_len );
7427 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007428
7429 return( 0 );
7430}
7431
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007432int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7433 const unsigned char *psk, size_t psk_len )
7434{
7435 if( psk == NULL || ssl->handshake == NULL )
7436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7437
7438 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7440
7441 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007442 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007443 mbedtls_platform_zeroize( ssl->handshake->psk,
7444 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007445 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007446 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007447 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007449 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007450 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007451
7452 ssl->handshake->psk_len = psk_len;
7453 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7454
7455 return( 0 );
7456}
7457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007458void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007460 size_t),
7461 void *p_psk )
7462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007463 conf->f_psk = f_psk;
7464 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007465}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007467
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007468#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007469
7470#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007471int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007472{
7473 int ret;
7474
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007475 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7476 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7477 {
7478 mbedtls_mpi_free( &conf->dhm_P );
7479 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007482
7483 return( 0 );
7484}
Hanno Becker470a8c42017-10-04 15:28:46 +01007485#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007486
Hanno Beckera90658f2017-10-04 15:29:08 +01007487int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7488 const unsigned char *dhm_P, size_t P_len,
7489 const unsigned char *dhm_G, size_t G_len )
7490{
7491 int ret;
7492
7493 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7494 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7495 {
7496 mbedtls_mpi_free( &conf->dhm_P );
7497 mbedtls_mpi_free( &conf->dhm_G );
7498 return( ret );
7499 }
7500
7501 return( 0 );
7502}
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007504int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007505{
7506 int ret;
7507
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007508 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7509 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7510 {
7511 mbedtls_mpi_free( &conf->dhm_P );
7512 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007513 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007514 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007515
7516 return( 0 );
7517}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007518#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007519
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007520#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7521/*
7522 * Set the minimum length for Diffie-Hellman parameters
7523 */
7524void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7525 unsigned int bitlen )
7526{
7527 conf->dhm_min_bitlen = bitlen;
7528}
7529#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7530
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007531#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007532/*
7533 * Set allowed/preferred hashes for handshake signatures
7534 */
7535void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7536 const int *hashes )
7537{
7538 conf->sig_hashes = hashes;
7539}
Hanno Becker947194e2017-04-07 13:25:49 +01007540#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007541
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007542#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007543/*
7544 * Set the allowed elliptic curves
7545 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007546void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007547 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007550}
Hanno Becker947194e2017-04-07 13:25:49 +01007551#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007552
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007553#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007555{
Hanno Becker947194e2017-04-07 13:25:49 +01007556 /* Initialize to suppress unnecessary compiler warning */
7557 size_t hostname_len = 0;
7558
7559 /* Check if new hostname is valid before
7560 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007561 if( hostname != NULL )
7562 {
7563 hostname_len = strlen( hostname );
7564
7565 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7567 }
7568
7569 /* Now it's clear that we will overwrite the old hostname,
7570 * so we can free it safely */
7571
7572 if( ssl->hostname != NULL )
7573 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007574 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007575 mbedtls_free( ssl->hostname );
7576 }
7577
7578 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007579
Paul Bakker5121ce52009-01-03 21:22:43 +00007580 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007581 {
7582 ssl->hostname = NULL;
7583 }
7584 else
7585 {
7586 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007587 if( ssl->hostname == NULL )
7588 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007589
Hanno Becker947194e2017-04-07 13:25:49 +01007590 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007591
Hanno Becker947194e2017-04-07 13:25:49 +01007592 ssl->hostname[hostname_len] = '\0';
7593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007594
7595 return( 0 );
7596}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007597#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007598
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007599#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007600void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007602 const unsigned char *, size_t),
7603 void *p_sni )
7604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007605 conf->f_sni = f_sni;
7606 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007611int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007612{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007613 size_t cur_len, tot_len;
7614 const char **p;
7615
7616 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007617 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7618 * MUST NOT be truncated."
7619 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007620 */
7621 tot_len = 0;
7622 for( p = protos; *p != NULL; p++ )
7623 {
7624 cur_len = strlen( *p );
7625 tot_len += cur_len;
7626
Ronald Cron157cffe2020-04-23 16:41:44 +02007627 if( ( cur_len == 0 ) ||
7628 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7629 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007631 }
7632
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007633 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007634
7635 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007636}
7637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007639{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007640 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007643
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007644void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007646 conf->max_major_ver = major;
7647 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007648}
7649
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007650void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007651{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007652 conf->min_major_ver = major;
7653 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007654}
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007657void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007658{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007659 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007660}
7661#endif
7662
Janos Follath088ce432017-04-10 12:42:31 +01007663#if defined(MBEDTLS_SSL_SRV_C)
7664void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7665 char cert_req_ca_list )
7666{
7667 conf->cert_req_ca_list = cert_req_ca_list;
7668}
7669#endif
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007672void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007673{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007674 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007675}
7676#endif
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007679void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007681 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007682}
7683#endif
7684
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007685#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007686void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007688 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007689}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007690#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007693int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007696 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007699 }
7700
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007701 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007702
7703 return( 0 );
7704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007708void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007710 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007715void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007716{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007717 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007718}
7719#endif
7720
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007721void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007722{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007723 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007724}
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007727void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007729 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007730}
7731
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007732void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007734 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007735}
7736
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007737void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007738 const unsigned char period[8] )
7739{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007740 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007745#if defined(MBEDTLS_SSL_CLI_C)
7746void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007747{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007748 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007749}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007750#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007751
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007752#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007753void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7754 mbedtls_ssl_ticket_write_t *f_ticket_write,
7755 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7756 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007757{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007758 conf->f_ticket_write = f_ticket_write;
7759 conf->f_ticket_parse = f_ticket_parse;
7760 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007761}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007764
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007765#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7766void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7767 mbedtls_ssl_export_keys_t *f_export_keys,
7768 void *p_export_keys )
7769{
7770 conf->f_export_keys = f_export_keys;
7771 conf->p_export_keys = p_export_keys;
7772}
7773#endif
7774
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007775#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007776void mbedtls_ssl_conf_async_private_cb(
7777 mbedtls_ssl_config *conf,
7778 mbedtls_ssl_async_sign_t *f_async_sign,
7779 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7780 mbedtls_ssl_async_resume_t *f_async_resume,
7781 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007782 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007783{
7784 conf->f_async_sign_start = f_async_sign;
7785 conf->f_async_decrypt_start = f_async_decrypt;
7786 conf->f_async_resume = f_async_resume;
7787 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007788 conf->p_async_config_data = async_config_data;
7789}
7790
Gilles Peskine8f97af72018-04-26 11:46:10 +02007791void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7792{
7793 return( conf->p_async_config_data );
7794}
7795
Gilles Peskine1febfef2018-04-30 11:54:39 +02007796void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007797{
7798 if( ssl->handshake == NULL )
7799 return( NULL );
7800 else
7801 return( ssl->handshake->user_async_ctx );
7802}
7803
Gilles Peskine1febfef2018-04-30 11:54:39 +02007804void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007805 void *ctx )
7806{
7807 if( ssl->handshake != NULL )
7808 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007809}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007810#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007811
Paul Bakker5121ce52009-01-03 21:22:43 +00007812/*
7813 * SSL get accessors
7814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007816{
7817 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7818}
7819
Hanno Becker8b170a02017-10-10 11:51:19 +01007820int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7821{
7822 /*
7823 * Case A: We're currently holding back
7824 * a message for further processing.
7825 */
7826
7827 if( ssl->keep_current_message == 1 )
7828 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007830 return( 1 );
7831 }
7832
7833 /*
7834 * Case B: Further records are pending in the current datagram.
7835 */
7836
7837#if defined(MBEDTLS_SSL_PROTO_DTLS)
7838 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7839 ssl->in_left > ssl->next_record_offset )
7840 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007842 return( 1 );
7843 }
7844#endif /* MBEDTLS_SSL_PROTO_DTLS */
7845
7846 /*
7847 * Case C: A handshake message is being processed.
7848 */
7849
Hanno Becker8b170a02017-10-10 11:51:19 +01007850 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7851 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007853 return( 1 );
7854 }
7855
7856 /*
7857 * Case D: An application data message is being processed
7858 */
7859 if( ssl->in_offt != NULL )
7860 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007862 return( 1 );
7863 }
7864
7865 /*
7866 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007867 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007868 * we implement support for multiple alerts in single records.
7869 */
7870
7871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7872 return( 0 );
7873}
7874
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007875uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007877 if( ssl->session != NULL )
7878 return( ssl->session->verify_result );
7879
7880 if( ssl->session_negotiate != NULL )
7881 return( ssl->session_negotiate->verify_result );
7882
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007883 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007884}
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007887{
Paul Bakker926c8e42013-03-06 10:23:34 +01007888 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007889 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007892}
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007898 {
7899 switch( ssl->minor_ver )
7900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007902 return( "DTLSv1.0" );
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007905 return( "DTLSv1.2" );
7906
7907 default:
7908 return( "unknown (DTLS)" );
7909 }
7910 }
7911#endif
7912
Paul Bakker43ca69c2011-01-15 17:35:19 +00007913 switch( ssl->minor_ver )
7914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007916 return( "SSLv3.0" );
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007919 return( "TLSv1.0" );
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007922 return( "TLSv1.1" );
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007925 return( "TLSv1.2" );
7926
Paul Bakker43ca69c2011-01-15 17:35:19 +00007927 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007928 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007929 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007930}
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007933{
Hanno Becker3136ede2018-08-17 15:28:19 +01007934 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007936 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007937
Hanno Becker78640902018-08-13 16:35:15 +01007938 if( transform == NULL )
7939 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_ZLIB_SUPPORT)
7942 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007944#endif
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 case MBEDTLS_MODE_GCM:
7949 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007950 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007952 transform_expansion = transform->minlen;
7953 break;
7954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007956
7957 block_size = mbedtls_cipher_get_block_size(
7958 &transform->cipher_ctx_enc );
7959
Hanno Becker3136ede2018-08-17 15:28:19 +01007960 /* Expansion due to the addition of the MAC. */
7961 transform_expansion += transform->maclen;
7962
7963 /* Expansion due to the addition of CBC padding;
7964 * Theoretically up to 256 bytes, but we never use
7965 * more than the block size of the underlying cipher. */
7966 transform_expansion += block_size;
7967
7968 /* For TLS 1.1 or higher, an explicit IV is added
7969 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007970#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7971 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007972 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007973#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007974
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007975 break;
7976
7977 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007980 }
7981
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007982 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007983}
7984
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007985#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7986size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7987{
7988 size_t max_len;
7989
7990 /*
7991 * Assume mfl_code is correct since it was checked when set
7992 */
Angus Grattond8213d02016-05-25 20:56:48 +10007993 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007994
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007995 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007996 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007997 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007998 {
Angus Grattond8213d02016-05-25 20:56:48 +10007999 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008000 }
8001
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008002 /* During a handshake, use the value being negotiated */
8003 if( ssl->session_negotiate != NULL &&
8004 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8005 {
8006 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8007 }
8008
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008009 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008010}
8011#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8012
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008013#if defined(MBEDTLS_SSL_PROTO_DTLS)
8014static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8015{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008016 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8017 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8018 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8019 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8020 return ( 0 );
8021
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008022 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8023 return( ssl->mtu );
8024
8025 if( ssl->mtu == 0 )
8026 return( ssl->handshake->mtu );
8027
8028 return( ssl->mtu < ssl->handshake->mtu ?
8029 ssl->mtu : ssl->handshake->mtu );
8030}
8031#endif /* MBEDTLS_SSL_PROTO_DTLS */
8032
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008033int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8034{
8035 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8036
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008037#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8038 !defined(MBEDTLS_SSL_PROTO_DTLS)
8039 (void) ssl;
8040#endif
8041
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008042#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8043 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8044
8045 if( max_len > mfl )
8046 max_len = mfl;
8047#endif
8048
8049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008050 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008051 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008052 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008053 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8054 const size_t overhead = (size_t) ret;
8055
8056 if( ret < 0 )
8057 return( ret );
8058
8059 if( mtu <= overhead )
8060 {
8061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8062 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8063 }
8064
8065 if( max_len > mtu - overhead )
8066 max_len = mtu - overhead;
8067 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008068#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008069
Hanno Becker0defedb2018-08-10 12:35:02 +01008070#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8071 !defined(MBEDTLS_SSL_PROTO_DTLS)
8072 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008073#endif
8074
8075 return( (int) max_len );
8076}
8077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078#if defined(MBEDTLS_X509_CRT_PARSE_C)
8079const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008080{
8081 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008082 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008083
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008084 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088#if defined(MBEDTLS_SSL_CLI_C)
8089int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008090{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008091 if( ssl == NULL ||
8092 dst == NULL ||
8093 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008094 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008097 }
8098
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008099 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008102
Paul Bakker5121ce52009-01-03 21:22:43 +00008103/*
Paul Bakker1961b702013-01-25 14:49:24 +01008104 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008109
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008110 if( ssl == NULL || ssl->conf == NULL )
8111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008120#endif
8121
Paul Bakker1961b702013-01-25 14:49:24 +01008122 return( ret );
8123}
8124
8125/*
8126 * Perform the SSL handshake
8127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008129{
8130 int ret = 0;
8131
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008132 if( ssl == NULL || ssl->conf == NULL )
8133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008140
8141 if( ret != 0 )
8142 break;
8143 }
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008146
8147 return( ret );
8148}
8149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_RENEGOTIATION)
8151#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008152/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008153 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008156{
8157 int ret;
8158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008160
8161 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8163 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008164
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008165 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008166 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008168 return( ret );
8169 }
8170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008172
8173 return( 0 );
8174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008176
8177/*
8178 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 * - any side: calling mbedtls_ssl_renegotiate(),
8180 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8181 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008182 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008183 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008187{
8188 int ret;
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008191
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008192 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8193 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008194
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008195 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8196 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008200 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008201 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008202 ssl->handshake->out_msg_seq = 1;
8203 else
8204 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008205 }
8206#endif
8207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8209 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008214 return( ret );
8215 }
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008218
8219 return( 0 );
8220}
8221
8222/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008223 * Renegotiate current connection on client,
8224 * or request renegotiation on server
8225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008229
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008230 if( ssl == NULL || ssl->conf == NULL )
8231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008234 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008241
8242 /* Did we already try/start sending HelloRequest? */
8243 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008245
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008246 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008251 /*
8252 * On client, either start the renegotiation process or,
8253 * if already in progress, continue the handshake
8254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008259
8260 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008263 return( ret );
8264 }
8265 }
8266 else
8267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008271 return( ret );
8272 }
8273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008275
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008276 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008277}
8278
8279/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008280 * Check record counters and renegotiate if they're above the limit.
8281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008283{
Andres AG2196c7f2016-12-15 17:01:16 +00008284 size_t ep_len = ssl_ep_len( ssl );
8285 int in_ctr_cmp;
8286 int out_ctr_cmp;
8287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8289 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008290 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008291 {
8292 return( 0 );
8293 }
8294
Andres AG2196c7f2016-12-15 17:01:16 +00008295 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8296 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008297 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008298 ssl->conf->renego_period + ep_len, 8 - ep_len );
8299
8300 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008301 {
8302 return( 0 );
8303 }
8304
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008309
8310/*
8311 * Receive application data decrypted from the SSL layer
8312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008314{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008315 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008316 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008317
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008318 if( ssl == NULL || ssl->conf == NULL )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008327 return( ret );
8328
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008329 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008331 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008332 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008333 return( ret );
8334 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008335 }
8336#endif
8337
Hanno Becker4a810fb2017-05-24 16:27:30 +01008338 /*
8339 * Check if renegotiation is necessary and/or handshake is
8340 * in process. If yes, perform/continue, and fall through
8341 * if an unexpected packet is received while the client
8342 * is waiting for the ServerHello.
8343 *
8344 * (There is no equivalent to the last condition on
8345 * the server-side as it is not treated as within
8346 * a handshake while waiting for the ClientHello
8347 * after a renegotiation request.)
8348 */
8349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008351 ret = ssl_check_ctr_renegotiate( ssl );
8352 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8353 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008356 return( ret );
8357 }
8358#endif
8359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008363 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8364 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008367 return( ret );
8368 }
8369 }
8370
Hanno Beckere41158b2017-10-23 13:30:32 +01008371 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008372 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008373 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008374 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008375 if( ssl->f_get_timer != NULL &&
8376 ssl->f_get_timer( ssl->p_timer ) == -1 )
8377 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008378 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008379 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008380
Hanno Becker327c93b2018-08-15 13:56:18 +01008381 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008382 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008383 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8384 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008385
Hanno Becker4a810fb2017-05-24 16:27:30 +01008386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8387 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008388 }
8389
8390 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008392 {
8393 /*
8394 * OpenSSL sends empty messages to randomize the IV
8395 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008396 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008399 return( 0 );
8400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008402 return( ret );
8403 }
8404 }
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008409
Hanno Becker4a810fb2017-05-24 16:27:30 +01008410 /*
8411 * - For client-side, expect SERVER_HELLO_REQUEST.
8412 * - For server-side, expect CLIENT_HELLO.
8413 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8414 */
8415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008419 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008422
8423 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008426 {
8427 continue;
8428 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008431 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008432#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008433
Hanno Becker4a810fb2017-05-24 16:27:30 +01008434#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008439
8440 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008443 {
8444 continue;
8445 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008448 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008449#endif /* MBEDTLS_SSL_SRV_C */
8450
Hanno Becker21df7f92017-10-17 11:03:26 +01008451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008452 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008453 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8454 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8455 ssl->conf->allow_legacy_renegotiation ==
8456 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8457 {
8458 /*
8459 * Accept renegotiation request
8460 */
Paul Bakker48916f92012-09-16 19:57:18 +00008461
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008462 /* DTLS clients need to know renego is server-initiated */
8463#if defined(MBEDTLS_SSL_PROTO_DTLS)
8464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8465 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8466 {
8467 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8468 }
8469#endif
8470 ret = ssl_start_renegotiation( ssl );
8471 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8472 ret != 0 )
8473 {
8474 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8475 return( ret );
8476 }
8477 }
8478 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008479#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008480 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008481 /*
8482 * Refuse renegotiation
8483 */
8484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#if defined(MBEDTLS_SSL_PROTO_SSL3)
8488 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008489 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008490 /* SSLv3 does not have a "no_renegotiation" warning, so
8491 we send a fatal alert and abort the connection. */
8492 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8493 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8494 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008495 }
8496 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8498#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8499 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8500 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8503 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8504 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008505 {
8506 return( ret );
8507 }
Paul Bakker48916f92012-09-16 19:57:18 +00008508 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8511 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008515 }
Paul Bakker48916f92012-09-16 19:57:18 +00008516 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008517
Hanno Becker90333da2017-10-10 11:27:13 +01008518 /* At this point, we don't know whether the renegotiation has been
8519 * completed or not. The cases to consider are the following:
8520 * 1) The renegotiation is complete. In this case, no new record
8521 * has been read yet.
8522 * 2) The renegotiation is incomplete because the client received
8523 * an application data record while awaiting the ServerHello.
8524 * 3) The renegotiation is incomplete because the client received
8525 * a non-handshake, non-application data message while awaiting
8526 * the ServerHello.
8527 * In each of these case, looping will be the proper action:
8528 * - For 1), the next iteration will read a new record and check
8529 * if it's application data.
8530 * - For 2), the loop condition isn't satisfied as application data
8531 * is present, hence continue is the same as break
8532 * - For 3), the loop condition is satisfied and read_record
8533 * will re-deliver the message that was held back by the client
8534 * when expecting the ServerHello.
8535 */
8536 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008537 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008538#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008540 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008541 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008542 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008543 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008546 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008548 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008549 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8554 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008557 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008558 }
8559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008564 }
8565
8566 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008567
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008568 /* We're going to return something now, cancel timer,
8569 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008571 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008572
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008574 /* If we requested renego but received AppData, resend HelloRequest.
8575 * Do it now, after setting in_offt, to avoid taking this branch
8576 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008580 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008581 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008584 return( ret );
8585 }
8586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008588#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008589 }
8590
8591 n = ( len < ssl->in_msglen )
8592 ? len : ssl->in_msglen;
8593
8594 memcpy( buf, ssl->in_offt, n );
8595 ssl->in_msglen -= n;
8596
8597 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008598 {
8599 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008600 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008601 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008603 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008604 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008605 /* more data available */
8606 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008610
Paul Bakker23986e52011-04-24 08:57:21 +00008611 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008612}
8613
8614/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008615 * Send application data to be encrypted by the SSL layer, taking care of max
8616 * fragment length and buffer size.
8617 *
8618 * According to RFC 5246 Section 6.2.1:
8619 *
8620 * Zero-length fragments of Application data MAY be sent as they are
8621 * potentially useful as a traffic analysis countermeasure.
8622 *
8623 * Therefore, it is possible that the input message length is 0 and the
8624 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008625 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008626static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008627 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008628{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008629 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8630 const size_t max_len = (size_t) ret;
8631
8632 if( ret < 0 )
8633 {
8634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8635 return( ret );
8636 }
8637
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008638 if( len > max_len )
8639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008641 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008644 "maximum fragment length: %d > %d",
8645 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008647 }
8648 else
8649#endif
8650 len = max_len;
8651 }
Paul Bakker887bd502011-06-08 13:10:54 +00008652
Paul Bakker5121ce52009-01-03 21:22:43 +00008653 if( ssl->out_left != 0 )
8654 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008655 /*
8656 * The user has previously tried to send the data and
8657 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8658 * written. In this case, we expect the high-level write function
8659 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008664 return( ret );
8665 }
8666 }
Paul Bakker887bd502011-06-08 13:10:54 +00008667 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008668 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008669 /*
8670 * The user is trying to send a message the first time, so we need to
8671 * copy the data into the internal buffers and setup the data structure
8672 * to keep track of partial writes
8673 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008674 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008676 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008677
Hanno Becker67bc7c32018-08-06 11:33:50 +01008678 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008681 return( ret );
8682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008683 }
8684
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008685 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008686}
8687
8688/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008689 * Write application data, doing 1/n-1 splitting if necessary.
8690 *
8691 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008692 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008693 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008696static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008697 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008698{
8699 int ret;
8700
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008701 if( ssl->conf->cbc_record_splitting ==
8702 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008703 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8705 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8706 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008707 {
8708 return( ssl_write_real( ssl, buf, len ) );
8709 }
8710
8711 if( ssl->split_done == 0 )
8712 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008713 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008714 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008715 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008716 }
8717
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008718 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8719 return( ret );
8720 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008721
8722 return( ret + 1 );
8723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008725
8726/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008727 * Write application data (public-facing wrapper)
8728 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008729int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008730{
8731 int ret;
8732
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008734
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008735 if( ssl == NULL || ssl->conf == NULL )
8736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8737
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008738#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008739 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8740 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008741 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008742 return( ret );
8743 }
8744#endif
8745
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008746 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008747 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008748 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008749 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008751 return( ret );
8752 }
8753 }
8754
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008755#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008756 ret = ssl_write_split( ssl, buf, len );
8757#else
8758 ret = ssl_write_real( ssl, buf, len );
8759#endif
8760
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008762
8763 return( ret );
8764}
8765
8766/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008767 * Notify the peer that the connection is being closed
8768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008770{
8771 int ret;
8772
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008773 if( ssl == NULL || ssl->conf == NULL )
8774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008777
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008778 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8784 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8785 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008788 return( ret );
8789 }
8790 }
8791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008793
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008794 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008795}
8796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008798{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008799 if( transform == NULL )
8800 return;
8801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008803 deflateEnd( &transform->ctx_deflate );
8804 inflateEnd( &transform->ctx_inflate );
8805#endif
8806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8808 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 mbedtls_md_free( &transform->md_ctx_enc );
8811 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008813 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008814}
8815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816#if defined(MBEDTLS_X509_CRT_PARSE_C)
8817static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008820
8821 while( cur != NULL )
8822 {
8823 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008825 cur = next;
8826 }
8827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008829
Hanno Becker0271f962018-08-16 13:23:47 +01008830#if defined(MBEDTLS_SSL_PROTO_DTLS)
8831
8832static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8833{
8834 unsigned offset;
8835 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8836
8837 if( hs == NULL )
8838 return;
8839
Hanno Becker283f5ef2018-08-24 09:34:47 +01008840 ssl_free_buffered_record( ssl );
8841
Hanno Becker0271f962018-08-16 13:23:47 +01008842 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008843 ssl_buffering_free_slot( ssl, offset );
8844}
8845
8846static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8847 uint8_t slot )
8848{
8849 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8850 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008851
8852 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8853 return;
8854
Hanno Beckere605b192018-08-21 15:59:07 +01008855 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008856 {
Hanno Beckere605b192018-08-21 15:59:07 +01008857 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008858 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008859 mbedtls_free( hs_buf->data );
8860 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008861 }
8862}
8863
8864#endif /* MBEDTLS_SSL_PROTO_DTLS */
8865
Gilles Peskine9b562d52018-04-25 20:32:43 +02008866void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008867{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008868 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8869
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008870 if( handshake == NULL )
8871 return;
8872
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008873#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8874 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8875 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008876 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008877 handshake->async_in_progress = 0;
8878 }
8879#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8880
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008881#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8882 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8883 mbedtls_md5_free( &handshake->fin_md5 );
8884 mbedtls_sha1_free( &handshake->fin_sha1 );
8885#endif
8886#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8887#if defined(MBEDTLS_SHA256_C)
8888 mbedtls_sha256_free( &handshake->fin_sha256 );
8889#endif
8890#if defined(MBEDTLS_SHA512_C)
8891 mbedtls_sha512_free( &handshake->fin_sha512 );
8892#endif
8893#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_DHM_C)
8896 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898#if defined(MBEDTLS_ECDH_C)
8899 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008900#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008901#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008902 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008903#if defined(MBEDTLS_SSL_CLI_C)
8904 mbedtls_free( handshake->ecjpake_cache );
8905 handshake->ecjpake_cache = NULL;
8906 handshake->ecjpake_cache_len = 0;
8907#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008908#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008909
Janos Follath4ae5c292016-02-10 11:27:43 +00008910#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8911 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008912 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008914#endif
8915
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008916#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8917 if( handshake->psk != NULL )
8918 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008919 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008920 mbedtls_free( handshake->psk );
8921 }
8922#endif
8923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8925 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008926 /*
8927 * Free only the linked list wrapper, not the keys themselves
8928 * since the belong to the SNI callback
8929 */
8930 if( handshake->sni_key_cert != NULL )
8931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008933
8934 while( cur != NULL )
8935 {
8936 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008938 cur = next;
8939 }
8940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008942
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008943#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008944 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008945#endif
8946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947#if defined(MBEDTLS_SSL_PROTO_DTLS)
8948 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008949 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008950 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008951#endif
8952
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008953 mbedtls_platform_zeroize( handshake,
8954 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008955}
8956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008958{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008959 if( session == NULL )
8960 return;
8961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008963 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965 mbedtls_x509_crt_free( session->peer_cert );
8966 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008967 }
Paul Bakkered27a042013-04-18 22:46:23 +02008968#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008969
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008970#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008972#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008973
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008974 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008975}
8976
Paul Bakker5121ce52009-01-03 21:22:43 +00008977/*
8978 * Free an SSL context
8979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008981{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008982 if( ssl == NULL )
8983 return;
8984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008986
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008987 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008988 {
Angus Grattond8213d02016-05-25 20:56:48 +10008989 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008991 }
8992
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008993 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008994 {
Angus Grattond8213d02016-05-25 20:56:48 +10008995 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008997 }
8998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009000 if( ssl->compress_buf != NULL )
9001 {
Angus Grattond8213d02016-05-25 20:56:48 +10009002 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009004 }
9005#endif
9006
Paul Bakker48916f92012-09-16 19:57:18 +00009007 if( ssl->transform )
9008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 mbedtls_ssl_transform_free( ssl->transform );
9010 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009011 }
9012
9013 if( ssl->handshake )
9014 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009015 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9017 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 mbedtls_free( ssl->handshake );
9020 mbedtls_free( ssl->transform_negotiate );
9021 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009022 }
9023
Paul Bakkerc0463502013-02-14 11:19:38 +01009024 if( ssl->session )
9025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 mbedtls_ssl_session_free( ssl->session );
9027 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009028 }
9029
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009030#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009031 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009032 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009033 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009035 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9039 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9042 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009043 }
9044#endif
9045
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009046#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009048#endif
9049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009051
Paul Bakker86f04f42013-02-14 11:20:09 +01009052 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009053 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009054}
9055
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009056/*
9057 * Initialze mbedtls_ssl_config
9058 */
9059void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9060{
9061 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9062}
9063
Simon Butcherc97b6972015-12-27 23:48:17 +00009064#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009065static int ssl_preset_default_hashes[] = {
9066#if defined(MBEDTLS_SHA512_C)
9067 MBEDTLS_MD_SHA512,
9068 MBEDTLS_MD_SHA384,
9069#endif
9070#if defined(MBEDTLS_SHA256_C)
9071 MBEDTLS_MD_SHA256,
9072 MBEDTLS_MD_SHA224,
9073#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009074#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009075 MBEDTLS_MD_SHA1,
9076#endif
9077 MBEDTLS_MD_NONE
9078};
Simon Butcherc97b6972015-12-27 23:48:17 +00009079#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009080
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009081static int ssl_preset_suiteb_ciphersuites[] = {
9082 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9083 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9084 0
9085};
9086
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009087#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009088static int ssl_preset_suiteb_hashes[] = {
9089 MBEDTLS_MD_SHA256,
9090 MBEDTLS_MD_SHA384,
9091 MBEDTLS_MD_NONE
9092};
9093#endif
9094
9095#if defined(MBEDTLS_ECP_C)
9096static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009097#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009098 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009099#endif
9100#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009101 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009102#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009103 MBEDTLS_ECP_DP_NONE
9104};
9105#endif
9106
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009107/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009108 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009109 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009110int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009111 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009112{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009113#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009114 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009115#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009116
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009117 /* Use the functions here so that they are covered in tests,
9118 * but otherwise access member directly for efficiency */
9119 mbedtls_ssl_conf_endpoint( conf, endpoint );
9120 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009121
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009122 /*
9123 * Things that are common to all presets
9124 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009125#if defined(MBEDTLS_SSL_CLI_C)
9126 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9127 {
9128 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9129#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9130 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9131#endif
9132 }
9133#endif
9134
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009135#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009136 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009137#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009138
9139#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9140 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9141#endif
9142
9143#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9144 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9145#endif
9146
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009147#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9148 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9149#endif
9150
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009151#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009152 conf->f_cookie_write = ssl_cookie_write_dummy;
9153 conf->f_cookie_check = ssl_cookie_check_dummy;
9154#endif
9155
9156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9157 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9158#endif
9159
Janos Follath088ce432017-04-10 12:42:31 +01009160#if defined(MBEDTLS_SSL_SRV_C)
9161 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9162#endif
9163
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009164#if defined(MBEDTLS_SSL_PROTO_DTLS)
9165 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9166 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9167#endif
9168
9169#if defined(MBEDTLS_SSL_RENEGOTIATION)
9170 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009171 memset( conf->renego_period, 0x00, 2 );
9172 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009173#endif
9174
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009175#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9176 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9177 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009178 const unsigned char dhm_p[] =
9179 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9180 const unsigned char dhm_g[] =
9181 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9182
Hanno Beckera90658f2017-10-04 15:29:08 +01009183 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9184 dhm_p, sizeof( dhm_p ),
9185 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009186 {
9187 return( ret );
9188 }
9189 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009190#endif
9191
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009192 /*
9193 * Preset-specific defaults
9194 */
9195 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009196 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009197 /*
9198 * NSA Suite B
9199 */
9200 case MBEDTLS_SSL_PRESET_SUITEB:
9201 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9202 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9203 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9204 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9205
9206 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9207 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9208 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9209 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9210 ssl_preset_suiteb_ciphersuites;
9211
9212#if defined(MBEDTLS_X509_CRT_PARSE_C)
9213 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009214#endif
9215
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009216#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009217 conf->sig_hashes = ssl_preset_suiteb_hashes;
9218#endif
9219
9220#if defined(MBEDTLS_ECP_C)
9221 conf->curve_list = ssl_preset_suiteb_curves;
9222#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009223 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009224
9225 /*
9226 * Default
9227 */
9228 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009229 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9230 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9231 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9232 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9233 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9234 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9235 MBEDTLS_SSL_MIN_MINOR_VERSION :
9236 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009237 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9238 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9239
9240#if defined(MBEDTLS_SSL_PROTO_DTLS)
9241 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9242 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9243#endif
9244
9245 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9246 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9247 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9248 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9249 mbedtls_ssl_list_ciphersuites();
9250
9251#if defined(MBEDTLS_X509_CRT_PARSE_C)
9252 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9253#endif
9254
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009255#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009256 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009257#endif
9258
9259#if defined(MBEDTLS_ECP_C)
9260 conf->curve_list = mbedtls_ecp_grp_id_list();
9261#endif
9262
9263#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9264 conf->dhm_min_bitlen = 1024;
9265#endif
9266 }
9267
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009268 return( 0 );
9269}
9270
9271/*
9272 * Free mbedtls_ssl_config
9273 */
9274void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9275{
9276#if defined(MBEDTLS_DHM_C)
9277 mbedtls_mpi_free( &conf->dhm_P );
9278 mbedtls_mpi_free( &conf->dhm_G );
9279#endif
9280
9281#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9282 if( conf->psk != NULL )
9283 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009284 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009285 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009286 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009287 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009288 }
9289
9290 if( conf->psk_identity != NULL )
9291 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009292 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009293 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009294 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009295 conf->psk_identity_len = 0;
9296 }
9297#endif
9298
9299#if defined(MBEDTLS_X509_CRT_PARSE_C)
9300 ssl_key_cert_free( conf->key_cert );
9301#endif
9302
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009303 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009304}
9305
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009306#if defined(MBEDTLS_PK_C) && \
9307 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009308/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313#if defined(MBEDTLS_RSA_C)
9314 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9315 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#if defined(MBEDTLS_ECDSA_C)
9318 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9319 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009322}
9323
Hanno Becker7e5437a2017-04-28 17:15:26 +01009324unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9325{
9326 switch( type ) {
9327 case MBEDTLS_PK_RSA:
9328 return( MBEDTLS_SSL_SIG_RSA );
9329 case MBEDTLS_PK_ECDSA:
9330 case MBEDTLS_PK_ECKEY:
9331 return( MBEDTLS_SSL_SIG_ECDSA );
9332 default:
9333 return( MBEDTLS_SSL_SIG_ANON );
9334 }
9335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009338{
9339 switch( sig )
9340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_RSA_C)
9342 case MBEDTLS_SSL_SIG_RSA:
9343 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009344#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#if defined(MBEDTLS_ECDSA_C)
9346 case MBEDTLS_SSL_SIG_ECDSA:
9347 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009348#endif
9349 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009351 }
9352}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009353#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009354
Hanno Becker7e5437a2017-04-28 17:15:26 +01009355#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9356 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9357
9358/* Find an entry in a signature-hash set matching a given hash algorithm. */
9359mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9360 mbedtls_pk_type_t sig_alg )
9361{
9362 switch( sig_alg )
9363 {
9364 case MBEDTLS_PK_RSA:
9365 return( set->rsa );
9366 case MBEDTLS_PK_ECDSA:
9367 return( set->ecdsa );
9368 default:
9369 return( MBEDTLS_MD_NONE );
9370 }
9371}
9372
9373/* Add a signature-hash-pair to a signature-hash set */
9374void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9375 mbedtls_pk_type_t sig_alg,
9376 mbedtls_md_type_t md_alg )
9377{
9378 switch( sig_alg )
9379 {
9380 case MBEDTLS_PK_RSA:
9381 if( set->rsa == MBEDTLS_MD_NONE )
9382 set->rsa = md_alg;
9383 break;
9384
9385 case MBEDTLS_PK_ECDSA:
9386 if( set->ecdsa == MBEDTLS_MD_NONE )
9387 set->ecdsa = md_alg;
9388 break;
9389
9390 default:
9391 break;
9392 }
9393}
9394
9395/* Allow exactly one hash algorithm for each signature. */
9396void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9397 mbedtls_md_type_t md_alg )
9398{
9399 set->rsa = md_alg;
9400 set->ecdsa = md_alg;
9401}
9402
9403#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9404 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9405
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009406/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009407 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009410{
9411 switch( hash )
9412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_MD5_C)
9414 case MBEDTLS_SSL_HASH_MD5:
9415 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_SHA1_C)
9418 case MBEDTLS_SSL_HASH_SHA1:
9419 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009420#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#if defined(MBEDTLS_SHA256_C)
9422 case MBEDTLS_SSL_HASH_SHA224:
9423 return( MBEDTLS_MD_SHA224 );
9424 case MBEDTLS_SSL_HASH_SHA256:
9425 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#if defined(MBEDTLS_SHA512_C)
9428 case MBEDTLS_SSL_HASH_SHA384:
9429 return( MBEDTLS_MD_SHA384 );
9430 case MBEDTLS_SSL_HASH_SHA512:
9431 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009432#endif
9433 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009435 }
9436}
9437
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009438/*
9439 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9440 */
9441unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9442{
9443 switch( md )
9444 {
9445#if defined(MBEDTLS_MD5_C)
9446 case MBEDTLS_MD_MD5:
9447 return( MBEDTLS_SSL_HASH_MD5 );
9448#endif
9449#if defined(MBEDTLS_SHA1_C)
9450 case MBEDTLS_MD_SHA1:
9451 return( MBEDTLS_SSL_HASH_SHA1 );
9452#endif
9453#if defined(MBEDTLS_SHA256_C)
9454 case MBEDTLS_MD_SHA224:
9455 return( MBEDTLS_SSL_HASH_SHA224 );
9456 case MBEDTLS_MD_SHA256:
9457 return( MBEDTLS_SSL_HASH_SHA256 );
9458#endif
9459#if defined(MBEDTLS_SHA512_C)
9460 case MBEDTLS_MD_SHA384:
9461 return( MBEDTLS_SSL_HASH_SHA384 );
9462 case MBEDTLS_MD_SHA512:
9463 return( MBEDTLS_SSL_HASH_SHA512 );
9464#endif
9465 default:
9466 return( MBEDTLS_SSL_HASH_NONE );
9467 }
9468}
9469
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009470#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009471/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009472 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009473 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009474 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009475int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009478
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009479 if( ssl->conf->curve_list == NULL )
9480 return( -1 );
9481
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009482 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009483 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009484 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009485
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009486 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009487}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009488#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009489
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009490#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009491/*
9492 * Check if a hash proposed by the peer is in our list.
9493 * Return 0 if we're willing to use it, -1 otherwise.
9494 */
9495int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9496 mbedtls_md_type_t md )
9497{
9498 const int *cur;
9499
9500 if( ssl->conf->sig_hashes == NULL )
9501 return( -1 );
9502
9503 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9504 if( *cur == (int) md )
9505 return( 0 );
9506
9507 return( -1 );
9508}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009509#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#if defined(MBEDTLS_X509_CRT_PARSE_C)
9512int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9513 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009514 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009515 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009516{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009517 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009519 int usage = 0;
9520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009522 const char *ext_oid;
9523 size_t ext_len;
9524#endif
9525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9527 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009528 ((void) cert);
9529 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009530 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009531#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9534 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009535 {
9536 /* Server part of the key exchange */
9537 switch( ciphersuite->key_exchange )
9538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 case MBEDTLS_KEY_EXCHANGE_RSA:
9540 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009541 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009542 break;
9543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9545 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9546 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9547 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009548 break;
9549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9551 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009552 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009553 break;
9554
9555 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 case MBEDTLS_KEY_EXCHANGE_NONE:
9557 case MBEDTLS_KEY_EXCHANGE_PSK:
9558 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9559 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009560 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009561 usage = 0;
9562 }
9563 }
9564 else
9565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9567 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009568 }
9569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009571 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009572 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009573 ret = -1;
9574 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009575#else
9576 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9580 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9583 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009584 }
9585 else
9586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9588 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009589 }
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009592 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009593 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009594 ret = -1;
9595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009597
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009598 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009601
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009602/*
9603 * Convert version numbers to/from wire format
9604 * and, for DTLS, to/from TLS equivalent.
9605 *
9606 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009607 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009608 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9609 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009612 unsigned char ver[2] )
9613{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_SSL_PROTO_DTLS)
9615 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009618 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9619
9620 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9621 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9622 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009623 else
9624#else
9625 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009626#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009627 {
9628 ver[0] = (unsigned char) major;
9629 ver[1] = (unsigned char) minor;
9630 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009631}
9632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009634 const unsigned char ver[2] )
9635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636#if defined(MBEDTLS_SSL_PROTO_DTLS)
9637 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009638 {
9639 *major = 255 - ver[0] + 2;
9640 *minor = 255 - ver[1] + 1;
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009643 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9644 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009645 else
9646#else
9647 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009648#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009649 {
9650 *major = ver[0];
9651 *minor = ver[1];
9652 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009653}
9654
Simon Butcher99000142016-10-13 17:21:01 +01009655int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9656{
9657#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9658 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9659 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9660
9661 switch( md )
9662 {
9663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9664#if defined(MBEDTLS_MD5_C)
9665 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009666 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009667#endif
9668#if defined(MBEDTLS_SHA1_C)
9669 case MBEDTLS_SSL_HASH_SHA1:
9670 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9671 break;
9672#endif
9673#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9674#if defined(MBEDTLS_SHA512_C)
9675 case MBEDTLS_SSL_HASH_SHA384:
9676 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9677 break;
9678#endif
9679#if defined(MBEDTLS_SHA256_C)
9680 case MBEDTLS_SSL_HASH_SHA256:
9681 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9682 break;
9683#endif
9684 default:
9685 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9686 }
9687
9688 return 0;
9689#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9690 (void) ssl;
9691 (void) md;
9692
9693 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9695}
9696
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009697#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9698 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9699int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9700 unsigned char *output,
9701 unsigned char *data, size_t data_len )
9702{
9703 int ret = 0;
9704 mbedtls_md5_context mbedtls_md5;
9705 mbedtls_sha1_context mbedtls_sha1;
9706
9707 mbedtls_md5_init( &mbedtls_md5 );
9708 mbedtls_sha1_init( &mbedtls_sha1 );
9709
9710 /*
9711 * digitally-signed struct {
9712 * opaque md5_hash[16];
9713 * opaque sha_hash[20];
9714 * };
9715 *
9716 * md5_hash
9717 * MD5(ClientHello.random + ServerHello.random
9718 * + ServerParams);
9719 * sha_hash
9720 * SHA(ClientHello.random + ServerHello.random
9721 * + ServerParams);
9722 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009723 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009724 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009726 goto exit;
9727 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009728 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009729 ssl->handshake->randbytes, 64 ) ) != 0 )
9730 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009732 goto exit;
9733 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009734 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009735 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009737 goto exit;
9738 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009739 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009740 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009742 goto exit;
9743 }
9744
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009745 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009746 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009748 goto exit;
9749 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009750 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009751 ssl->handshake->randbytes, 64 ) ) != 0 )
9752 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009754 goto exit;
9755 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009756 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009757 data_len ) ) != 0 )
9758 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009760 goto exit;
9761 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009762 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009763 output + 16 ) ) != 0 )
9764 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009766 goto exit;
9767 }
9768
9769exit:
9770 mbedtls_md5_free( &mbedtls_md5 );
9771 mbedtls_sha1_free( &mbedtls_sha1 );
9772
9773 if( ret != 0 )
9774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9775 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9776
9777 return( ret );
9778
9779}
9780#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9781 MBEDTLS_SSL_PROTO_TLS1_1 */
9782
9783#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9784 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9785int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009786 unsigned char *hash, size_t *hashlen,
9787 unsigned char *data, size_t data_len,
9788 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009789{
9790 int ret = 0;
9791 mbedtls_md_context_t ctx;
9792 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009793 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009794
9795 mbedtls_md_init( &ctx );
9796
9797 /*
9798 * digitally-signed struct {
9799 * opaque client_random[32];
9800 * opaque server_random[32];
9801 * ServerDHParams params;
9802 * };
9803 */
9804 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9805 {
9806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9807 goto exit;
9808 }
9809 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9810 {
9811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9812 goto exit;
9813 }
9814 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9815 {
9816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9817 goto exit;
9818 }
9819 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9820 {
9821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9822 goto exit;
9823 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009824 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009825 {
9826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9827 goto exit;
9828 }
9829
9830exit:
9831 mbedtls_md_free( &ctx );
9832
9833 if( ret != 0 )
9834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9835 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9836
9837 return( ret );
9838}
9839#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9840 MBEDTLS_SSL_PROTO_TLS1_2 */
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#endif /* MBEDTLS_SSL_TLS_C */