blob: 05176ba2e910184101cf915c13c419c2f25548c3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SSL 3.0 specification was drafted by Netscape in 1996,
48 * and became an IETF standard in 1999.
49 *
50 * http://wp.netscape.com/eng/ssl3/
51 * http://www.ietf.org/rfc/rfc2246.txt
52 * http://www.ietf.org/rfc/rfc4346.txt
53 */
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000062
SimonBd5800b72016-04-26 07:43:27 +010063#if defined(MBEDTLS_PLATFORM_C)
64#include "mbedtls/platform.h"
65#else
66#include <stdlib.h>
67#define mbedtls_calloc calloc
68#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010069#endif
70
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/debug.h"
72#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020073#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050074#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020075
Rich Evans00ab4702015-02-06 13:43:58 +000076#include <string.h>
77
Janos Follath23bdca02016-10-07 14:47:14 +010078#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020080#endif
81
Hanno Becker2a43f6f2018-08-10 11:12:52 +010082static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010083static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010084
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010085/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010090 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010091#else
92 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010093#endif
94 return( 0 );
95}
96
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097/*
98 * Start a timer.
99 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200103 if( ssl->f_set_timer == NULL )
104 return;
105
106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
107 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200108}
109
110/*
111 * Return -1 is timer is expired, 0 if it isn't.
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200114{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200115 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200116 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200117
118 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119 {
120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200121 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200122 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200123
124 return( 0 );
125}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200126
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100127static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
128 mbedtls_ssl_transform *transform );
129static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
130 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100131
132#define SSL_DONT_FORCE_FLUSH 0
133#define SSL_FORCE_FLUSH 1
134
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
Hanno Beckerd5847772018-08-28 10:09:23 +0100137/* Forward declarations for functions related to message buffering. */
138static void ssl_buffering_free( mbedtls_ssl_context *ssl );
139static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
140 uint8_t slot );
141static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
142static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
143static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
144static int ssl_buffer_message( mbedtls_ssl_context *ssl );
145static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100146static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100147
Hanno Beckera67dee22018-08-22 10:05:20 +0100148static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100149static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100150{
Hanno Becker11682cc2018-08-22 14:41:02 +0100151 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100152
153 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100154 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100155
156 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
157}
158
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
160{
Hanno Becker11682cc2018-08-22 14:41:02 +0100161 size_t const bytes_written = ssl->out_left;
162 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100163
164 /* Double-check that the write-index hasn't gone
165 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100166 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100167 {
168 /* Should never happen... */
169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
170 }
171
172 return( (int) ( mtu - bytes_written ) );
173}
174
175static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
176{
177 int ret;
178 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400179 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100180
181#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
182 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
183
184 if( max_len > mfl )
185 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100186
187 /* By the standard (RFC 6066 Sect. 4), the MFL extension
188 * only limits the maximum record payload size, so in theory
189 * we would be allowed to pack multiple records of payload size
190 * MFL into a single datagram. However, this would mean that there's
191 * no way to explicitly communicate MTU restrictions to the peer.
192 *
193 * The following reduction of max_len makes sure that we never
194 * write datagrams larger than MFL + Record Expansion Overhead.
195 */
196 if( max_len <= ssl->out_left )
197 return( 0 );
198
199 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100200#endif
201
202 ret = ssl_get_remaining_space_in_datagram( ssl );
203 if( ret < 0 )
204 return( ret );
205 remaining = (size_t) ret;
206
207 ret = mbedtls_ssl_get_record_expansion( ssl );
208 if( ret < 0 )
209 return( ret );
210 expansion = (size_t) ret;
211
212 if( remaining <= expansion )
213 return( 0 );
214
215 remaining -= expansion;
216 if( remaining >= max_len )
217 remaining = max_len;
218
219 return( (int) remaining );
220}
221
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200222/*
223 * Double the retransmit timeout value, within the allowed range,
224 * returning -1 if the maximum value has already been reached.
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200227{
228 uint32_t new_timeout;
229
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200230 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200231 return( -1 );
232
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200233 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
234 * in the following way: after the initial transmission and a first
235 * retransmission, back off to a temporary estimated MTU of 508 bytes.
236 * This value is guaranteed to be deliverable (if not guaranteed to be
237 * delivered) of any compliant IPv4 (and IPv6) network, and should work
238 * on most non-IP stacks too. */
239 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400240 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200241 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
243 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200244
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200245 new_timeout = 2 * ssl->handshake->retransmit_timeout;
246
247 /* Avoid arithmetic overflow and range overflow */
248 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200249 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200250 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200251 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252 }
253
254 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256 ssl->handshake->retransmit_timeout ) );
257
258 return( 0 );
259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200263 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200265 ssl->handshake->retransmit_timeout ) );
266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200270/*
271 * Convert max_fragment_length codes to length.
272 * RFC 6066 says:
273 * enum{
274 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
275 * } MaxFragmentLength;
276 * and we add 0 -> extension unused
277 */
Angus Grattond8213d02016-05-25 20:56:48 +1000278static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200279{
Angus Grattond8213d02016-05-25 20:56:48 +1000280 switch( mfl )
281 {
282 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
283 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
284 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
285 return 512;
286 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
287 return 1024;
288 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
289 return 2048;
290 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
291 return 4096;
292 default:
293 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
294 }
295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200297
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200298#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_ssl_session_free( dst );
302 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 if( src->peer_cert != NULL )
306 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200307 int ret;
308
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200309 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200310 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200311 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200316 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319 dst->peer_cert = NULL;
320 return( ret );
321 }
322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200325#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326 if( src->ticket != NULL )
327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200329 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200331
332 memcpy( dst->ticket, src->ticket, src->ticket_len );
333 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200334#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 return( 0 );
337}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200338#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
518static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100519 const unsigned char *secret, size_t slen,
520 const char *label,
521 const unsigned char *random, size_t rlen,
522 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523{
524 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100525 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000526 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
528 const mbedtls_md_info_t *md_info;
529 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100530 int ret;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100538
539 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000541
542 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100543 memcpy( tmp + md_len, label, nb );
544 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000545 nb += rlen;
546
547 /*
548 * Compute P_<hash>(secret, label + random)[0..dlen]
549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551 return( ret );
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
554 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
555 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100556
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100557 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_md_hmac_reset ( &md_ctx );
560 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
561 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_md_hmac_reset ( &md_ctx );
564 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
565 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000566
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100567 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
569 for( j = 0; j < k; j++ )
570 dstbuf[i + j] = h_i[j];
571 }
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500575 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
576 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000577
578 return( 0 );
579}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100582static int tls_prf_sha256( const unsigned char *secret, size_t slen,
583 const char *label,
584 const unsigned char *random, size_t rlen,
585 unsigned char *dstbuf, size_t dlen )
586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588 label, random, rlen, dstbuf, dlen ) );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593static int tls_prf_sha384( const unsigned char *secret, size_t slen,
594 const char *label,
595 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000596 unsigned char *dstbuf, size_t dlen )
597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100599 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_SHA512_C */
602#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
607 defined(MBEDTLS_SSL_PROTO_TLS1_1)
608static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
Paul Bakker380da532012-04-18 16:10:25 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_SSL_PROTO_SSL3)
612static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
613static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200614#endif
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
617static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
618static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#endif
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
622#if defined(MBEDTLS_SHA256_C)
623static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
624static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
625static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SHA512_C)
629static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
630static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
631static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000636{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200637 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 unsigned char keyblk[256];
640 unsigned char *key1;
641 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100642 unsigned char *mac_enc;
643 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000644 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200645 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 const mbedtls_cipher_info_t *cipher_info;
647 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_ssl_session *session = ssl->session_negotiate;
650 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
651 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100656 if( cipher_info == NULL )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100659 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100661 }
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100664 if( md_info == NULL )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100667 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100669 }
670
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_SSL3)
675 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000676 {
Paul Bakker48916f92012-09-16 19:57:18 +0000677 handshake->tls_prf = ssl3_prf;
678 handshake->calc_verify = ssl_calc_verify_ssl;
679 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000680 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200681 else
682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
684 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 {
Paul Bakker48916f92012-09-16 19:57:18 +0000686 handshake->tls_prf = tls1_prf;
687 handshake->calc_verify = ssl_calc_verify_tls;
688 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200690 else
691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
693#if defined(MBEDTLS_SHA512_C)
694 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
695 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000696 {
Paul Bakker48916f92012-09-16 19:57:18 +0000697 handshake->tls_prf = tls_prf_sha384;
698 handshake->calc_verify = ssl_calc_verify_tls_sha384;
699 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000700 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000701 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SHA256_C)
704 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000705 {
Paul Bakker48916f92012-09-16 19:57:18 +0000706 handshake->tls_prf = tls_prf_sha256;
707 handshake->calc_verify = ssl_calc_verify_tls_sha256;
708 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200710 else
711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200716 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000717
718 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 * SSLv3:
720 * master =
721 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
722 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
723 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200724 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 * master = PRF( premaster, "master secret", randbytes )[0..47]
727 */
Paul Bakker0a597072012-09-25 21:55:46 +0000728 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000731 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
734 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200735 {
736 unsigned char session_hash[48];
737 size_t hash_len;
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200740
741 ssl->handshake->calc_verify( ssl, session_hash );
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
744 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200747 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200749 {
750 hash_len = 48;
751 }
752 else
753#endif
754 hash_len = 32;
755 }
756 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200758 hash_len = 36;
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200761
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100762 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
763 "extended master secret",
764 session_hash, hash_len,
765 session->master, 48 );
766 if( ret != 0 )
767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100769 return( ret );
770 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200771
772 }
773 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200774#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100775 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
776 "master secret",
777 handshake->randbytes, 64,
778 session->master, 48 );
779 if( ret != 0 )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100782 return( ret );
783 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200784
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500785 mbedtls_platform_zeroize( handshake->premaster,
786 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 /*
792 * Swap the client and server random values.
793 */
Paul Bakker48916f92012-09-16 19:57:18 +0000794 memcpy( tmp, handshake->randbytes, 64 );
795 memcpy( handshake->randbytes, tmp + 32, 32 );
796 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500797 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
799 /*
800 * SSLv3:
801 * key block =
802 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
803 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
804 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
805 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
806 * ...
807 *
808 * TLSv1:
809 * key block = PRF( master, "key expansion", randbytes )
810 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100811 ret = handshake->tls_prf( session->master, 48, "key expansion",
812 handshake->randbytes, 64, keyblk, 256 );
813 if( ret != 0 )
814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100816 return( ret );
817 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
820 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
822 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
823 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500825 mbedtls_platform_zeroize( handshake->randbytes,
826 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
828 /*
829 * Determine the appropriate key, IV and MAC length.
830 */
Paul Bakker68884e32013-01-07 18:20:04 +0100831
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200832 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200835 cipher_info->mode == MBEDTLS_MODE_CCM ||
836 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200838 size_t taglen, explicit_ivlen;
839
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200840 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000841 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200843 /* All modes haves 96-bit IVs;
844 * GCM and CCM has 4 implicit and 8 explicit bytes
845 * ChachaPoly has all 12 bytes implicit
846 */
Paul Bakker68884e32013-01-07 18:20:04 +0100847 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200848 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
849 transform->fixed_ivlen = 12;
850 else
851 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200853 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
854 taglen = transform->ciphersuite_info->flags &
855 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
856
857
858 /* Minimum length of encrypted record */
859 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
860 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100861 }
862 else
863 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200864 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
866 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200869 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100870 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = mbedtls_md_get_size( md_info );
874 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877 /*
878 * If HMAC is to be truncated, we shall keep the leftmost bytes,
879 * (rfc 6066 page 13 or rfc 2104 section 4),
880 * so we only need to adjust the length here.
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000885
886#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
887 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000888 * HMAC implementation which also truncates the key
889 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000890 mac_key_len = transform->maclen;
891#endif
892 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200894
895 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100896 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200898 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 transform->minlen = transform->maclen;
901 else
Paul Bakker68884e32013-01-07 18:20:04 +0100902 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200903 /*
904 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100905 * 1. if EtM is in use: one block plus MAC
906 * otherwise: * first multiple of blocklen greater than maclen
907 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
910 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100911 {
912 transform->minlen = transform->maclen
913 + cipher_info->block_size;
914 }
915 else
916#endif
917 {
918 transform->minlen = transform->maclen
919 + cipher_info->block_size
920 - transform->maclen % cipher_info->block_size;
921 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
924 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
925 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200926 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100927 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
930 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
931 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200932 {
933 transform->minlen += transform->ivlen;
934 }
935 else
936#endif
937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 }
Paul Bakker68884e32013-01-07 18:20:04 +0100941 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 }
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000945 transform->keylen, transform->minlen, transform->ivlen,
946 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
948 /*
949 * Finally setup the cipher contexts, IVs and MAC secrets.
950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000954 key1 = keyblk + mac_key_len * 2;
955 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Paul Bakker68884e32013-01-07 18:20:04 +0100957 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000958 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000960 /*
961 * This is not used in TLS v1.1.
962 */
Paul Bakker48916f92012-09-16 19:57:18 +0000963 iv_copy_len = ( transform->fixed_ivlen ) ?
964 transform->fixed_ivlen : transform->ivlen;
965 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
966 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000967 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 }
969 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#endif /* MBEDTLS_SSL_CLI_C */
971#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 key1 = keyblk + mac_key_len * 2 + transform->keylen;
975 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Hanno Becker81c7b182017-11-09 18:39:33 +0000977 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100978 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000980 /*
981 * This is not used in TLS v1.1.
982 */
Paul Bakker48916f92012-09-16 19:57:18 +0000983 iv_copy_len = ( transform->fixed_ivlen ) ?
984 transform->fixed_ivlen : transform->ivlen;
985 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
986 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000987 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100994 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_PROTO_SSL3)
997 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001003 }
1004
Hanno Becker81c7b182017-11-09 18:39:33 +00001005 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1006 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001007 }
1008 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1010#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1011 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1012 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001013 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001014 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1015 For AEAD-based ciphersuites, there is nothing to do here. */
1016 if( mac_key_len != 0 )
1017 {
1018 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1019 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1020 }
Paul Bakker68884e32013-01-07 18:20:04 +01001021 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001022 else
1023#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001027 }
Paul Bakker68884e32013-01-07 18:20:04 +01001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1030 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001035 transform->iv_enc, transform->iv_dec,
1036 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001037 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001038 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1041 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001042 }
1043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001045
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001046#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1047 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001048 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001049 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1050 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001052 iv_copy_len );
1053 }
1054#endif
1055
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001056 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001057 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001060 return( ret );
1061 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001062
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001063 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001064 cipher_info ) ) != 0 )
1065 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001067 return( ret );
1068 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001071 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075 return( ret );
1076 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001079 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083 return( ret );
1084 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_CIPHER_MODE_CBC)
1087 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1090 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001094 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1097 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 return( ret );
1101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001105 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001108 // Initialize compression
1109 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001111 {
Paul Bakker16770332013-10-11 09:59:44 +02001112 if( ssl->compress_buf == NULL )
1113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001115 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001116 if( ssl->compress_buf == NULL )
1117 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001119 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001120 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001121 }
1122 }
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001125
Paul Bakker48916f92012-09-16 19:57:18 +00001126 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1127 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001128
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001129 if( deflateInit( &transform->ctx_deflate,
1130 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001131 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1134 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001135 }
1136 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 return( 0 );
1142}
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_SSL_PROTO_SSL3)
1145void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001147 mbedtls_md5_context md5;
1148 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 unsigned char pad_1[48];
1150 unsigned char pad_2[48];
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001154 mbedtls_md5_init( &md5 );
1155 mbedtls_sha1_init( &sha1 );
1156
1157 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1158 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker380da532012-04-18 16:10:25 +00001160 memset( pad_1, 0x36, 48 );
1161 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001163 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1164 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1165 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001167 mbedtls_md5_starts_ret( &md5 );
1168 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1169 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1170 mbedtls_md5_update_ret( &md5, hash, 16 );
1171 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001173 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1174 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1175 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001176
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001177 mbedtls_sha1_starts_ret( &sha1 );
1178 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1179 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1180 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1181 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001185
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001186 mbedtls_md5_free( &md5 );
1187 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001188
Paul Bakker380da532012-04-18 16:10:25 +00001189 return;
1190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1194void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001195{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001196 mbedtls_md5_context md5;
1197 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001201 mbedtls_md5_init( &md5 );
1202 mbedtls_sha1_init( &sha1 );
1203
1204 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1205 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001206
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001207 mbedtls_md5_finish_ret( &md5, hash );
1208 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001212
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001213 mbedtls_md5_free( &md5 );
1214 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001215
Paul Bakker380da532012-04-18 16:10:25 +00001216 return;
1217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1221#if defined(MBEDTLS_SHA256_C)
1222void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001223{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001224 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001225
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001226 mbedtls_sha256_init( &sha256 );
1227
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001229
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001230 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001231 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001235
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001236 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001237
Paul Bakker380da532012-04-18 16:10:25 +00001238 return;
1239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_SHA512_C)
1243void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001244{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001245 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001246
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001247 mbedtls_sha512_init( &sha512 );
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001250
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001251 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001252 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001257 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 return;
1260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#endif /* MBEDTLS_SHA512_C */
1262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1265int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001266{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001267 unsigned char *p = ssl->handshake->premaster;
1268 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001269 const unsigned char *psk = ssl->conf->psk;
1270 size_t psk_len = ssl->conf->psk_len;
1271
1272 /* If the psk callback was called, use its result */
1273 if( ssl->handshake->psk != NULL )
1274 {
1275 psk = ssl->handshake->psk;
1276 psk_len = ssl->handshake->psk_len;
1277 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001278
1279 /*
1280 * PMS = struct {
1281 * opaque other_secret<0..2^16-1>;
1282 * opaque psk<0..2^16-1>;
1283 * };
1284 * with "other_secret" depending on the particular key exchange
1285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1287 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001288 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001289 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001292 *(p++) = (unsigned char)( psk_len >> 8 );
1293 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001294
1295 if( end < p || (size_t)( end - p ) < psk_len )
1296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1297
1298 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001299 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001300 }
1301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1303#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1304 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001305 {
1306 /*
1307 * other_secret already set by the ClientKeyExchange message,
1308 * and is 48 bytes long
1309 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001310 if( end - p < 2 )
1311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1312
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001313 *p++ = 0;
1314 *p++ = 48;
1315 p += 48;
1316 }
1317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1319#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1320 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001321 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001322 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001323 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001324
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001325 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001327 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001328 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001331 return( ret );
1332 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001333 *(p++) = (unsigned char)( len >> 8 );
1334 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001335 p += len;
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001338 }
1339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1341#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1342 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001343 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001344 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001345 size_t zlen;
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001348 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001349 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001352 return( ret );
1353 }
1354
1355 *(p++) = (unsigned char)( zlen >> 8 );
1356 *(p++) = (unsigned char)( zlen );
1357 p += zlen;
1358
Janos Follath3fbdada2018-08-15 10:26:53 +01001359 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1360 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001361 }
1362 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001367 }
1368
1369 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001370 if( end - p < 2 )
1371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001372
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001373 *(p++) = (unsigned char)( psk_len >> 8 );
1374 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001375
1376 if( end < p || (size_t)( end - p ) < psk_len )
1377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1378
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001379 memcpy( p, psk, psk_len );
1380 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001381
1382 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1383
1384 return( 0 );
1385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001389/*
1390 * SSLv3.0 MAC functions
1391 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001392#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001393static void ssl_mac( mbedtls_md_context_t *md_ctx,
1394 const unsigned char *secret,
1395 const unsigned char *buf, size_t len,
1396 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001397 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398{
1399 unsigned char header[11];
1400 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001401 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1403 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001404
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001405 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001407 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001408 else
Paul Bakker68884e32013-01-07 18:20:04 +01001409 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
1411 memcpy( header, ctr, 8 );
1412 header[ 8] = (unsigned char) type;
1413 header[ 9] = (unsigned char)( len >> 8 );
1414 header[10] = (unsigned char)( len );
1415
Paul Bakker68884e32013-01-07 18:20:04 +01001416 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_md_starts( md_ctx );
1418 mbedtls_md_update( md_ctx, secret, md_size );
1419 mbedtls_md_update( md_ctx, padding, padlen );
1420 mbedtls_md_update( md_ctx, header, 11 );
1421 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001422 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Paul Bakker68884e32013-01-07 18:20:04 +01001424 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_md_starts( md_ctx );
1426 mbedtls_md_update( md_ctx, secret, md_size );
1427 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001428 mbedtls_md_update( md_ctx, out, md_size );
1429 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001434 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001435#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001436#endif
1437
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001438/* The function below is only used in the Lucky 13 counter-measure in
1439 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1440#if defined(SSL_SOME_MODES_USE_MAC) && \
1441 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1442 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1443 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1444/* This function makes sure every byte in the memory region is accessed
1445 * (in ascending addresses order) */
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001446static void ssl_read_memory( const unsigned char *p, size_t len )
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001447{
1448 unsigned char acc = 0;
1449 volatile unsigned char force;
1450
1451 for( ; len != 0; p++, len-- )
1452 acc ^= *p;
1453
1454 force = acc;
1455 (void) force;
1456}
1457#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1458
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001459/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001469 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001478 ssl->out_msg, ssl->out_msglen );
1479
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001481 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001483#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( mode == MBEDTLS_MODE_STREAM ||
1485 ( mode == MBEDTLS_MODE_CBC
1486#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1487 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001488#endif
1489 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_SSL_PROTO_SSL3)
1492 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001493 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001494 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001495
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001496 ssl_mac( &ssl->transform_out->md_ctx_enc,
1497 ssl->transform_out->mac_enc,
1498 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001499 ssl->out_ctr, ssl->out_msgtype,
1500 mac );
1501
1502 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001503 }
1504 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1507 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1508 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001509 {
Hanno Becker992b6872017-11-09 18:57:39 +00001510 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1513 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1514 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1515 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001516 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001517 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001519
1520 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001521 }
1522 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001527 }
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001530 ssl->out_msg + ssl->out_msglen,
1531 ssl->transform_out->maclen );
1532
1533 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001534 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001535 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001536#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001538 /*
1539 * Encrypt
1540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1542 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001544 int ret;
1545 size_t olen = 0;
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 "including %d bytes of padding",
1549 ssl->out_msglen, 0 ) );
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001552 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001553 ssl->transform_out->ivlen,
1554 ssl->out_msg, ssl->out_msglen,
1555 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558 return( ret );
1559 }
1560
1561 if( ssl->out_msglen != olen )
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Paul Bakker68884e32013-01-07 18:20:04 +01001567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001569#if defined(MBEDTLS_GCM_C) || \
1570 defined(MBEDTLS_CCM_C) || \
1571 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001573 mode == MBEDTLS_MODE_CCM ||
1574 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001575 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001576 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001577 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001578 unsigned char *enc_msg;
1579 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 unsigned char iv[12];
1581 mbedtls_ssl_transform *transform = ssl->transform_out;
1582 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001584 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 /*
1587 * Prepare additional authenticated data
1588 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001589 memcpy( add_data, ssl->out_ctr, 8 );
1590 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001592 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001593 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1594 add_data[12] = ssl->out_msglen & 0xFF;
1595
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001596 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001597
Paul Bakker68884e32013-01-07 18:20:04 +01001598 /*
1599 * Generate IV
1600 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001601 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1602 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001603 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001604 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1605 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1606 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1607
1608 }
1609 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1610 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001611 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001612 unsigned char i;
1613
1614 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1615
1616 for( i = 0; i < 8; i++ )
1617 iv[i+4] ^= ssl->out_ctr[i];
1618 }
1619 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001620 {
1621 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001624 }
1625
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001626 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1627 iv, transform->ivlen );
1628 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1629 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001630
Paul Bakker68884e32013-01-07 18:20:04 +01001631 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001632 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001633 */
1634 enc_msg = ssl->out_msg;
1635 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001636 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001639 "including 0 bytes of padding",
1640 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001641
Paul Bakker68884e32013-01-07 18:20:04 +01001642 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001643 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001644 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001645 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1646 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001647 add_data, 13,
1648 enc_msg, enc_msglen,
1649 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001650 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001653 return( ret );
1654 }
1655
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001656 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001660 }
1661
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001662 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001663 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001669#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001672 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001673 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001674 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675
Paul Bakker48916f92012-09-16 19:57:18 +00001676 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1677 ssl->transform_out->ivlen;
1678 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 padlen = 0;
1680
1681 for( i = 0; i <= padlen; i++ )
1682 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1683
1684 ssl->out_msglen += padlen + 1;
1685
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001686 enc_msglen = ssl->out_msglen;
1687 enc_msg = ssl->out_msg;
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001690 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001691 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1692 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 {
1696 /*
1697 * Generate IV
1698 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001699 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001700 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001701 if( ret != 0 )
1702 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001703
Paul Bakker92be97b2013-01-02 17:30:03 +01001704 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001705 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001706
1707 /*
1708 * Fix pointer positions and message length with added IV
1709 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001710 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001711 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001712 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001718 ssl->out_msglen, ssl->transform_out->ivlen,
1719 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001722 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001723 ssl->transform_out->ivlen,
1724 enc_msg, enc_msglen,
1725 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001728 return( ret );
1729 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001730
Paul Bakkercca5b812013-08-31 17:40:26 +02001731 if( enc_msglen != olen )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001735 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1738 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001739 {
1740 /*
1741 * Save IV in SSL3 and TLS1
1742 */
1743 memcpy( ssl->transform_out->iv_enc,
1744 ssl->transform_out->cipher_ctx_enc.iv,
1745 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001747#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001752 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1753
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001754 /*
1755 * MAC(MAC_write_key, seq_num +
1756 * TLSCipherText.type +
1757 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001758 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759 * IV + // except for TLS 1.0
1760 * ENC(content + padding + padding_length));
1761 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 unsigned char pseudo_hdr[13];
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1767 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001768 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1769 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1774 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001775 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001776 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001778
Hanno Becker3d8c9072018-01-05 16:24:22 +00001779 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1780 ssl->transform_out->maclen );
1781
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001783 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001787 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001788#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001794 /* Make extra sure authentication was performed, exactly once */
1795 if( auth_done != 1 )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
1803 return( 0 );
1804}
1805
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001806#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001807/*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001808 * Constant-flow conditional memcpy:
1809 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1810 * - otherwise, a no-op,
1811 * but with execution flow independent of the values of c1 and c2.
1812 *
1813 * Use only bit operations to avoid branches that could be used by some
1814 * compilers on some platforms to translate comparison operators.
1815 */
Manuel Pégourié-Gonnard94fd8dc2020-07-28 11:56:05 +02001816static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1817 const unsigned char *src,
1818 size_t len,
1819 size_t c1, size_t c2 )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001820{
1821 /* diff = 0 if c1 == c2, non-zero otherwise */
1822 const size_t diff = c1 ^ c2;
1823
1824 /* MSVC has a warning about unary minus on unsigned integer types,
1825 * but this is well-defined and precisely what we want to do here. */
1826#if defined(_MSC_VER)
1827#pragma warning( push )
1828#pragma warning( disable : 4146 )
1829#endif
1830
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001831 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001832 const size_t diff_msb = ( diff | -diff );
1833
1834 /* diff1 = c1 != c2 */
1835 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1836
1837 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001838 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001839
1840#if defined(_MSC_VER)
1841#pragma warning( pop )
1842#endif
1843
1844 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnard253b0de2020-07-29 10:04:36 +02001845 size_t i;
1846 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001847 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1848}
1849
1850/*
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001851 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnarde1b1fff2020-07-29 12:54:04 +02001852 *
1853 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1854 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001855 */
1856int mbedtls_ssl_cf_hmac(
1857 mbedtls_md_context_t *ctx,
1858 const unsigned char *add_data, size_t add_data_len,
1859 const unsigned char *data, size_t data_len_secret,
1860 size_t min_data_len, size_t max_data_len,
1861 unsigned char *output )
1862{
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001863 /*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001864 * This function breaks the HMAC abstraction and uses the md_clone()
1865 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001866 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001867 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001868 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001869 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001870 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001871 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1872 * minlen, then cloning the context, and for each byte up to maxlen
1873 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001874 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001875 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001876 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001877 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001878 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1879 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001880 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnarda6c13172020-07-28 11:45:02 +02001881 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001882 const unsigned char * const okey = ikey + block_size;
1883 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001884
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001885 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1886 mbedtls_md_context_t aux;
1887 size_t offset;
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001888 int ret;
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001889
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001890 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001891
1892#define MD_CHK( func_call ) \
1893 do { \
1894 ret = (func_call); \
1895 if( ret != 0 ) \
1896 goto cleanup; \
1897 } while( 0 )
1898
1899 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001900
1901 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1902 * so we can start directly with the message */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001903 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1904 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001905
1906 /* For each possible length, compute the hash up to that point */
1907 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001908 {
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001909 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1910 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001911 /* Keep only the correct inner_hash in the output buffer */
1912 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1913 offset, data_len_secret );
1914
1915 if( offset < max_data_len )
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001916 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001917 }
1918
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001919 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001920 MD_CHK( mbedtls_md_starts( ctx ) );
1921 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1922 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1923 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001924
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001925 /* Done, get ready for next time */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001926 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001927
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001928#undef MD_CHK
1929
1930cleanup:
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001931 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02001932 return( ret );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001933}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001934#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001939 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001940#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001941 size_t padlen = 0, correct = 1;
1942#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001946 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950 }
1951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001953
Paul Bakker48916f92012-09-16 19:57:18 +00001954 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001957 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 }
1960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1962 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001963 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001964 int ret;
1965 size_t olen = 0;
1966
Paul Bakker68884e32013-01-07 18:20:04 +01001967 padlen = 0;
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001970 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001971 ssl->transform_in->ivlen,
1972 ssl->in_msg, ssl->in_msglen,
1973 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001976 return( ret );
1977 }
1978
1979 if( ssl->in_msglen != olen )
1980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 }
Paul Bakker68884e32013-01-07 18:20:04 +01001985 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001987#if defined(MBEDTLS_GCM_C) || \
1988 defined(MBEDTLS_CCM_C) || \
1989 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001991 mode == MBEDTLS_MODE_CCM ||
1992 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001993 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001994 int ret;
1995 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001996 unsigned char *dec_msg;
1997 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001998 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001999 unsigned char iv[12];
2000 mbedtls_ssl_transform *transform = ssl->transform_in;
2001 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002003 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002004
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002005 /*
2006 * Compute and update sizes
2007 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002008 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002011 "+ taglen (%d)", ssl->in_msglen,
2012 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002014 }
2015 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2016
Paul Bakker68884e32013-01-07 18:20:04 +01002017 dec_msg = ssl->in_msg;
2018 dec_msg_result = ssl->in_msg;
2019 ssl->in_msglen = dec_msglen;
2020
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 /*
2022 * Prepare additional authenticated data
2023 */
Paul Bakker68884e32013-01-07 18:20:04 +01002024 memcpy( add_data, ssl->in_ctr, 8 );
2025 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002027 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002028 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2029 add_data[12] = ssl->in_msglen & 0xFF;
2030
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002031 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002032
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002033 /*
2034 * Prepare IV
2035 */
2036 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2037 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002038 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2040 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002041
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 }
2043 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2044 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002045 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 unsigned char i;
2047
2048 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2049
2050 for( i = 0; i < 8; i++ )
2051 iv[i+4] ^= ssl->in_ctr[i];
2052 }
2053 else
2054 {
2055 /* Reminder if we ever add an AEAD mode with a different size */
2056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2058 }
2059
2060 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002062
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002063 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002064 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002068 add_data, 13,
2069 dec_msg, dec_msglen,
2070 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002071 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2076 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002077
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002078 return( ret );
2079 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002080 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002081
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002082 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002086 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002090#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 {
Paul Bakker45829992013-01-03 14:52:21 +01002093 /*
2094 * Decrypt and check the padding
2095 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002096 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002097 unsigned char *dec_msg;
2098 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002099 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002101 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 /*
Paul Bakker45829992013-01-03 14:52:21 +01002104 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2107 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002108 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002109#endif
Paul Bakker45829992013-01-03 14:52:21 +01002110
2111 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2112 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002115 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2116 ssl->transform_in->ivlen,
2117 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002119 }
2120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002121 dec_msglen = ssl->in_msglen;
2122 dec_msg = ssl->in_msg;
2123 dec_msg_result = ssl->in_msg;
2124
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002125 /*
2126 * Authenticate before decrypt if enabled
2127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2129 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002130 {
Hanno Becker992b6872017-11-09 18:57:39 +00002131 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002132 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002136 dec_msglen -= ssl->transform_in->maclen;
2137 ssl->in_msglen -= ssl->transform_in->maclen;
2138
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002139 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2140 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2141 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2142 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2147 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002148 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002149 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002154 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002155 ssl->transform_in->maclen );
2156
Hanno Becker992b6872017-11-09 18:57:39 +00002157 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2158 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002163 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002164 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002165 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167
2168 /*
2169 * Check length sanity
2170 */
2171 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002174 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002180 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002185 dec_msglen -= ssl->transform_in->ivlen;
2186 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002187
Paul Bakker48916f92012-09-16 19:57:18 +00002188 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002189 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002190 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002194 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002195 ssl->transform_in->ivlen,
2196 dec_msg, dec_msglen,
2197 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 return( ret );
2201 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002202
Paul Bakkercca5b812013-08-31 17:40:26 +02002203 if( dec_msglen != olen )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002207 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2210 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002211 {
2212 /*
2213 * Save IV in SSL3 and TLS1
2214 */
2215 memcpy( ssl->transform_in->iv_dec,
2216 ssl->transform_in->cipher_ctx_dec.iv,
2217 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
2221 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002222
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002224 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_DEBUG_ALL)
2227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002228 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002229#endif
Paul Bakker45829992013-01-03 14:52:21 +01002230 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002231 correct = 0;
2232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#if defined(MBEDTLS_SSL_PROTO_SSL3)
2235 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
Paul Bakker48916f92012-09-16 19:57:18 +00002237 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_DEBUG_ALL)
2240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002242 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002243#endif
Paul Bakker45829992013-01-03 14:52:21 +01002244 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 }
2246 }
2247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2249#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2251 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 /*
Paul Bakker45829992013-01-03 14:52:21 +01002254 * TLSv1+: always check the padding up to the first failure
2255 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002257 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002258 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002259 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002260
Paul Bakker956c9e02013-12-19 14:42:28 +01002261 /*
2262 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002263 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002264 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002265 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002266 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002267 *
2268 * In both cases we reset padding_idx to a safe value (0) to
2269 * prevent out-of-buffer reads.
2270 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002271 correct &= ( padlen <= ssl->in_msglen );
2272 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002273 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002274
2275 padding_idx *= correct;
2276
Angus Grattonb512bc12018-06-19 15:57:50 +10002277 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002278 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002279 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002280 pad_count += real_count *
2281 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2282 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002283
2284 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002287 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002289#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002290 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2294 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002298 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002299
2300 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002302 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002303#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002309#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 * Authenticate if not done yet.
2316 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002318#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002320 {
Hanno Becker992b6872017-11-09 18:57:39 +00002321 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002322 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002323
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002324 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002326 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2327 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_SSL_PROTO_SSL3)
2330 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002331 {
2332 ssl_mac( &ssl->transform_in->md_ctx_dec,
2333 ssl->transform_in->mac_dec,
2334 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002335 ssl->in_ctr, ssl->in_msgtype,
2336 mac_expect );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002337 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2338 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002339 }
2340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2342#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2343 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2344 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002345 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002346 int ret;
2347 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002348
2349 /*
2350 * The next two sizes are the minimum and maximum values of
2351 * in_msglen over all padlen values.
2352 *
2353 * They're independent of padlen, since we previously did
2354 * in_msglen -= padlen.
2355 *
2356 * Note that max_len + maclen is never more than the buffer
2357 * length, as we previously did in_msglen -= maclen too.
2358 */
2359 const size_t max_len = ssl->in_msglen + padlen;
2360 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2361
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002362 memcpy( add_data + 0, ssl->in_ctr, 8 );
2363 memcpy( add_data + 8, ssl->in_hdr, 3 );
2364 memcpy( add_data + 11, ssl->in_len, 2 );
2365
2366 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2367 add_data, sizeof( add_data ),
2368 ssl->in_msg, ssl->in_msglen,
2369 min_len, max_len,
2370 mac_expect );
2371 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002372 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2374 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02002375 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002376
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002377 /* Make sure we access all the memory that could contain the MAC,
2378 * before we check it in the next code block. This makes the
2379 * synchronisation requirements for just-in-time Prime+Probe
2380 * attacks much tighter and hopefully impractical. */
2381 ssl_read_memory( ssl->in_msg + min_len,
2382 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002383 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2384 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002385 }
2386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2388 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002394#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002395 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002396 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002397#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002399 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002400 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_SSL_DEBUG_ALL)
2403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002404#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002405 correct = 0;
2406 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002407 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002408 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002409
2410 /*
2411 * Finally check the correct flag
2412 */
2413 if( correct == 0 )
2414 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002415#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002416
2417 /* Make extra sure authentication was performed, exactly once */
2418 if( auth_done != 1 )
2419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
2424 if( ssl->in_msglen == 0 )
2425 {
Angus Gratton34817922018-06-19 15:58:22 +10002426#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2427 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2428 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2429 {
2430 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2433 }
2434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2435
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 ssl->nb_zero++;
2437
2438 /*
2439 * Three or more empty messages may be a DoS attack
2440 * (excessive CPU consumption).
2441 */
2442 if( ssl->nb_zero > 3 )
2443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448 }
2449 else
2450 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002454 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002455 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002456 }
2457 else
2458#endif
2459 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002460 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002461 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2462 if( ++ssl->in_ctr[i - 1] != 0 )
2463 break;
2464
2465 /* The loop goes to its end iff the counter is wrapping */
2466 if( i == ssl_ep_len( ssl ) )
2467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2469 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002470 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002471 }
2472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
2475 return( 0 );
2476}
2477
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002478#undef MAC_NONE
2479#undef MAC_PLAINTEXT
2480#undef MAC_CIPHERTEXT
2481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002483/*
2484 * Compression/decompression functions
2485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487{
2488 int ret;
2489 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002490 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002491 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002492 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002495
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002496 if( len_pre == 0 )
2497 return( 0 );
2498
Paul Bakker2770fbd2012-07-03 13:30:23 +00002499 memcpy( msg_pre, ssl->out_msg, len_pre );
2500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002502 ssl->out_msglen ) );
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002505 ssl->out_msg, ssl->out_msglen );
2506
Paul Bakker48916f92012-09-16 19:57:18 +00002507 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2508 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2509 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002510 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002511
Paul Bakker48916f92012-09-16 19:57:18 +00002512 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002513 if( ret != Z_OK )
2514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2516 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002517 }
2518
Angus Grattond8213d02016-05-25 20:56:48 +10002519 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002520 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002523 ssl->out_msglen ) );
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002526 ssl->out_msg, ssl->out_msglen );
2527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002529
2530 return( 0 );
2531}
2532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002534{
2535 int ret;
2536 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002537 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002538 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002539 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002542
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002543 if( len_pre == 0 )
2544 return( 0 );
2545
Paul Bakker2770fbd2012-07-03 13:30:23 +00002546 memcpy( msg_pre, ssl->in_msg, len_pre );
2547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002549 ssl->in_msglen ) );
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002552 ssl->in_msg, ssl->in_msglen );
2553
Paul Bakker48916f92012-09-16 19:57:18 +00002554 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2555 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2556 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002557 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002558 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002559
Paul Bakker48916f92012-09-16 19:57:18 +00002560 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002561 if( ret != Z_OK )
2562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2564 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002565 }
2566
Angus Grattond8213d02016-05-25 20:56:48 +10002567 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002568 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002571 ssl->in_msglen ) );
2572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002574 ssl->in_msg, ssl->in_msglen );
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002577
2578 return( 0 );
2579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2583static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if defined(MBEDTLS_SSL_PROTO_DTLS)
2586static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002587{
2588 /* If renegotiation is not enforced, retransmit until we would reach max
2589 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002590 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002591 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002592 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002593 unsigned char doublings = 1;
2594
2595 while( ratio != 0 )
2596 {
2597 ++doublings;
2598 ratio >>= 1;
2599 }
2600
2601 if( ++ssl->renego_records_seen > doublings )
2602 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002604 return( 0 );
2605 }
2606 }
2607
2608 return( ssl_write_hello_request( ssl ) );
2609}
2610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002612
Paul Bakker5121ce52009-01-03 21:22:43 +00002613/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002614 * Fill the input message buffer by appending data to it.
2615 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002616 *
2617 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2618 * available (from this read and/or a previous one). Otherwise, an error code
2619 * is returned (possibly EOF or WANT_READ).
2620 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002621 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2622 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2623 * since we always read a whole datagram at once.
2624 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002625 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002626 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629{
Paul Bakker23986e52011-04-24 08:57:21 +00002630 int ret;
2631 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002635 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002638 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002640 }
2641
Angus Grattond8213d02016-05-25 20:56:48 +10002642 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002646 }
2647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002651 uint32_t timeout;
2652
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002653 /* Just to be sure */
2654 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2655 {
2656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2657 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2659 }
2660
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002661 /*
2662 * The point is, we need to always read a full datagram at once, so we
2663 * sometimes read more then requested, and handle the additional data.
2664 * It could be the rest of the current record (while fetching the
2665 * header) and/or some other records in the same datagram.
2666 */
2667
2668 /*
2669 * Move to the next record in the already read datagram if applicable
2670 */
2671 if( ssl->next_record_offset != 0 )
2672 {
2673 if( ssl->in_left < ssl->next_record_offset )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002677 }
2678
2679 ssl->in_left -= ssl->next_record_offset;
2680
2681 if( ssl->in_left != 0 )
2682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002684 ssl->next_record_offset ) );
2685 memmove( ssl->in_hdr,
2686 ssl->in_hdr + ssl->next_record_offset,
2687 ssl->in_left );
2688 }
2689
2690 ssl->next_record_offset = 0;
2691 }
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002695
2696 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002697 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698 */
2699 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002702 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002703 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002704
2705 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002706 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002707 * are not at the beginning of a new record, the caller did something
2708 * wrong.
2709 */
2710 if( ssl->in_left != 0 )
2711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002714 }
2715
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002716 /*
2717 * Don't even try to read if time's out already.
2718 * This avoids by-passing the timer when repeatedly receiving messages
2719 * that will end up being dropped.
2720 */
2721 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002722 {
2723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002724 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002725 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002726 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002727 {
Angus Grattond8213d02016-05-25 20:56:48 +10002728 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002731 timeout = ssl->handshake->retransmit_timeout;
2732 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002733 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002736
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002737 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002738 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2739 timeout );
2740 else
2741 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002744
2745 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002747 }
2748
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002749 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002752 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002755 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002756 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002759 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002760 }
2761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002765 return( ret );
2766 }
2767
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002768 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002771 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002773 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002774 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002777 return( ret );
2778 }
2779
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002780 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002783 }
2784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 if( ret < 0 )
2786 return( ret );
2787
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002788 ssl->in_left = ret;
2789 }
2790 else
2791#endif
2792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002794 ssl->in_left, nb_want ) );
2795
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002796 while( ssl->in_left < nb_want )
2797 {
2798 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002799
2800 if( ssl_check_timer( ssl ) != 0 )
2801 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2802 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002803 {
2804 if( ssl->f_recv_timeout != NULL )
2805 {
2806 ret = ssl->f_recv_timeout( ssl->p_bio,
2807 ssl->in_hdr + ssl->in_left, len,
2808 ssl->conf->read_timeout );
2809 }
2810 else
2811 {
2812 ret = ssl->f_recv( ssl->p_bio,
2813 ssl->in_hdr + ssl->in_left, len );
2814 }
2815 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002818 ssl->in_left, nb_want ) );
2819 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002820
2821 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002823
2824 if( ret < 0 )
2825 return( ret );
2826
makise-homura8c3fa632020-08-24 18:33:54 +03002827 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002828 {
Darryl Green11999bb2018-03-13 15:22:58 +00002829 MBEDTLS_SSL_DEBUG_MSG( 1,
2830 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002831 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2833 }
2834
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002835 ssl->in_left += ret;
2836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 }
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 return( 0 );
2842}
2843
2844/*
2845 * Flush any data not yet written
2846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002848{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002849 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002850 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002854 if( ssl->f_send == NULL )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002857 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002859 }
2860
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002861 /* Avoid incrementing counter if data is flushed */
2862 if( ssl->out_left == 0 )
2863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002865 return( 0 );
2866 }
2867
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 while( ssl->out_left > 0 )
2869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2871 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Hanno Becker2b1e3542018-08-06 11:19:13 +01002873 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002874 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
2878 if( ret <= 0 )
2879 return( ret );
2880
makise-homura8c3fa632020-08-24 18:33:54 +03002881 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002882 {
Darryl Green11999bb2018-03-13 15:22:58 +00002883 MBEDTLS_SSL_DEBUG_MSG( 1,
2884 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002885 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002886 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2887 }
2888
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 ssl->out_left -= ret;
2890 }
2891
Hanno Becker2b1e3542018-08-06 11:19:13 +01002892#if defined(MBEDTLS_SSL_PROTO_DTLS)
2893 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002894 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002895 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002896 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002897 else
2898#endif
2899 {
2900 ssl->out_hdr = ssl->out_buf + 8;
2901 }
2902 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
2906 return( 0 );
2907}
2908
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909/*
2910 * Functions to handle the DTLS retransmission state machine
2911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002913/*
2914 * Append current handshake message to current outgoing flight
2915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2920 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2921 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002922
2923 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002924 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002928 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002929 }
2930
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002931 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002932 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002935 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936 }
2937
2938 /* Copy current handshake message with headers */
2939 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2940 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002941 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002942 msg->next = NULL;
2943
2944 /* Append to the current flight */
2945 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002946 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002947 else
2948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002950 while( cur->next != NULL )
2951 cur = cur->next;
2952 cur->next = msg;
2953 }
2954
Hanno Becker3b235902018-08-06 09:54:53 +01002955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002956 return( 0 );
2957}
2958
2959/*
2960 * Free the current flight of handshake messages
2961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 mbedtls_ssl_flight_item *cur = flight;
2965 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002966
2967 while( cur != NULL )
2968 {
2969 next = cur->next;
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 mbedtls_free( cur->p );
2972 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002973
2974 cur = next;
2975 }
2976}
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2979static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002980#endif
2981
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002982/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002983 * Swap transform_out and out_ctr with the alternative ones
2984 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002985static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002988 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002989#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2990 int ret;
2991#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002992
2993 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002996 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002997 }
2998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003000
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003001 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003002 tmp_transform = ssl->transform_out;
3003 ssl->transform_out = ssl->handshake->alt_transform_out;
3004 ssl->handshake->alt_transform_out = tmp_transform;
3005
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003006 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003007 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3008 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003009 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003010
3011 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003012 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3015 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3020 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003021 }
3022 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003023#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3024
3025 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003026}
3027
3028/*
3029 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003030 */
3031int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3032{
3033 int ret = 0;
3034
3035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3036
3037 ret = mbedtls_ssl_flight_transmit( ssl );
3038
3039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3040
3041 return( ret );
3042}
3043
3044/*
3045 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046 *
3047 * Need to remember the current message in case flush_output returns
3048 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003049 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003050 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003051int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003053 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003057 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003059
3060 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003061 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003062 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3063 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003066 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003067
3068 while( ssl->handshake->cur_msg != NULL )
3069 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003070 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003071 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003072
Hanno Beckere1dcb032018-08-17 16:47:58 +01003073 int const is_finished =
3074 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3075 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3076
Hanno Becker04da1892018-08-14 13:22:10 +01003077 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3078 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3079
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003080 /* Swap epochs before sending Finished: we can't do it after
3081 * sending ChangeCipherSpec, in case write returns WANT_READ.
3082 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003083 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003084 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003086 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3087 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003088 }
3089
Hanno Becker67bc7c32018-08-06 11:33:50 +01003090 ret = ssl_get_remaining_payload_in_datagram( ssl );
3091 if( ret < 0 )
3092 return( ret );
3093 max_frag_len = (size_t) ret;
3094
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003095 /* CCS is copied as is, while HS messages may need fragmentation */
3096 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3097 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003098 if( max_frag_len == 0 )
3099 {
3100 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3101 return( ret );
3102
3103 continue;
3104 }
3105
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003106 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003107 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003108 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003109
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003110 /* Update position inside current message */
3111 ssl->handshake->cur_msg_p += cur->len;
3112 }
3113 else
3114 {
3115 const unsigned char * const p = ssl->handshake->cur_msg_p;
3116 const size_t hs_len = cur->len - 12;
3117 const size_t frag_off = p - ( cur->p + 12 );
3118 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003119 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120
Hanno Beckere1dcb032018-08-17 16:47:58 +01003121 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003122 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003123 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003124 {
3125 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3126 return( ret );
3127 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003128
Hanno Becker67bc7c32018-08-06 11:33:50 +01003129 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3130 return( ret );
3131
3132 continue;
3133 }
3134 max_hs_frag_len = max_frag_len - 12;
3135
3136 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3137 max_hs_frag_len : rem_len;
3138
3139 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003140 {
3141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003142 (unsigned) cur_hs_frag_len,
3143 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003144 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003145
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003146 /* Messages are stored with handshake headers as if not fragmented,
3147 * copy beginning of headers then fill fragmentation fields.
3148 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3149 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003150
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003151 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3152 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3153 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3154
Hanno Becker67bc7c32018-08-06 11:33:50 +01003155 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3156 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3157 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003158
3159 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3160
Hanno Becker3f7b9732018-08-28 09:53:25 +01003161 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003162 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3163 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003164 ssl->out_msgtype = cur->type;
3165
3166 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003167 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003168 }
3169
3170 /* If done with the current message move to the next one if any */
3171 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3172 {
3173 if( cur->next != NULL )
3174 {
3175 ssl->handshake->cur_msg = cur->next;
3176 ssl->handshake->cur_msg_p = cur->next->p + 12;
3177 }
3178 else
3179 {
3180 ssl->handshake->cur_msg = NULL;
3181 ssl->handshake->cur_msg_p = NULL;
3182 }
3183 }
3184
3185 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003186 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189 return( ret );
3190 }
3191 }
3192
Hanno Becker67bc7c32018-08-06 11:33:50 +01003193 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3194 return( ret );
3195
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003196 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003199 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003202 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3203 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003204
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206
3207 return( 0 );
3208}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003209
3210/*
3211 * To be called when the last message of an incoming flight is received.
3212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003214{
3215 /* We won't need to resend that one any more */
3216 ssl_flight_free( ssl->handshake->flight );
3217 ssl->handshake->flight = NULL;
3218 ssl->handshake->cur_msg = NULL;
3219
3220 /* The next incoming flight will start with this msg_seq */
3221 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3222
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003223 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003224 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003225
Hanno Becker0271f962018-08-16 13:23:47 +01003226 /* Clear future message buffering structure. */
3227 ssl_buffering_free( ssl );
3228
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003229 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003230 ssl_set_timer( ssl, 0 );
3231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3233 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003236 }
3237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003239}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003240
3241/*
3242 * To be called when the last message of an outgoing flight is send.
3243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003245{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003246 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003247 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3250 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003253 }
3254 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003258
Paul Bakker5121ce52009-01-03 21:22:43 +00003259/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003260 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262
3263/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003264 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003265 *
3266 * - fill in handshake headers
3267 * - update handshake checksum
3268 * - DTLS: save message for resending
3269 * - then pass to the record layer
3270 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003271 * DTLS: except for HelloRequest, messages are only queued, and will only be
3272 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003273 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003274 * Inputs:
3275 * - ssl->out_msglen: 4 + actual handshake message len
3276 * (4 is the size of handshake headers for TLS)
3277 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3278 * - ssl->out_msg + 4: the handshake message body
3279 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003280 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003281 * - ssl->out_msglen: the length of the record contents
3282 * (including handshake headers but excluding record headers)
3283 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003284 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003285int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003286{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003287 int ret;
3288 const size_t hs_len = ssl->out_msglen - 4;
3289 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3292
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003293 /*
3294 * Sanity checks
3295 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003296 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003297 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3298 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003299 /* In SSLv3, the client might send a NoCertificate alert. */
3300#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3301 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3302 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3303 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3304#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3305 {
3306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3308 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003311 /* Whenever we send anything different from a
3312 * HelloRequest we should be in a handshake - double check. */
3313 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3314 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003315 ssl->handshake == NULL )
3316 {
3317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003328 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003329#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003330
Hanno Beckerb50a2532018-08-06 11:52:54 +01003331 /* Double-check that we did not exceed the bounds
3332 * of the outgoing record buffer.
3333 * This should never fail as the various message
3334 * writing functions must obey the bounds of the
3335 * outgoing record buffer, but better be safe.
3336 *
3337 * Note: We deliberately do not check for the MTU or MFL here.
3338 */
3339 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3340 {
3341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3342 "size %u, maximum %u",
3343 (unsigned) ssl->out_msglen,
3344 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3346 }
3347
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003348 /*
3349 * Fill handshake headers
3350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003353 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3354 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3355 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003357 /*
3358 * DTLS has additional fields in the Handshake layer,
3359 * between the length field and the actual payload:
3360 * uint16 message_seq;
3361 * uint24 fragment_offset;
3362 * uint24 fragment_length;
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003366 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003367 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003368 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003369 {
3370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3371 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003372 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003373 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3375 }
3376
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003377 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003378 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003379
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003380 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003381 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003382 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003383 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3384 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3385 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003386 }
3387 else
3388 {
3389 ssl->out_msg[4] = 0;
3390 ssl->out_msg[5] = 0;
3391 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003392
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003393 /* Handshake hashes are computed without fragmentation,
3394 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003395 memset( ssl->out_msg + 6, 0x00, 3 );
3396 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003399
Hanno Becker0207e532018-08-28 10:28:28 +01003400 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003401 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3402 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 }
3404
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003405 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003408 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3409 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003410 {
3411 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414 return( ret );
3415 }
3416 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003417 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003418#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003419 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003420 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003421 {
3422 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3423 return( ret );
3424 }
3425 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003426
3427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3428
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003429 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003430}
3431
3432/*
3433 * Record layer functions
3434 */
3435
3436/*
3437 * Write current record.
3438 *
3439 * Uses:
3440 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3441 * - ssl->out_msglen: length of the record content (excl headers)
3442 * - ssl->out_msg: record content
3443 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003445{
3446 int ret, done = 0;
3447 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003448 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003449
3450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003453 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455 {
3456 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459 return( ret );
3460 }
3461
3462 len = ssl->out_msglen;
3463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3467 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 ret = mbedtls_ssl_hw_record_write( ssl );
3472 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3475 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003476 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003477
3478 if( ret == 0 )
3479 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003482 if( !done )
3483 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003484 unsigned i;
3485 size_t protected_record_size;
3486
Paul Bakker05ef8352012-05-08 09:17:57 +00003487 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003489 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003490
Hanno Becker19859472018-08-06 09:40:20 +01003491 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003492 ssl->out_len[0] = (unsigned char)( len >> 8 );
3493 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003494
Paul Bakker48916f92012-09-16 19:57:18 +00003495 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003496 {
3497 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003500 return( ret );
3501 }
3502
3503 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003504 ssl->out_len[0] = (unsigned char)( len >> 8 );
3505 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003506 }
3507
Hanno Becker2b1e3542018-08-06 11:19:13 +01003508 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3509
3510#if defined(MBEDTLS_SSL_PROTO_DTLS)
3511 /* In case of DTLS, double-check that we don't exceed
3512 * the remaining space in the datagram. */
3513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3514 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003515 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003516 if( ret < 0 )
3517 return( ret );
3518
3519 if( protected_record_size > (size_t) ret )
3520 {
3521 /* Should never happen */
3522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3523 }
3524 }
3525#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003528 "version = [%d:%d], msglen = %d",
3529 ssl->out_hdr[0], ssl->out_hdr[1],
3530 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003533 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003534
3535 ssl->out_left += protected_record_size;
3536 ssl->out_hdr += protected_record_size;
3537 ssl_update_out_pointers( ssl, ssl->transform_out );
3538
Hanno Becker04484622018-08-06 09:49:38 +01003539 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3540 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3541 break;
3542
3543 /* The loop goes to its end iff the counter is wrapping */
3544 if( i == ssl_ep_len( ssl ) )
3545 {
3546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3547 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 }
3550
Hanno Becker67bc7c32018-08-06 11:33:50 +01003551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3553 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003555 size_t remaining;
3556 ret = ssl_get_remaining_payload_in_datagram( ssl );
3557 if( ret < 0 )
3558 {
3559 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3560 ret );
3561 return( ret );
3562 }
3563
3564 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003566 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003567 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003568 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003569 else
3570 {
Hanno Becker513815a2018-08-20 11:56:09 +01003571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003572 }
3573 }
3574#endif /* MBEDTLS_SSL_PROTO_DTLS */
3575
3576 if( ( flush == SSL_FORCE_FLUSH ) &&
3577 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 return( ret );
3581 }
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
3585 return( 0 );
3586}
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003589
3590static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3591{
3592 if( ssl->in_msglen < ssl->in_hslen ||
3593 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3594 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3595 {
3596 return( 1 );
3597 }
3598 return( 0 );
3599}
Hanno Becker44650b72018-08-16 12:51:11 +01003600
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003601static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003602{
3603 return( ( ssl->in_msg[9] << 16 ) |
3604 ( ssl->in_msg[10] << 8 ) |
3605 ssl->in_msg[11] );
3606}
3607
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003608static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003609{
3610 return( ( ssl->in_msg[6] << 16 ) |
3611 ( ssl->in_msg[7] << 8 ) |
3612 ssl->in_msg[8] );
3613}
3614
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003615static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003616{
3617 uint32_t msg_len, frag_off, frag_len;
3618
3619 msg_len = ssl_get_hs_total_len( ssl );
3620 frag_off = ssl_get_hs_frag_off( ssl );
3621 frag_len = ssl_get_hs_frag_len( ssl );
3622
3623 if( frag_off > msg_len )
3624 return( -1 );
3625
3626 if( frag_len > msg_len - frag_off )
3627 return( -1 );
3628
3629 if( frag_len + 12 > ssl->in_msglen )
3630 return( -1 );
3631
3632 return( 0 );
3633}
3634
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003635/*
3636 * Mark bits in bitmask (used for DTLS HS reassembly)
3637 */
3638static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3639{
3640 unsigned int start_bits, end_bits;
3641
3642 start_bits = 8 - ( offset % 8 );
3643 if( start_bits != 8 )
3644 {
3645 size_t first_byte_idx = offset / 8;
3646
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003647 /* Special case */
3648 if( len <= start_bits )
3649 {
3650 for( ; len != 0; len-- )
3651 mask[first_byte_idx] |= 1 << ( start_bits - len );
3652
3653 /* Avoid potential issues with offset or len becoming invalid */
3654 return;
3655 }
3656
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003657 offset += start_bits; /* Now offset % 8 == 0 */
3658 len -= start_bits;
3659
3660 for( ; start_bits != 0; start_bits-- )
3661 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3662 }
3663
3664 end_bits = len % 8;
3665 if( end_bits != 0 )
3666 {
3667 size_t last_byte_idx = ( offset + len ) / 8;
3668
3669 len -= end_bits; /* Now len % 8 == 0 */
3670
3671 for( ; end_bits != 0; end_bits-- )
3672 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3673 }
3674
3675 memset( mask + offset / 8, 0xFF, len / 8 );
3676}
3677
3678/*
3679 * Check that bitmask is full
3680 */
3681static int ssl_bitmask_check( unsigned char *mask, size_t len )
3682{
3683 size_t i;
3684
3685 for( i = 0; i < len / 8; i++ )
3686 if( mask[i] != 0xFF )
3687 return( -1 );
3688
3689 for( i = 0; i < len % 8; i++ )
3690 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3691 return( -1 );
3692
3693 return( 0 );
3694}
3695
Hanno Becker56e205e2018-08-16 09:06:12 +01003696/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003697static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003698 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003699{
Hanno Becker56e205e2018-08-16 09:06:12 +01003700 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003701
Hanno Becker56e205e2018-08-16 09:06:12 +01003702 alloc_len = 12; /* Handshake header */
3703 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003704
Hanno Beckerd07df862018-08-16 09:14:58 +01003705 if( add_bitmap )
3706 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003707
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003708 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003709}
Hanno Becker56e205e2018-08-16 09:06:12 +01003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003712
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003713static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003714{
3715 return( ( ssl->in_msg[1] << 16 ) |
3716 ( ssl->in_msg[2] << 8 ) |
3717 ssl->in_msg[3] );
3718}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003719
Simon Butcher99000142016-10-13 17:21:01 +01003720int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003725 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003727 }
3728
Hanno Becker12555c62018-08-16 12:47:53 +01003729 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003732 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003737 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003738 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003739 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003740
Hanno Becker44650b72018-08-16 12:51:11 +01003741 if( ssl_check_hs_header( ssl ) != 0 )
3742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3744 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3745 }
3746
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003747 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003748 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3749 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3750 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3751 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003752 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003753 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3754 {
3755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3756 recv_msg_seq,
3757 ssl->handshake->in_msg_seq ) );
3758 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3759 }
3760
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003761 /* Retransmit only on last message from previous flight, to avoid
3762 * too many retransmissions.
3763 * Besides, No sane server ever retransmits HelloVerifyRequest */
3764 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003768 "message_seq = %d, start_of_flight = %d",
3769 recv_msg_seq,
3770 ssl->handshake->in_flight_start_seq ) );
3771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003775 return( ret );
3776 }
3777 }
3778 else
3779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003781 "message_seq = %d, expected = %d",
3782 recv_msg_seq,
3783 ssl->handshake->in_msg_seq ) );
3784 }
3785
Hanno Becker90333da2017-10-10 11:27:13 +01003786 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003787 }
3788 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003789
Hanno Becker6d97ef52018-08-16 13:09:04 +01003790 /* Message reassembly is handled alongside buffering of future
3791 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003792 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003793 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003794 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003797 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003798 }
3799 }
3800 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003802 /* With TLS we don't handle fragmentation (for now) */
3803 if( ssl->in_msglen < ssl->in_hslen )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3806 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003807 }
3808
Simon Butcher99000142016-10-13 17:21:01 +01003809 return( 0 );
3810}
3811
3812void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3813{
Hanno Becker0271f962018-08-16 13:23:47 +01003814 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003815
Hanno Becker0271f962018-08-16 13:23:47 +01003816 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003817 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003818 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003819 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003820
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003821 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003823 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003824 ssl->handshake != NULL )
3825 {
Hanno Becker0271f962018-08-16 13:23:47 +01003826 unsigned offset;
3827 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003828
Hanno Becker0271f962018-08-16 13:23:47 +01003829 /* Increment handshake sequence number */
3830 hs->in_msg_seq++;
3831
3832 /*
3833 * Clear up handshake buffering and reassembly structure.
3834 */
3835
3836 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003837 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003838
3839 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003840 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3841 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003842 offset++, hs_buf++ )
3843 {
3844 *hs_buf = *(hs_buf + 1);
3845 }
3846
3847 /* Create a fresh last entry */
3848 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003849 }
3850#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003851}
3852
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003853/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003854 * DTLS anti-replay: RFC 6347 4.1.2.6
3855 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003856 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3857 * Bit n is set iff record number in_window_top - n has been seen.
3858 *
3859 * Usually, in_window_top is the last record number seen and the lsb of
3860 * in_window is set. The only exception is the initial state (record number 0
3861 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3864static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003865{
3866 ssl->in_window_top = 0;
3867 ssl->in_window = 0;
3868}
3869
3870static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3871{
3872 return( ( (uint64_t) buf[0] << 40 ) |
3873 ( (uint64_t) buf[1] << 32 ) |
3874 ( (uint64_t) buf[2] << 24 ) |
3875 ( (uint64_t) buf[3] << 16 ) |
3876 ( (uint64_t) buf[4] << 8 ) |
3877 ( (uint64_t) buf[5] ) );
3878}
3879
3880/*
3881 * Return 0 if sequence number is acceptable, -1 otherwise
3882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003884{
3885 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3886 uint64_t bit;
3887
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003888 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003889 return( 0 );
3890
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003891 if( rec_seqnum > ssl->in_window_top )
3892 return( 0 );
3893
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003894 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003895
3896 if( bit >= 64 )
3897 return( -1 );
3898
3899 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3900 return( -1 );
3901
3902 return( 0 );
3903}
3904
3905/*
3906 * Update replay window on new validated record
3907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003909{
3910 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3911
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003912 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003913 return;
3914
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003915 if( rec_seqnum > ssl->in_window_top )
3916 {
3917 /* Update window_top and the contents of the window */
3918 uint64_t shift = rec_seqnum - ssl->in_window_top;
3919
3920 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003921 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003922 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003923 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003924 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003925 ssl->in_window |= 1;
3926 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003927
3928 ssl->in_window_top = rec_seqnum;
3929 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003930 else
3931 {
3932 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003933 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003934
3935 if( bit < 64 ) /* Always true, but be extra sure */
3936 ssl->in_window |= (uint64_t) 1 << bit;
3937 }
3938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003940
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003941#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003942/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003943static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3944
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003946 * Without any SSL context, check if a datagram looks like a ClientHello with
3947 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003948 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003949 *
3950 * - if cookie is valid, return 0
3951 * - if ClientHello looks superficially valid but cookie is not,
3952 * fill obuf and set olen, then
3953 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3954 * - otherwise return a specific error code
3955 */
3956static int ssl_check_dtls_clihlo_cookie(
3957 mbedtls_ssl_cookie_write_t *f_cookie_write,
3958 mbedtls_ssl_cookie_check_t *f_cookie_check,
3959 void *p_cookie,
3960 const unsigned char *cli_id, size_t cli_id_len,
3961 const unsigned char *in, size_t in_len,
3962 unsigned char *obuf, size_t buf_len, size_t *olen )
3963{
3964 size_t sid_len, cookie_len;
3965 unsigned char *p;
3966
3967 if( f_cookie_write == NULL || f_cookie_check == NULL )
3968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3969
3970 /*
3971 * Structure of ClientHello with record and handshake headers,
3972 * and expected values. We don't need to check a lot, more checks will be
3973 * done when actually parsing the ClientHello - skipping those checks
3974 * avoids code duplication and does not make cookie forging any easier.
3975 *
3976 * 0-0 ContentType type; copied, must be handshake
3977 * 1-2 ProtocolVersion version; copied
3978 * 3-4 uint16 epoch; copied, must be 0
3979 * 5-10 uint48 sequence_number; copied
3980 * 11-12 uint16 length; (ignored)
3981 *
3982 * 13-13 HandshakeType msg_type; (ignored)
3983 * 14-16 uint24 length; (ignored)
3984 * 17-18 uint16 message_seq; copied
3985 * 19-21 uint24 fragment_offset; copied, must be 0
3986 * 22-24 uint24 fragment_length; (ignored)
3987 *
3988 * 25-26 ProtocolVersion client_version; (ignored)
3989 * 27-58 Random random; (ignored)
3990 * 59-xx SessionID session_id; 1 byte len + sid_len content
3991 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3992 * ...
3993 *
3994 * Minimum length is 61 bytes.
3995 */
3996 if( in_len < 61 ||
3997 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3998 in[3] != 0 || in[4] != 0 ||
3999 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4000 {
4001 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4002 }
4003
4004 sid_len = in[59];
4005 if( sid_len > in_len - 61 )
4006 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4007
4008 cookie_len = in[60 + sid_len];
4009 if( cookie_len > in_len - 60 )
4010 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4011
4012 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4013 cli_id, cli_id_len ) == 0 )
4014 {
4015 /* Valid cookie */
4016 return( 0 );
4017 }
4018
4019 /*
4020 * If we get here, we've got an invalid cookie, let's prepare HVR.
4021 *
4022 * 0-0 ContentType type; copied
4023 * 1-2 ProtocolVersion version; copied
4024 * 3-4 uint16 epoch; copied
4025 * 5-10 uint48 sequence_number; copied
4026 * 11-12 uint16 length; olen - 13
4027 *
4028 * 13-13 HandshakeType msg_type; hello_verify_request
4029 * 14-16 uint24 length; olen - 25
4030 * 17-18 uint16 message_seq; copied
4031 * 19-21 uint24 fragment_offset; copied
4032 * 22-24 uint24 fragment_length; olen - 25
4033 *
4034 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4035 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4036 *
4037 * Minimum length is 28.
4038 */
4039 if( buf_len < 28 )
4040 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4041
4042 /* Copy most fields and adapt others */
4043 memcpy( obuf, in, 25 );
4044 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4045 obuf[25] = 0xfe;
4046 obuf[26] = 0xff;
4047
4048 /* Generate and write actual cookie */
4049 p = obuf + 28;
4050 if( f_cookie_write( p_cookie,
4051 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4052 {
4053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4054 }
4055
4056 *olen = p - obuf;
4057
4058 /* Go back and fill length fields */
4059 obuf[27] = (unsigned char)( *olen - 28 );
4060
4061 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4062 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4063 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4064
4065 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4066 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4067
4068 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4069}
4070
4071/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004072 * Handle possible client reconnect with the same UDP quadruplet
4073 * (RFC 6347 Section 4.2.8).
4074 *
4075 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4076 * that looks like a ClientHello.
4077 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004078 * - if the input looks like a ClientHello without cookies,
4079 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004080 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004081 * - if the input looks like a ClientHello with a valid cookie,
4082 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004083 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004084 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004085 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004086 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004087 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4088 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004089 */
4090static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4091{
4092 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004093 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004094
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004095 ret = ssl_check_dtls_clihlo_cookie(
4096 ssl->conf->f_cookie_write,
4097 ssl->conf->f_cookie_check,
4098 ssl->conf->p_cookie,
4099 ssl->cli_id, ssl->cli_id_len,
4100 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004101 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004102
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004103 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4104
4105 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004106 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004107 int send_ret;
4108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4109 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4110 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004111 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004112 * If the error is permanent we'll catch it later,
4113 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004114 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4115 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4116 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004117
4118 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004119 }
4120
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004121 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004122 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004124 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4125 {
4126 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4127 return( ret );
4128 }
4129
4130 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004131 }
4132
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004133 return( ret );
4134}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004135#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004136
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004137/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004138 * ContentType type;
4139 * ProtocolVersion version;
4140 * uint16 epoch; // DTLS only
4141 * uint48 sequence_number; // DTLS only
4142 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004143 *
4144 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004145 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004146 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4147 *
4148 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004149 * 1. proceed with the record if this function returns 0
4150 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4151 * 3. return CLIENT_RECONNECT if this function return that value
4152 * 4. drop the whole datagram if this function returns anything else.
4153 * Point 2 is needed when the peer is resending, and we have already received
4154 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004157{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004158 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 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 +02004161
Paul Bakker5121ce52009-01-03 21:22:43 +00004162 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004163 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004164 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004167 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004168 ssl->in_msgtype,
4169 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004171 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4173 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4174 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4175 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004178
4179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004180 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4181 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004182 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4183#endif /* MBEDTLS_SSL_PROTO_DTLS */
4184 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4185 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004188 }
4189
4190 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004191 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4194 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 }
4196
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004197 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 }
4202
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004203 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004204 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004205 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4208 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004209 }
4210
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004211 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004212 * DTLS-related tests.
4213 * Check epoch before checking length constraint because
4214 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4215 * message gets duplicated before the corresponding Finished message,
4216 * the second ChangeCipherSpec should be discarded because it belongs
4217 * to an old epoch, but not because its length is shorter than
4218 * the minimum record length for packets using the new record transform.
4219 * Note that these two kinds of failures are handled differently,
4220 * as an unexpected record is silently skipped but an invalid
4221 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004222 */
4223#if defined(MBEDTLS_SSL_PROTO_DTLS)
4224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4225 {
4226 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4227
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004228 /* Check epoch (and sequence number) with DTLS */
4229 if( rec_epoch != ssl->in_epoch )
4230 {
4231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4232 "expected %d, received %d",
4233 ssl->in_epoch, rec_epoch ) );
4234
4235#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4236 /*
4237 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4238 * access the first byte of record content (handshake type), as we
4239 * have an active transform (possibly iv_len != 0), so use the
4240 * fact that the record header len is 13 instead.
4241 */
4242 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4243 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4244 rec_epoch == 0 &&
4245 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4246 ssl->in_left > 13 &&
4247 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4248 {
4249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4250 "from the same port" ) );
4251 return( ssl_handle_possible_reconnect( ssl ) );
4252 }
4253 else
4254#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004255 {
4256 /* Consider buffering the record. */
4257 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4258 {
4259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4260 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4261 }
4262
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004263 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004264 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004265 }
4266
4267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4268 /* Replay detection only works for the current epoch */
4269 if( rec_epoch == ssl->in_epoch &&
4270 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4273 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4274 }
4275#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004276
Hanno Becker52c6dc62017-05-26 16:07:36 +01004277 /* Drop unexpected ApplicationData records,
4278 * except at the beginning of renegotiations */
4279 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4280 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4281#if defined(MBEDTLS_SSL_RENEGOTIATION)
4282 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4283 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4284#endif
4285 )
4286 {
4287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4288 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4289 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004290 }
4291#endif /* MBEDTLS_SSL_PROTO_DTLS */
4292
Hanno Becker52c6dc62017-05-26 16:07:36 +01004293
4294 /* Check length against bounds of the current transform and version */
4295 if( ssl->transform_in == NULL )
4296 {
4297 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004298 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004299 {
4300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4301 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4302 }
4303 }
4304 else
4305 {
4306 if( ssl->in_msglen < ssl->transform_in->minlen )
4307 {
4308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4309 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4310 }
4311
4312#if defined(MBEDTLS_SSL_PROTO_SSL3)
4313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004314 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004315 {
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4317 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4318 }
4319#endif
4320#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4321 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4322 /*
4323 * TLS encrypted messages can have up to 256 bytes of padding
4324 */
4325 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4326 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004327 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004328 {
4329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4330 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4331 }
4332#endif
4333 }
4334
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004335 return( 0 );
4336}
Paul Bakker5121ce52009-01-03 21:22:43 +00004337
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004338/*
4339 * If applicable, decrypt (and decompress) record content
4340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004342{
4343 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4346 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4349 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 ret = mbedtls_ssl_hw_record_read( ssl );
4354 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4357 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004358 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004359
4360 if( ret == 0 )
4361 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004364 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
4366 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 return( ret );
4370 }
4371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 ssl->in_msg, ssl->in_msglen );
4374
Angus Grattond8213d02016-05-25 20:56:48 +10004375 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 }
4380 }
4381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004383 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004385 {
4386 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004389 return( ret );
4390 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004391 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004392#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004398 }
4399#endif
4400
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004401 return( 0 );
4402}
4403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004405
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004406/*
4407 * Read a record.
4408 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004409 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4410 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4411 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004412 */
Hanno Becker1097b342018-08-15 14:09:41 +01004413
4414/* Helper functions for mbedtls_ssl_read_record(). */
4415static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004416static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4417static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004418
Hanno Becker327c93b2018-08-15 13:56:18 +01004419int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004420 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421{
4422 int ret;
4423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004425
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004426 if( ssl->keep_current_message == 0 )
4427 {
4428 do {
Simon Butcher99000142016-10-13 17:21:01 +01004429
Hanno Becker26994592018-08-15 14:14:59 +01004430 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004431 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004432 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004433
Hanno Beckere74d5562018-08-15 14:26:08 +01004434 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004435 {
Hanno Becker40f50842018-08-15 14:48:01 +01004436#if defined(MBEDTLS_SSL_PROTO_DTLS)
4437 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004438
Hanno Becker40f50842018-08-15 14:48:01 +01004439 /* We only check for buffered messages if the
4440 * current datagram is fully consumed. */
4441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004442 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004443 {
Hanno Becker40f50842018-08-15 14:48:01 +01004444 if( ssl_load_buffered_message( ssl ) == 0 )
4445 have_buffered = 1;
4446 }
4447
4448 if( have_buffered == 0 )
4449#endif /* MBEDTLS_SSL_PROTO_DTLS */
4450 {
4451 ret = ssl_get_next_record( ssl );
4452 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4453 continue;
4454
4455 if( ret != 0 )
4456 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004457 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004458 return( ret );
4459 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004460 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004461 }
4462
4463 ret = mbedtls_ssl_handle_message_type( ssl );
4464
Hanno Becker40f50842018-08-15 14:48:01 +01004465#if defined(MBEDTLS_SSL_PROTO_DTLS)
4466 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4467 {
4468 /* Buffer future message */
4469 ret = ssl_buffer_message( ssl );
4470 if( ret != 0 )
4471 return( ret );
4472
4473 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4474 }
4475#endif /* MBEDTLS_SSL_PROTO_DTLS */
4476
Hanno Becker90333da2017-10-10 11:27:13 +01004477 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4478 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004479
4480 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004481 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004482 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004483 return( ret );
4484 }
4485
Hanno Becker327c93b2018-08-15 13:56:18 +01004486 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004487 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004488 {
4489 mbedtls_ssl_update_handshake_status( ssl );
4490 }
Simon Butcher99000142016-10-13 17:21:01 +01004491 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004492 else
Simon Butcher99000142016-10-13 17:21:01 +01004493 {
Hanno Becker02f59072018-08-15 14:00:24 +01004494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004495 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004496 }
4497
4498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4499
4500 return( 0 );
4501}
4502
Hanno Becker40f50842018-08-15 14:48:01 +01004503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004504static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004505{
Hanno Becker40f50842018-08-15 14:48:01 +01004506 if( ssl->in_left > ssl->next_record_offset )
4507 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004508
Hanno Becker40f50842018-08-15 14:48:01 +01004509 return( 0 );
4510}
4511
4512static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4513{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004514 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004515 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004516 int ret = 0;
4517
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004518 if( hs == NULL )
4519 return( -1 );
4520
Hanno Beckere00ae372018-08-20 09:39:42 +01004521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4522
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004523 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4524 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4525 {
4526 /* Check if we have seen a ChangeCipherSpec before.
4527 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004528 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004529 {
4530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4531 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004532 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004533 }
4534
Hanno Becker39b8bc92018-08-28 17:17:13 +01004535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004536 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4537 ssl->in_msglen = 1;
4538 ssl->in_msg[0] = 1;
4539
4540 /* As long as they are equal, the exact value doesn't matter. */
4541 ssl->in_left = 0;
4542 ssl->next_record_offset = 0;
4543
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004544 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004545 goto exit;
4546 }
Hanno Becker37f95322018-08-16 13:55:32 +01004547
Hanno Beckerb8f50142018-08-28 10:01:34 +01004548#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004549 /* Debug only */
4550 {
4551 unsigned offset;
4552 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4553 {
4554 hs_buf = &hs->buffering.hs[offset];
4555 if( hs_buf->is_valid == 1 )
4556 {
4557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4558 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004559 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004560 }
4561 }
4562 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004563#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004564
4565 /* Check if we have buffered and/or fully reassembled the
4566 * next handshake message. */
4567 hs_buf = &hs->buffering.hs[0];
4568 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4569 {
4570 /* Synthesize a record containing the buffered HS message. */
4571 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4572 ( hs_buf->data[2] << 8 ) |
4573 hs_buf->data[3];
4574
4575 /* Double-check that we haven't accidentally buffered
4576 * a message that doesn't fit into the input buffer. */
4577 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4578 {
4579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4580 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4581 }
4582
4583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4584 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4585 hs_buf->data, msg_len + 12 );
4586
4587 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4588 ssl->in_hslen = msg_len + 12;
4589 ssl->in_msglen = msg_len + 12;
4590 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4591
4592 ret = 0;
4593 goto exit;
4594 }
4595 else
4596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4598 hs->in_msg_seq ) );
4599 }
4600
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004601 ret = -1;
4602
4603exit:
4604
4605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4606 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004607}
4608
Hanno Beckera02b0b42018-08-21 17:20:27 +01004609static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4610 size_t desired )
4611{
4612 int offset;
4613 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4615 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004616
Hanno Becker01315ea2018-08-21 17:22:17 +01004617 /* Get rid of future records epoch first, if such exist. */
4618 ssl_free_buffered_record( ssl );
4619
4620 /* Check if we have enough space available now. */
4621 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4622 hs->buffering.total_bytes_buffered ) )
4623 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004625 return( 0 );
4626 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004627
Hanno Becker4f432ad2018-08-28 10:02:32 +01004628 /* We don't have enough space to buffer the next expected handshake
4629 * message. Remove buffers used for future messages to gain space,
4630 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004631 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4632 offset >= 0; offset-- )
4633 {
4634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4635 offset ) );
4636
Hanno Beckerb309b922018-08-23 13:18:05 +01004637 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004638
4639 /* Check if we have enough space available now. */
4640 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4641 hs->buffering.total_bytes_buffered ) )
4642 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004644 return( 0 );
4645 }
4646 }
4647
4648 return( -1 );
4649}
4650
Hanno Becker40f50842018-08-15 14:48:01 +01004651static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4652{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004653 int ret = 0;
4654 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4655
4656 if( hs == NULL )
4657 return( 0 );
4658
4659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4660
4661 switch( ssl->in_msgtype )
4662 {
4663 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004665
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004666 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004667 break;
4668
4669 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004670 {
4671 unsigned recv_msg_seq_offset;
4672 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4673 mbedtls_ssl_hs_buffer *hs_buf;
4674 size_t msg_len = ssl->in_hslen - 12;
4675
4676 /* We should never receive an old handshake
4677 * message - double-check nonetheless. */
4678 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4679 {
4680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4682 }
4683
4684 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4685 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4686 {
4687 /* Silently ignore -- message too far in the future */
4688 MBEDTLS_SSL_DEBUG_MSG( 2,
4689 ( "Ignore future HS message with sequence number %u, "
4690 "buffering window %u - %u",
4691 recv_msg_seq, ssl->handshake->in_msg_seq,
4692 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4693
4694 goto exit;
4695 }
4696
4697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4698 recv_msg_seq, recv_msg_seq_offset ) );
4699
4700 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4701
4702 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004703 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004704 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004705 size_t reassembly_buf_sz;
4706
Hanno Becker37f95322018-08-16 13:55:32 +01004707 hs_buf->is_fragmented =
4708 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4709
4710 /* We copy the message back into the input buffer
4711 * after reassembly, so check that it's not too large.
4712 * This is an implementation-specific limitation
4713 * and not one from the standard, hence it is not
4714 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004715 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004716 {
4717 /* Ignore message */
4718 goto exit;
4719 }
4720
Hanno Beckere0b150f2018-08-21 15:51:03 +01004721 /* Check if we have enough space to buffer the message. */
4722 if( hs->buffering.total_bytes_buffered >
4723 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4724 {
4725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4727 }
4728
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004729 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4730 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004731
4732 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4733 hs->buffering.total_bytes_buffered ) )
4734 {
4735 if( recv_msg_seq_offset > 0 )
4736 {
4737 /* If we can't buffer a future message because
4738 * of space limitations -- ignore. */
4739 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",
4740 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4741 (unsigned) hs->buffering.total_bytes_buffered ) );
4742 goto exit;
4743 }
Hanno Beckere1801392018-08-21 16:51:05 +01004744 else
4745 {
4746 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",
4747 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4748 (unsigned) hs->buffering.total_bytes_buffered ) );
4749 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004750
Hanno Beckera02b0b42018-08-21 17:20:27 +01004751 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004752 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004753 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",
4754 (unsigned) msg_len,
4755 (unsigned) reassembly_buf_sz,
4756 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004757 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004758 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4759 goto exit;
4760 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004761 }
4762
4763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4764 msg_len ) );
4765
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004766 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4767 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004768 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004769 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004770 goto exit;
4771 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004772 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004773
4774 /* Prepare final header: copy msg_type, length and message_seq,
4775 * then add standardised fragment_offset and fragment_length */
4776 memcpy( hs_buf->data, ssl->in_msg, 6 );
4777 memset( hs_buf->data + 6, 0, 3 );
4778 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4779
4780 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004781
4782 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004783 }
4784 else
4785 {
4786 /* Make sure msg_type and length are consistent */
4787 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4790 /* Ignore */
4791 goto exit;
4792 }
4793 }
4794
Hanno Becker4422bbb2018-08-20 09:40:19 +01004795 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004796 {
4797 size_t frag_len, frag_off;
4798 unsigned char * const msg = hs_buf->data + 12;
4799
4800 /*
4801 * Check and copy current fragment
4802 */
4803
4804 /* Validation of header fields already done in
4805 * mbedtls_ssl_prepare_handshake_record(). */
4806 frag_off = ssl_get_hs_frag_off( ssl );
4807 frag_len = ssl_get_hs_frag_len( ssl );
4808
4809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4810 frag_off, frag_len ) );
4811 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4812
4813 if( hs_buf->is_fragmented )
4814 {
4815 unsigned char * const bitmask = msg + msg_len;
4816 ssl_bitmask_set( bitmask, frag_off, frag_len );
4817 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4818 msg_len ) == 0 );
4819 }
4820 else
4821 {
4822 hs_buf->is_complete = 1;
4823 }
4824
4825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4826 hs_buf->is_complete ? "" : "not yet " ) );
4827 }
4828
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004829 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004830 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004831
4832 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004833 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004834 break;
4835 }
4836
4837exit:
4838
4839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4840 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004841}
4842#endif /* MBEDTLS_SSL_PROTO_DTLS */
4843
Hanno Becker1097b342018-08-15 14:09:41 +01004844static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004845{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004846 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004847 * Consume last content-layer message and potentially
4848 * update in_msglen which keeps track of the contents'
4849 * consumption state.
4850 *
4851 * (1) Handshake messages:
4852 * Remove last handshake message, move content
4853 * and adapt in_msglen.
4854 *
4855 * (2) Alert messages:
4856 * Consume whole record content, in_msglen = 0.
4857 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004858 * (3) Change cipher spec:
4859 * Consume whole record content, in_msglen = 0.
4860 *
4861 * (4) Application data:
4862 * Don't do anything - the record layer provides
4863 * the application data as a stream transport
4864 * and consumes through mbedtls_ssl_read only.
4865 *
4866 */
4867
4868 /* Case (1): Handshake messages */
4869 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004870 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004871 /* Hard assertion to be sure that no application data
4872 * is in flight, as corrupting ssl->in_msglen during
4873 * ssl->in_offt != NULL is fatal. */
4874 if( ssl->in_offt != NULL )
4875 {
4876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4878 }
4879
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004880 /*
4881 * Get next Handshake message in the current record
4882 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004883
Hanno Becker4a810fb2017-05-24 16:27:30 +01004884 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004885 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004886 * current handshake content: If DTLS handshake
4887 * fragmentation is used, that's the fragment
4888 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004889 * size here is faulty and should be changed at
4890 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004891 * (2) While it doesn't seem to cause problems, one
4892 * has to be very careful not to assume that in_hslen
4893 * is always <= in_msglen in a sensible communication.
4894 * Again, it's wrong for DTLS handshake fragmentation.
4895 * The following check is therefore mandatory, and
4896 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004897 * Additionally, ssl->in_hslen might be arbitrarily out of
4898 * bounds after handling a DTLS message with an unexpected
4899 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004900 */
4901 if( ssl->in_hslen < ssl->in_msglen )
4902 {
4903 ssl->in_msglen -= ssl->in_hslen;
4904 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4905 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004906
Hanno Becker4a810fb2017-05-24 16:27:30 +01004907 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4908 ssl->in_msg, ssl->in_msglen );
4909 }
4910 else
4911 {
4912 ssl->in_msglen = 0;
4913 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004914
Hanno Becker4a810fb2017-05-24 16:27:30 +01004915 ssl->in_hslen = 0;
4916 }
4917 /* Case (4): Application data */
4918 else if( ssl->in_offt != NULL )
4919 {
4920 return( 0 );
4921 }
4922 /* Everything else (CCS & Alerts) */
4923 else
4924 {
4925 ssl->in_msglen = 0;
4926 }
4927
Hanno Becker1097b342018-08-15 14:09:41 +01004928 return( 0 );
4929}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004930
Hanno Beckere74d5562018-08-15 14:26:08 +01004931static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4932{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004933 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004934 return( 1 );
4935
4936 return( 0 );
4937}
4938
Hanno Becker5f066e72018-08-16 14:56:31 +01004939#if defined(MBEDTLS_SSL_PROTO_DTLS)
4940
4941static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4942{
4943 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4944 if( hs == NULL )
4945 return;
4946
Hanno Becker01315ea2018-08-21 17:22:17 +01004947 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004948 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004949 hs->buffering.total_bytes_buffered -=
4950 hs->buffering.future_record.len;
4951
4952 mbedtls_free( hs->buffering.future_record.data );
4953 hs->buffering.future_record.data = NULL;
4954 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004955}
4956
4957static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4958{
4959 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4960 unsigned char * rec;
4961 size_t rec_len;
4962 unsigned rec_epoch;
4963
4964 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4965 return( 0 );
4966
4967 if( hs == NULL )
4968 return( 0 );
4969
Hanno Becker5f066e72018-08-16 14:56:31 +01004970 rec = hs->buffering.future_record.data;
4971 rec_len = hs->buffering.future_record.len;
4972 rec_epoch = hs->buffering.future_record.epoch;
4973
4974 if( rec == NULL )
4975 return( 0 );
4976
Hanno Becker4cb782d2018-08-20 11:19:05 +01004977 /* Only consider loading future records if the
4978 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004979 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004980 return( 0 );
4981
Hanno Becker5f066e72018-08-16 14:56:31 +01004982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4983
4984 if( rec_epoch != ssl->in_epoch )
4985 {
4986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4987 goto exit;
4988 }
4989
4990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4991
4992 /* Double-check that the record is not too large */
4993 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4994 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4995 {
4996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4998 }
4999
5000 memcpy( ssl->in_hdr, rec, rec_len );
5001 ssl->in_left = rec_len;
5002 ssl->next_record_offset = 0;
5003
5004 ssl_free_buffered_record( ssl );
5005
5006exit:
5007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5008 return( 0 );
5009}
5010
5011static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5012{
5013 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5014 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005015 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005016
5017 /* Don't buffer future records outside handshakes. */
5018 if( hs == NULL )
5019 return( 0 );
5020
5021 /* Only buffer handshake records (we are only interested
5022 * in Finished messages). */
5023 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5024 return( 0 );
5025
5026 /* Don't buffer more than one future epoch record. */
5027 if( hs->buffering.future_record.data != NULL )
5028 return( 0 );
5029
Hanno Becker01315ea2018-08-21 17:22:17 +01005030 /* Don't buffer record if there's not enough buffering space remaining. */
5031 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5032 hs->buffering.total_bytes_buffered ) )
5033 {
5034 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",
5035 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5036 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037 return( 0 );
5038 }
5039
Hanno Becker5f066e72018-08-16 14:56:31 +01005040 /* Buffer record */
5041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5042 ssl->in_epoch + 1 ) );
5043 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5044 rec_hdr_len + ssl->in_msglen );
5045
5046 /* ssl_parse_record_header() only considers records
5047 * of the next epoch as candidates for buffering. */
5048 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005049 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005050
5051 hs->buffering.future_record.data =
5052 mbedtls_calloc( 1, hs->buffering.future_record.len );
5053 if( hs->buffering.future_record.data == NULL )
5054 {
5055 /* If we run out of RAM trying to buffer a
5056 * record from the next epoch, just ignore. */
5057 return( 0 );
5058 }
5059
Hanno Becker01315ea2018-08-21 17:22:17 +01005060 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005061
Hanno Becker01315ea2018-08-21 17:22:17 +01005062 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005063 return( 0 );
5064}
5065
5066#endif /* MBEDTLS_SSL_PROTO_DTLS */
5067
Hanno Beckere74d5562018-08-15 14:26:08 +01005068static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005069{
5070 int ret;
5071
Hanno Becker5f066e72018-08-16 14:56:31 +01005072#if defined(MBEDTLS_SSL_PROTO_DTLS)
5073 /* We might have buffered a future record; if so,
5074 * and if the epoch matches now, load it.
5075 * On success, this call will set ssl->in_left to
5076 * the length of the buffered record, so that
5077 * the calls to ssl_fetch_input() below will
5078 * essentially be no-ops. */
5079 ret = ssl_load_buffered_record( ssl );
5080 if( ret != 0 )
5081 return( ret );
5082#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005087 return( ret );
5088 }
5089
5090 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5094 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005095 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005096 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5097 {
5098 ret = ssl_buffer_future_record( ssl );
5099 if( ret != 0 )
5100 return( ret );
5101
5102 /* Fall through to handling of unexpected records */
5103 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5104 }
5105
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005106 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5107 {
5108 /* Skip unexpected record (but not whole datagram) */
5109 ssl->next_record_offset = ssl->in_msglen
5110 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005111
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5113 "(header)" ) );
5114 }
5115 else
5116 {
5117 /* Skip invalid record and the rest of the datagram */
5118 ssl->next_record_offset = 0;
5119 ssl->in_left = 0;
5120
5121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5122 "(header)" ) );
5123 }
5124
5125 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005126 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005127 }
5128#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005129 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005130 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005131
5132 /*
5133 * Read and optionally decrypt the message contents
5134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5136 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005139 return( ret );
5140 }
5141
5142 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005147 if( ssl->next_record_offset < ssl->in_left )
5148 {
5149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5150 }
5151 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005152 else
5153#endif
5154 ssl->in_left = 0;
5155
5156 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005159 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005160 {
5161 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5163 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005164 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005165 /* Except when waiting for Finished as a bad mac here
5166 * probably means something went wrong in the handshake
5167 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5168 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5169 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5170 {
5171#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5172 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5173 {
5174 mbedtls_ssl_send_alert_message( ssl,
5175 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5176 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5177 }
5178#endif
5179 return( ret );
5180 }
5181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005183 if( ssl->conf->badmac_limit != 0 &&
5184 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5187 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005188 }
5189#endif
5190
Hanno Becker4a810fb2017-05-24 16:27:30 +01005191 /* As above, invalid records cause
5192 * dismissal of the whole datagram. */
5193
5194 ssl->next_record_offset = 0;
5195 ssl->in_left = 0;
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005198 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005199 }
5200
5201 return( ret );
5202 }
5203 else
5204#endif
5205 {
5206 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5208 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 mbedtls_ssl_send_alert_message( ssl,
5211 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5212 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005213 }
5214#endif
5215 return( ret );
5216 }
5217 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005218
Simon Butcher99000142016-10-13 17:21:01 +01005219 return( 0 );
5220}
5221
5222int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5223{
5224 int ret;
5225
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005226 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005227 * Handle particular types of records
5228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005230 {
Simon Butcher99000142016-10-13 17:21:01 +01005231 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5232 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005233 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005235 }
5236
Hanno Beckere678eaa2018-08-21 14:57:46 +01005237 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005238 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005239 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005240 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5242 ssl->in_msglen ) );
5243 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005244 }
5245
Hanno Beckere678eaa2018-08-21 14:57:46 +01005246 if( ssl->in_msg[0] != 1 )
5247 {
5248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5249 ssl->in_msg[0] ) );
5250 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5251 }
5252
5253#if defined(MBEDTLS_SSL_PROTO_DTLS)
5254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5255 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5256 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5257 {
5258 if( ssl->handshake == NULL )
5259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5261 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5262 }
5263
5264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5265 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5266 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005267#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005268 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005272 if( ssl->in_msglen != 2 )
5273 {
5274 /* Note: Standard allows for more than one 2 byte alert
5275 to be packed in a single message, but Mbed TLS doesn't
5276 currently support this. */
5277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5278 ssl->in_msglen ) );
5279 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5280 }
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 ssl->in_msg[0], ssl->in_msg[1] ) );
5284
5285 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005286 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005291 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005293 }
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5296 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5299 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005301
5302#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5303 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5304 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5305 {
Hanno Becker90333da2017-10-10 11:27:13 +01005306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005307 /* Will be handled when trying to parse ServerHello */
5308 return( 0 );
5309 }
5310#endif
5311
5312#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5314 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5315 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5316 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5317 {
5318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5319 /* Will be handled in mbedtls_ssl_parse_certificate() */
5320 return( 0 );
5321 }
5322#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5323
5324 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005325 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 }
5327
Hanno Beckerc76c6192017-06-06 10:03:17 +01005328#if defined(MBEDTLS_SSL_PROTO_DTLS)
5329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5330 ssl->handshake != NULL &&
5331 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5332 {
5333 ssl_handshake_wrapup_free_hs_transform( ssl );
5334 }
5335#endif
5336
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 return( 0 );
5338}
5339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005341{
5342 int ret;
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5345 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5346 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005347 {
5348 return( ret );
5349 }
5350
5351 return( 0 );
5352}
5353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005355 unsigned char level,
5356 unsigned char message )
5357{
5358 int ret;
5359
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005360 if( ssl == NULL || ssl->conf == NULL )
5361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005367 ssl->out_msglen = 2;
5368 ssl->out_msg[0] = level;
5369 ssl->out_msg[1] = message;
5370
Hanno Becker67bc7c32018-08-06 11:33:50 +01005371 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005374 return( ret );
5375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005377
5378 return( 0 );
5379}
5380
Paul Bakker5121ce52009-01-03 21:22:43 +00005381/*
5382 * Handshake functions
5383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5385 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5386 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5387 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5388 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5389 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5390 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005391/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5399 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005400 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5401 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005404 ssl->state++;
5405 return( 0 );
5406 }
5407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005410}
5411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5419 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005420 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5421 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005424 ssl->state++;
5425 return( 0 );
5426 }
5427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005430}
Gilles Peskinef9828522017-05-03 12:28:43 +02005431
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005432#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005433/* Some certificate support -> implement write and parse */
5434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005438 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 const mbedtls_x509_crt *crt;
5440 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5445 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005446 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5447 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005450 ssl->state++;
5451 return( 0 );
5452 }
5453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005455 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 {
5457 if( ssl->client_auth == 0 )
5458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 ssl->state++;
5461 return( 0 );
5462 }
5463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 /*
5466 * If using SSLv3 and got no cert, send an Alert message
5467 * (otherwise an empty Certificate message will be sent).
5468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5470 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 {
5472 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5474 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5475 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 goto write_msg;
5479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482#endif /* MBEDTLS_SSL_CLI_C */
5483#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5489 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 }
5491 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005492#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
5496 /*
5497 * 0 . 0 handshake type
5498 * 1 . 3 handshake length
5499 * 4 . 6 length of all certs
5500 * 7 . 9 length of cert. 1
5501 * 10 . n-1 peer certificate
5502 * n . n+2 length of cert. 2
5503 * n+3 . ... upper level cert, etc.
5504 */
5505 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Paul Bakker29087132010-03-21 21:03:34 +00005508 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 {
5510 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005511 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005514 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 }
5517
5518 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5519 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5520 ssl->out_msg[i + 2] = (unsigned char)( n );
5521
5522 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5523 i += n; crt = crt->next;
5524 }
5525
5526 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5527 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5528 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5529
5530 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5532 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005534#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005535write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
5538 ssl->state++;
5539
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005540 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543 return( ret );
5544 }
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
Paul Bakkered27a042013-04-18 22:46:23 +02005548 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005549}
5550
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005551/*
5552 * Once the certificate message is read, parse it into a cert chain and
5553 * perform basic checks, but leave actual verification to the caller
5554 */
5555static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005556{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005557 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005558 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005559 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561#if defined(MBEDTLS_SSL_SRV_C)
5562#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005563 /*
5564 * Check if the client sent an empty certificate
5565 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005568 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005569 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5571 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5572 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005575
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005576 /* The client was asked for a certificate but didn't send
5577 one. The client should know what's going on, so we
5578 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005579 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005580 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5586 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5591 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5592 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5593 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005596
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005597 /* The client was asked for a certificate but didn't send
5598 one. The client should know what's going on, so we
5599 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005600 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005601 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 }
5603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5605 MBEDTLS_SSL_PROTO_TLS1_2 */
5606#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5612 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 }
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5617 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005620 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5621 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 }
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005626
Paul Bakker5121ce52009-01-03 21:22:43 +00005627 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005630 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005631
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005632 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5637 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 }
5640
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005641 /* In case we tried to reuse a session but it failed */
5642 if( ssl->session_negotiate->peer_cert != NULL )
5643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5645 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005646 }
5647
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005648 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005653 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5654 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005655 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005656 }
5657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005659
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005660 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005661
5662 while( i < ssl->in_hslen )
5663 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005664 if ( i + 3 > ssl->in_hslen ) {
5665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5666 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5667 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5668 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 if( ssl->in_msg[i] != 0 )
5671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005673 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5674 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 }
5677
5678 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5679 | (unsigned int) ssl->in_msg[i + 2];
5680 i += 3;
5681
5682 if( n < 128 || i + n > ssl->in_hslen )
5683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005685 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5686 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 }
5689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005691 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005692 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005693 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005694 case 0: /*ok*/
5695 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5696 /* Ignore certificate with an unknown algorithm: maybe a
5697 prior certificate was already trusted. */
5698 break;
5699
5700 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5701 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5702 goto crt_parse_der_failed;
5703
5704 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5705 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5706 goto crt_parse_der_failed;
5707
5708 default:
5709 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5710 crt_parse_der_failed:
5711 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 return( ret );
5714 }
5715
5716 i += n;
5717 }
5718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005720
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005721 /*
5722 * On client, make sure the server cert doesn't change during renego to
5723 * avoid "triple handshake" attack: https://secure-resumption.com/
5724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005728 {
5729 if( ssl->session->peer_cert == NULL )
5730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5733 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005735 }
5736
5737 if( ssl->session->peer_cert->raw.len !=
5738 ssl->session_negotiate->peer_cert->raw.len ||
5739 memcmp( ssl->session->peer_cert->raw.p,
5740 ssl->session_negotiate->peer_cert->raw.p,
5741 ssl->session->peer_cert->raw.len ) != 0 )
5742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005744 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5745 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005747 }
5748 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005750
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005751 return( 0 );
5752}
5753
5754int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5755{
5756 int ret;
5757 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5758 ssl->transform_negotiate->ciphersuite_info;
5759#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5760 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5761 ? ssl->handshake->sni_authmode
5762 : ssl->conf->authmode;
5763#else
5764 const int authmode = ssl->conf->authmode;
5765#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005766 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005767
5768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5769
5770 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5771 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5772 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5773 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5774 {
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5776 ssl->state++;
5777 return( 0 );
5778 }
5779
5780#if defined(MBEDTLS_SSL_SRV_C)
5781 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5782 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5783 {
5784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5785 ssl->state++;
5786 return( 0 );
5787 }
5788
5789 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5790 authmode == MBEDTLS_SSL_VERIFY_NONE )
5791 {
5792 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005794
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005795 ssl->state++;
5796 return( 0 );
5797 }
5798#endif
5799
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005800#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5801 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005802 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005803 {
5804 goto crt_verify;
5805 }
5806#endif
5807
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005808 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005809 {
5810 /* mbedtls_ssl_read_record may have sent an alert already. We
5811 let it decide whether to alert. */
5812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5813 return( ret );
5814 }
5815
5816 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5817 {
5818#if defined(MBEDTLS_SSL_SRV_C)
5819 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5820 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5821 {
5822 ret = 0;
5823 }
5824#endif
5825
5826 ssl->state++;
5827 return( ret );
5828 }
5829
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005830#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5831 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005832 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005833
5834crt_verify:
5835 if( ssl->handshake->ecrs_enabled)
5836 rs_ctx = &ssl->handshake->ecrs_ctx;
5837#endif
5838
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005839 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005840 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005841 mbedtls_x509_crt *ca_chain;
5842 mbedtls_x509_crl *ca_crl;
5843
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005844#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005845 if( ssl->handshake->sni_ca_chain != NULL )
5846 {
5847 ca_chain = ssl->handshake->sni_ca_chain;
5848 ca_crl = ssl->handshake->sni_ca_crl;
5849 }
5850 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005851#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005852 {
5853 ca_chain = ssl->conf->ca_chain;
5854 ca_crl = ssl->conf->ca_crl;
5855 }
5856
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005857 /*
5858 * Main check: verify certificate
5859 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005860 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005861 ssl->session_negotiate->peer_cert,
5862 ca_chain, ca_crl,
5863 ssl->conf->cert_profile,
5864 ssl->hostname,
5865 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005866 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867
5868 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005871 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005872
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005873#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5874 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005875 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005876#endif
5877
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005878 /*
5879 * Secondary checks: always done, but change 'ret' only if it was 0
5880 */
5881
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005882#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005885
5886 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005888 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005889 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005890 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005893 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005895 }
5896 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005897#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005900 ciphersuite_info,
5901 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005902 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005905 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005907 }
5908
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005909 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5910 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5911 * with details encoded in the verification flags. All other kinds
5912 * of error codes, including those from the user provided f_vrfy
5913 * functions, are treated as fatal and lead to a failure of
5914 * ssl_parse_certificate even if verification was optional. */
5915 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5916 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5917 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5918 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005920 }
5921
5922 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5923 {
5924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5925 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5926 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005927
5928 if( ret != 0 )
5929 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005930 uint8_t alert;
5931
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005932 /* The certificate may have been rejected for several reasons.
5933 Pick one and send the corresponding alert. Which alert to send
5934 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005935 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5936 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5937 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5938 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5939 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5940 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5941 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5942 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5943 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5944 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5945 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5946 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5947 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5948 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5949 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5950 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5951 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5952 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5953 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5954 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005955 else
5956 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005957 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5958 alert );
5959 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005960
Hanno Beckere6706e62017-05-15 16:05:15 +01005961#if defined(MBEDTLS_DEBUG_C)
5962 if( ssl->session_negotiate->verify_result != 0 )
5963 {
5964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5965 ssl->session_negotiate->verify_result ) );
5966 }
5967 else
5968 {
5969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5970 }
5971#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005972 }
5973
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005974 ssl->state++;
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005977
5978 return( ret );
5979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5981 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5982 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5983 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5984 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5985 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5986 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005989{
5990 int ret;
5991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005995 ssl->out_msglen = 1;
5996 ssl->out_msg[0] = 1;
5997
Paul Bakker5121ce52009-01-03 21:22:43 +00005998 ssl->state++;
5999
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006000 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006001 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 return( ret );
6004 }
6005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
6008 return( 0 );
6009}
6010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006012{
6013 int ret;
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
Hanno Becker327c93b2018-08-15 13:56:18 +01006017 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006020 return( ret );
6021 }
6022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6027 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029 }
6030
Hanno Beckere678eaa2018-08-21 14:57:46 +01006031 /* CCS records are only accepted if they have length 1 and content '1',
6032 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006033
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006034 /*
6035 * Switch to our negotiated transform and session parameters for inbound
6036 * data.
6037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006039 ssl->transform_in = ssl->transform_negotiate;
6040 ssl->session_in = ssl->session_negotiate;
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006043 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006046 ssl_dtls_replay_reset( ssl );
6047#endif
6048
6049 /* Increment epoch */
6050 if( ++ssl->in_epoch == 0 )
6051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006053 /* This is highly unlikely to happen for legitimate reasons, so
6054 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006056 }
6057 }
6058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006060 memset( ssl->in_ctr, 0, 8 );
6061
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006062 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6065 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006070 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6071 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006073 }
6074 }
6075#endif
6076
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 ssl->state++;
6078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
6081 return( 0 );
6082}
6083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6085 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006086{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006087 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6090 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6091 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006092 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006093 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6096#if defined(MBEDTLS_SHA512_C)
6097 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006098 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6099 else
6100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#if defined(MBEDTLS_SHA256_C)
6102 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006103 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006104 else
6105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006109 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006110 }
Paul Bakker380da532012-04-18 16:10:25 +00006111}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6116 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006117 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6118 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6121#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006122 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006125 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006128}
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006131 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6134 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006135 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6136 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6139#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006140 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006143 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006146}
6147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6149 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6150static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006151 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006152{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006153 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6154 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006155}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006156#endif
Paul Bakker380da532012-04-18 16:10:25 +00006157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6159#if defined(MBEDTLS_SHA256_C)
6160static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006161 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006162{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006163 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006164}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006165#endif
Paul Bakker380da532012-04-18 16:10:25 +00006166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#if defined(MBEDTLS_SHA512_C)
6168static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006169 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006170{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006171 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006172}
Paul Bakker769075d2012-11-24 11:26:46 +01006173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006177static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006179{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006180 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006181 mbedtls_md5_context md5;
6182 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006183
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 unsigned char padbuf[48];
6185 unsigned char md5sum[16];
6186 unsigned char sha1sum[20];
6187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006189 if( !session )
6190 session = ssl->session;
6191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006193
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006194 mbedtls_md5_init( &md5 );
6195 mbedtls_sha1_init( &sha1 );
6196
6197 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6198 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006199
6200 /*
6201 * SSLv3:
6202 * hash =
6203 * MD5( master + pad2 +
6204 * MD5( handshake + sender + master + pad1 ) )
6205 * + SHA1( master + pad2 +
6206 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006207 */
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006210 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6211 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006215 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6216 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006217#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006220 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006221
Paul Bakker1ef83d62012-04-11 12:09:53 +00006222 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006224 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6225 mbedtls_md5_update_ret( &md5, session->master, 48 );
6226 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6227 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006229 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6230 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6231 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6232 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Paul Bakker1ef83d62012-04-11 12:09:53 +00006234 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006235
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006236 mbedtls_md5_starts_ret( &md5 );
6237 mbedtls_md5_update_ret( &md5, session->master, 48 );
6238 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6239 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6240 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006242 mbedtls_sha1_starts_ret( &sha1 );
6243 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6244 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6245 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6246 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006250 mbedtls_md5_free( &md5 );
6251 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006253 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6254 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6255 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006262static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006264{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006265 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006266 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006267 mbedtls_md5_context md5;
6268 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006269 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006272 if( !session )
6273 session = ssl->session;
6274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006276
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006277 mbedtls_md5_init( &md5 );
6278 mbedtls_sha1_init( &sha1 );
6279
6280 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6281 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Paul Bakker1ef83d62012-04-11 12:09:53 +00006283 /*
6284 * TLSv1:
6285 * hash = PRF( master, finished_label,
6286 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6287 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006290 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6291 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006292#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006295 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6296 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006297#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006300 ? "client finished"
6301 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006302
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006303 mbedtls_md5_finish_ret( &md5, padbuf );
6304 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006305
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006306 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006307 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006310
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006311 mbedtls_md5_free( &md5 );
6312 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006313
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006314 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6321#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006322static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006324{
6325 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006326 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006327 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006328 unsigned char padbuf[32];
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006331 if( !session )
6332 session = ssl->session;
6333
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006334 mbedtls_sha256_init( &sha256 );
6335
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006337
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006338 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006339
6340 /*
6341 * TLSv1.2:
6342 * hash = PRF( master, finished_label,
6343 * Hash( handshake ) )[0.11]
6344 */
6345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#if !defined(MBEDTLS_SHA256_ALT)
6347 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006348 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006349#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006352 ? "client finished"
6353 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006355 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006356
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006357 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006358 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006361
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006362 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006364 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006371static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006373{
6374 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006375 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006376 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006377 unsigned char padbuf[48];
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006380 if( !session )
6381 session = ssl->session;
6382
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006383 mbedtls_sha512_init( &sha512 );
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006386
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006387 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006388
6389 /*
6390 * TLSv1.2:
6391 * hash = PRF( master, finished_label,
6392 * Hash( handshake ) )[0.11]
6393 */
6394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006396 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6397 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006398#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006401 ? "client finished"
6402 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006403
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006404 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006405
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006406 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006407 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006410
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006411 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006412
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006413 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#endif /* MBEDTLS_SHA512_C */
6418#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006423
6424 /*
6425 * Free our handshake params
6426 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006427 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006429 ssl->handshake = NULL;
6430
6431 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006432 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006433 */
6434 if( ssl->transform )
6435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436 mbedtls_ssl_transform_free( ssl->transform );
6437 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006438 }
6439 ssl->transform = ssl->transform_negotiate;
6440 ssl->transform_negotiate = NULL;
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006443}
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006446{
6447 int resume = ssl->handshake->resume;
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451#if defined(MBEDTLS_SSL_RENEGOTIATION)
6452 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006455 ssl->renego_records_seen = 0;
6456 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006457#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006458
6459 /*
6460 * Free the previous session and switch in the current one
6461 */
Paul Bakker0a597072012-09-25 21:55:46 +00006462 if( ssl->session )
6463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006465 /* RFC 7366 3.1: keep the EtM state */
6466 ssl->session_negotiate->encrypt_then_mac =
6467 ssl->session->encrypt_then_mac;
6468#endif
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 mbedtls_ssl_session_free( ssl->session );
6471 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006472 }
6473 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006474 ssl->session_negotiate = NULL;
6475
Paul Bakker0a597072012-09-25 21:55:46 +00006476 /*
6477 * Add cache entry
6478 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006479 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006480 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006481 resume == 0 )
6482 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006483 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006485 }
Paul Bakker0a597072012-09-25 21:55:46 +00006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006489 ssl->handshake->flight != NULL )
6490 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006491 /* Cancel handshake timer */
6492 ssl_set_timer( ssl, 0 );
6493
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006494 /* Keep last flight around in case we need to resend it:
6495 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006497 }
6498 else
6499#endif
6500 ssl_handshake_wrapup_free_hs_transform( ssl );
6501
Paul Bakker48916f92012-09-16 19:57:18 +00006502 ssl->state++;
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006505}
6506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006508{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006509 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006512
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006513 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006514
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006515 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006516
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006517 /*
6518 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6519 * may define some other value. Currently (early 2016), no defined
6520 * ciphersuite does this (and this is unlikely to change as activity has
6521 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006526 ssl->verify_data_len = hash_len;
6527 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006528#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006529
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6532 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006533
6534 /*
6535 * In case of session resuming, invert the client and server
6536 * ChangeCipherSpec messages order.
6537 */
Paul Bakker0a597072012-09-25 21:55:46 +00006538 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 }
6549 else
6550 ssl->state++;
6551
Paul Bakker48916f92012-09-16 19:57:18 +00006552 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006553 * Switch to our negotiated transform and session parameters for outbound
6554 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006560 {
6561 unsigned char i;
6562
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006563 /* Remember current epoch settings for resending */
6564 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006565 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006566
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006567 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006568 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006569
6570 /* Increment epoch */
6571 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006572 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006573 break;
6574
6575 /* The loop goes to its end iff the counter is wrapping */
6576 if( i == 0 )
6577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6579 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006580 }
6581 }
6582 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006584 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006585
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006586 ssl->transform_out = ssl->transform_negotiate;
6587 ssl->session_out = ssl->session_negotiate;
6588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6590 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6595 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006596 }
6597 }
6598#endif
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006603#endif
6604
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006605 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 return( ret );
6609 }
6610
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006611#if defined(MBEDTLS_SSL_PROTO_DTLS)
6612 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6613 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6614 {
6615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6616 return( ret );
6617 }
6618#endif
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006621
6622 return( 0 );
6623}
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006626#define SSL_MAX_HASH_LEN 36
6627#else
6628#define SSL_MAX_HASH_LEN 12
6629#endif
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632{
Paul Bakker23986e52011-04-24 08:57:21 +00006633 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006634 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006635 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006639 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640
Hanno Becker327c93b2018-08-15 13:56:18 +01006641 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 return( ret );
6645 }
6646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006650 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6651 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 }
6654
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006655 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#if defined(MBEDTLS_SSL_PROTO_SSL3)
6657 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006658 hash_len = 36;
6659 else
6660#endif
6661 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6664 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006667 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6668 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 }
6671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006673 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006676 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6677 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006679 }
6680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006682 ssl->verify_data_len = hash_len;
6683 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006684#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006685
Paul Bakker0a597072012-09-25 21:55:46 +00006686 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006689 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006693 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006695#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006696 }
6697 else
6698 ssl->state++;
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006701 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006703#endif
6704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006706
6707 return( 0 );
6708}
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6715 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6716 mbedtls_md5_init( &handshake->fin_md5 );
6717 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006718 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6719 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6722#if defined(MBEDTLS_SHA256_C)
6723 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006724 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#if defined(MBEDTLS_SHA512_C)
6727 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006728 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006731
6732 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006733
6734#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6735 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6736 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6737#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_DHM_C)
6740 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_ECDH_C)
6743 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006744#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006745#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006746 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006747#if defined(MBEDTLS_SSL_CLI_C)
6748 handshake->ecjpake_cache = NULL;
6749 handshake->ecjpake_cache_len = 0;
6750#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006751#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006752
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006753#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006754 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006755#endif
6756
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006757#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6758 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6759#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006760}
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6767 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 mbedtls_md_init( &transform->md_ctx_enc );
6770 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006771}
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006776}
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006779{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006780 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006781 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006783 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006785 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006786 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006787
6788 /*
6789 * Either the pointers are now NULL or cleared properly and can be freed.
6790 * Now allocate missing structures.
6791 */
6792 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006793 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006794 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006795 }
Paul Bakker48916f92012-09-16 19:57:18 +00006796
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006797 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006798 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006799 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006800 }
Paul Bakker48916f92012-09-16 19:57:18 +00006801
Paul Bakker82788fb2014-10-20 13:59:19 +02006802 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006803 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006804 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006805 }
Paul Bakker48916f92012-09-16 19:57:18 +00006806
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006807 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006808 if( ssl->handshake == NULL ||
6809 ssl->transform_negotiate == NULL ||
6810 ssl->session_negotiate == NULL )
6811 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 mbedtls_free( ssl->handshake );
6815 mbedtls_free( ssl->transform_negotiate );
6816 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006817
6818 ssl->handshake = NULL;
6819 ssl->transform_negotiate = NULL;
6820 ssl->session_negotiate = NULL;
6821
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006823 }
6824
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006825 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006827 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006828 ssl_handshake_params_init( ssl->handshake );
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6832 {
6833 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006834
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6836 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6837 else
6838 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006839
6840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006841 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006842#endif
6843
Paul Bakker48916f92012-09-16 19:57:18 +00006844 return( 0 );
6845}
6846
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006847#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006848/* Dummy cookie callbacks for defaults */
6849static int ssl_cookie_write_dummy( void *ctx,
6850 unsigned char **p, unsigned char *end,
6851 const unsigned char *cli_id, size_t cli_id_len )
6852{
6853 ((void) ctx);
6854 ((void) p);
6855 ((void) end);
6856 ((void) cli_id);
6857 ((void) cli_id_len);
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006860}
6861
6862static int ssl_cookie_check_dummy( void *ctx,
6863 const unsigned char *cookie, size_t cookie_len,
6864 const unsigned char *cli_id, size_t cli_id_len )
6865{
6866 ((void) ctx);
6867 ((void) cookie);
6868 ((void) cookie_len);
6869 ((void) cli_id);
6870 ((void) cli_id_len);
6871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006873}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006874#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006875
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006876/* Once ssl->out_hdr as the address of the beginning of the
6877 * next outgoing record is set, deduce the other pointers.
6878 *
6879 * Note: For TLS, we save the implicit record sequence number
6880 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6881 * and the caller has to make sure there's space for this.
6882 */
6883
6884static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6885 mbedtls_ssl_transform *transform )
6886{
6887#if defined(MBEDTLS_SSL_PROTO_DTLS)
6888 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6889 {
6890 ssl->out_ctr = ssl->out_hdr + 3;
6891 ssl->out_len = ssl->out_hdr + 11;
6892 ssl->out_iv = ssl->out_hdr + 13;
6893 }
6894 else
6895#endif
6896 {
6897 ssl->out_ctr = ssl->out_hdr - 8;
6898 ssl->out_len = ssl->out_hdr + 3;
6899 ssl->out_iv = ssl->out_hdr + 5;
6900 }
6901
6902 /* Adjust out_msg to make space for explicit IV, if used. */
6903 if( transform != NULL &&
6904 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6905 {
6906 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6907 }
6908 else
6909 ssl->out_msg = ssl->out_iv;
6910}
6911
6912/* Once ssl->in_hdr as the address of the beginning of the
6913 * next incoming record is set, deduce the other pointers.
6914 *
6915 * Note: For TLS, we save the implicit record sequence number
6916 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6917 * and the caller has to make sure there's space for this.
6918 */
6919
6920static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6921 mbedtls_ssl_transform *transform )
6922{
6923#if defined(MBEDTLS_SSL_PROTO_DTLS)
6924 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6925 {
6926 ssl->in_ctr = ssl->in_hdr + 3;
6927 ssl->in_len = ssl->in_hdr + 11;
6928 ssl->in_iv = ssl->in_hdr + 13;
6929 }
6930 else
6931#endif
6932 {
6933 ssl->in_ctr = ssl->in_hdr - 8;
6934 ssl->in_len = ssl->in_hdr + 3;
6935 ssl->in_iv = ssl->in_hdr + 5;
6936 }
6937
6938 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6939 if( transform != NULL &&
6940 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6941 {
6942 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6943 }
6944 else
6945 ssl->in_msg = ssl->in_iv;
6946}
6947
Paul Bakker5121ce52009-01-03 21:22:43 +00006948/*
6949 * Initialize an SSL context
6950 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006951void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6952{
6953 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6954}
6955
6956/*
6957 * Setup an SSL context
6958 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006959
6960static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6961{
6962 /* Set the incoming and outgoing record pointers. */
6963#if defined(MBEDTLS_SSL_PROTO_DTLS)
6964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6965 {
6966 ssl->out_hdr = ssl->out_buf;
6967 ssl->in_hdr = ssl->in_buf;
6968 }
6969 else
6970#endif /* MBEDTLS_SSL_PROTO_DTLS */
6971 {
6972 ssl->out_hdr = ssl->out_buf + 8;
6973 ssl->in_hdr = ssl->in_buf + 8;
6974 }
6975
6976 /* Derive other internal pointers. */
6977 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6978 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6979}
6980
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006981int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006982 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983{
Paul Bakker48916f92012-09-16 19:57:18 +00006984 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006986 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006987
6988 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006989 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006990 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006991
6992 /* Set to NULL in case of an error condition */
6993 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006994
Angus Grattond8213d02016-05-25 20:56:48 +10006995 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6996 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006997 {
Angus Grattond8213d02016-05-25 20:56:48 +10006998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006999 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007000 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007001 }
7002
7003 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7004 if( ssl->out_buf == NULL )
7005 {
7006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007007 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007008 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 }
7010
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007011 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007012
Paul Bakker48916f92012-09-16 19:57:18 +00007013 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007014 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
7016 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007017
7018error:
7019 mbedtls_free( ssl->in_buf );
7020 mbedtls_free( ssl->out_buf );
7021
7022 ssl->conf = NULL;
7023
7024 ssl->in_buf = NULL;
7025 ssl->out_buf = NULL;
7026
7027 ssl->in_hdr = NULL;
7028 ssl->in_ctr = NULL;
7029 ssl->in_len = NULL;
7030 ssl->in_iv = NULL;
7031 ssl->in_msg = NULL;
7032
7033 ssl->out_hdr = NULL;
7034 ssl->out_ctr = NULL;
7035 ssl->out_len = NULL;
7036 ssl->out_iv = NULL;
7037 ssl->out_msg = NULL;
7038
k-stachowiak9f7798e2018-07-31 16:52:32 +02007039 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007040}
7041
7042/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007043 * Reset an initialized and used SSL context for re-use while retaining
7044 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007045 *
7046 * If partial is non-zero, keep data in the input buffer and client ID.
7047 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007048 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007049static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007050{
Paul Bakker48916f92012-09-16 19:57:18 +00007051 int ret;
7052
Hanno Becker7e772132018-08-10 12:38:21 +01007053#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7054 !defined(MBEDTLS_SSL_SRV_C)
7055 ((void) partial);
7056#endif
7057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007059
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007060 /* Cancel any possibly running timer */
7061 ssl_set_timer( ssl, 0 );
7062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#if defined(MBEDTLS_SSL_RENEGOTIATION)
7064 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007065 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007066
7067 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7069 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007072
Paul Bakker7eb013f2011-10-06 12:37:39 +00007073 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007074 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007075
7076 ssl->in_msgtype = 0;
7077 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007079 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007080 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007083 ssl_dtls_replay_reset( ssl );
7084#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007085
7086 ssl->in_hslen = 0;
7087 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007088
7089 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007090
7091 ssl->out_msgtype = 0;
7092 ssl->out_msglen = 0;
7093 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7095 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007096 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007097#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007098
Hanno Becker19859472018-08-06 09:40:20 +01007099 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7100
Paul Bakker48916f92012-09-16 19:57:18 +00007101 ssl->transform_in = NULL;
7102 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007103
Hanno Becker78640902018-08-13 16:35:15 +01007104 ssl->session_in = NULL;
7105 ssl->session_out = NULL;
7106
Angus Grattond8213d02016-05-25 20:56:48 +10007107 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007108
7109#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007110 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007111#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7112 {
7113 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007114 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007115 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7118 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7121 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7124 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007125 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007126 }
7127#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007128
Paul Bakker48916f92012-09-16 19:57:18 +00007129 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 mbedtls_ssl_transform_free( ssl->transform );
7132 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007133 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007134 }
Paul Bakker48916f92012-09-16 19:57:18 +00007135
Paul Bakkerc0463502013-02-14 11:19:38 +01007136 if( ssl->session )
7137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 mbedtls_ssl_session_free( ssl->session );
7139 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007140 ssl->session = NULL;
7141 }
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007144 ssl->alpn_chosen = NULL;
7145#endif
7146
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007147#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007148#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007149 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007150#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007151 {
7152 mbedtls_free( ssl->cli_id );
7153 ssl->cli_id = NULL;
7154 ssl->cli_id_len = 0;
7155 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007156#endif
7157
Paul Bakker48916f92012-09-16 19:57:18 +00007158 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7159 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007160
7161 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007162}
7163
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007164/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007165 * Reset an initialized and used SSL context for re-use while retaining
7166 * all application-set variables, function pointers and data.
7167 */
7168int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7169{
7170 return( ssl_session_reset_int( ssl, 0 ) );
7171}
7172
7173/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007174 * SSL set accessors
7175 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007176void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007177{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007178 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007179}
7180
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007181void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007182{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007183 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007184}
7185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007187void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007188{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007189 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007190}
7191#endif
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007194void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007196 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007197}
7198#endif
7199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007201
Hanno Becker1841b0a2018-08-24 11:13:57 +01007202void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7203 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007204{
7205 ssl->disable_datagram_packing = !allow_packing;
7206}
7207
7208void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7209 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007210{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007211 conf->hs_timeout_min = min;
7212 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007213}
7214#endif
7215
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007216void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007218 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007219}
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007222void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007223 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007224 void *p_vrfy )
7225{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007226 conf->f_vrfy = f_vrfy;
7227 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007230
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007231void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007232 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007233 void *p_rng )
7234{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007235 conf->f_rng = f_rng;
7236 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007237}
7238
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007239void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007240 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007241 void *p_dbg )
7242{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007243 conf->f_dbg = f_dbg;
7244 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007245}
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007248 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007249 mbedtls_ssl_send_t *f_send,
7250 mbedtls_ssl_recv_t *f_recv,
7251 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007252{
7253 ssl->p_bio = p_bio;
7254 ssl->f_send = f_send;
7255 ssl->f_recv = f_recv;
7256 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007257}
7258
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007259#if defined(MBEDTLS_SSL_PROTO_DTLS)
7260void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7261{
7262 ssl->mtu = mtu;
7263}
7264#endif
7265
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007266void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007267{
7268 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007269}
7270
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007271void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7272 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007273 mbedtls_ssl_set_timer_t *f_set_timer,
7274 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007275{
7276 ssl->p_timer = p_timer;
7277 ssl->f_set_timer = f_set_timer;
7278 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007279
7280 /* Make sure we start with no timer running */
7281 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007282}
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007285void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007286 void *p_cache,
7287 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7288 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007289{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007290 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007291 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007292 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296#if defined(MBEDTLS_SSL_CLI_C)
7297int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007298{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007299 int ret;
7300
7301 if( ssl == NULL ||
7302 session == NULL ||
7303 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007304 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007307 }
7308
7309 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7310 return( ret );
7311
Paul Bakker0a597072012-09-25 21:55:46 +00007312 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007313
7314 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007318void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007319 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007321 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7322 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7323 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7324 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007325}
7326
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007327void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007328 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007329 int major, int minor )
7330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007332 return;
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007335 return;
7336
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007337 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007338}
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007341void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007342 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007343{
7344 conf->cert_profile = profile;
7345}
7346
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007347/* Append a new keycert entry to a (possibly empty) list */
7348static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7349 mbedtls_x509_crt *cert,
7350 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007351{
niisato8ee24222018-06-25 19:05:48 +09007352 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007353
niisato8ee24222018-06-25 19:05:48 +09007354 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7355 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007356 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007357
niisato8ee24222018-06-25 19:05:48 +09007358 new_cert->cert = cert;
7359 new_cert->key = key;
7360 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007361
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007362 /* Update head is the list was null, else add to the end */
7363 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007364 {
niisato8ee24222018-06-25 19:05:48 +09007365 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007366 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007367 else
7368 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007369 mbedtls_ssl_key_cert *cur = *head;
7370 while( cur->next != NULL )
7371 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007372 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007373 }
7374
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007375 return( 0 );
7376}
7377
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007378int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007379 mbedtls_x509_crt *own_cert,
7380 mbedtls_pk_context *pk_key )
7381{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007382 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007383}
7384
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007385void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007386 mbedtls_x509_crt *ca_chain,
7387 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007388{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007389 conf->ca_chain = ca_chain;
7390 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007393
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007394#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7395int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7396 mbedtls_x509_crt *own_cert,
7397 mbedtls_pk_context *pk_key )
7398{
7399 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7400 own_cert, pk_key ) );
7401}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007402
7403void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7404 mbedtls_x509_crt *ca_chain,
7405 mbedtls_x509_crl *ca_crl )
7406{
7407 ssl->handshake->sni_ca_chain = ca_chain;
7408 ssl->handshake->sni_ca_crl = ca_crl;
7409}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007410
7411void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7412 int authmode )
7413{
7414 ssl->handshake->sni_authmode = authmode;
7415}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007416#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7417
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007418#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007419/*
7420 * Set EC J-PAKE password for current handshake
7421 */
7422int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7423 const unsigned char *pw,
7424 size_t pw_len )
7425{
7426 mbedtls_ecjpake_role role;
7427
Janos Follath8eb64132016-06-03 15:40:57 +01007428 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7430
7431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7432 role = MBEDTLS_ECJPAKE_SERVER;
7433 else
7434 role = MBEDTLS_ECJPAKE_CLIENT;
7435
7436 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7437 role,
7438 MBEDTLS_MD_SHA256,
7439 MBEDTLS_ECP_DP_SECP256R1,
7440 pw, pw_len ) );
7441}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007442#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007445int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007446 const unsigned char *psk, size_t psk_len,
7447 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007448{
Paul Bakker6db455e2013-09-18 17:29:31 +02007449 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007454
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007455 /* Identity len will be encoded on two bytes */
7456 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007457 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007458 {
7459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7460 }
7461
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007462 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007464 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007465
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007466 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007467 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007468 conf->psk_len = 0;
7469 }
7470 if( conf->psk_identity != NULL )
7471 {
7472 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007473 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007474 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007475 }
7476
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007477 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7478 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007479 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007480 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007481 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007482 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007483 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007484 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007485 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007486
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007487 conf->psk_len = psk_len;
7488 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007489
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007490 memcpy( conf->psk, psk, conf->psk_len );
7491 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007492
7493 return( 0 );
7494}
7495
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007496int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7497 const unsigned char *psk, size_t psk_len )
7498{
7499 if( psk == NULL || ssl->handshake == NULL )
7500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7501
7502 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7504
7505 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007506 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007507 mbedtls_platform_zeroize( ssl->handshake->psk,
7508 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007509 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007510 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007511 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007512
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007513 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007514 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007515
7516 ssl->handshake->psk_len = psk_len;
7517 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7518
7519 return( 0 );
7520}
7521
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007522void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007524 size_t),
7525 void *p_psk )
7526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007527 conf->f_psk = f_psk;
7528 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007531
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007532#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007533
7534#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007535int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007536{
7537 int ret;
7538
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007539 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7540 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7541 {
7542 mbedtls_mpi_free( &conf->dhm_P );
7543 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007544 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
7547 return( 0 );
7548}
Hanno Becker470a8c42017-10-04 15:28:46 +01007549#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Hanno Beckera90658f2017-10-04 15:29:08 +01007551int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7552 const unsigned char *dhm_P, size_t P_len,
7553 const unsigned char *dhm_G, size_t G_len )
7554{
7555 int ret;
7556
7557 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7558 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7559 {
7560 mbedtls_mpi_free( &conf->dhm_P );
7561 mbedtls_mpi_free( &conf->dhm_G );
7562 return( ret );
7563 }
7564
7565 return( 0 );
7566}
Paul Bakker5121ce52009-01-03 21:22:43 +00007567
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007568int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007569{
7570 int ret;
7571
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007572 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7573 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7574 {
7575 mbedtls_mpi_free( &conf->dhm_P );
7576 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007577 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007578 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007579
7580 return( 0 );
7581}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007582#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007583
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007584#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7585/*
7586 * Set the minimum length for Diffie-Hellman parameters
7587 */
7588void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7589 unsigned int bitlen )
7590{
7591 conf->dhm_min_bitlen = bitlen;
7592}
7593#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7594
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007595#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007596/*
7597 * Set allowed/preferred hashes for handshake signatures
7598 */
7599void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7600 const int *hashes )
7601{
7602 conf->sig_hashes = hashes;
7603}
Hanno Becker947194e2017-04-07 13:25:49 +01007604#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007605
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007606#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007607/*
7608 * Set the allowed elliptic curves
7609 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007610void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007611 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007613 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007614}
Hanno Becker947194e2017-04-07 13:25:49 +01007615#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007616
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007617#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007619{
Hanno Becker947194e2017-04-07 13:25:49 +01007620 /* Initialize to suppress unnecessary compiler warning */
7621 size_t hostname_len = 0;
7622
7623 /* Check if new hostname is valid before
7624 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007625 if( hostname != NULL )
7626 {
7627 hostname_len = strlen( hostname );
7628
7629 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7631 }
7632
7633 /* Now it's clear that we will overwrite the old hostname,
7634 * so we can free it safely */
7635
7636 if( ssl->hostname != NULL )
7637 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007638 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007639 mbedtls_free( ssl->hostname );
7640 }
7641
7642 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007643
Paul Bakker5121ce52009-01-03 21:22:43 +00007644 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007645 {
7646 ssl->hostname = NULL;
7647 }
7648 else
7649 {
7650 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007651 if( ssl->hostname == NULL )
7652 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007653
Hanno Becker947194e2017-04-07 13:25:49 +01007654 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007655
Hanno Becker947194e2017-04-07 13:25:49 +01007656 ssl->hostname[hostname_len] = '\0';
7657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007658
7659 return( 0 );
7660}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007661#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007662
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007663#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007664void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007666 const unsigned char *, size_t),
7667 void *p_sni )
7668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007669 conf->f_sni = f_sni;
7670 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007671}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007675int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007676{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007677 size_t cur_len, tot_len;
7678 const char **p;
7679
7680 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007681 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7682 * MUST NOT be truncated."
7683 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007684 */
7685 tot_len = 0;
7686 for( p = protos; *p != NULL; p++ )
7687 {
7688 cur_len = strlen( *p );
7689 tot_len += cur_len;
7690
Ronald Cron157cffe2020-04-23 16:41:44 +02007691 if( ( cur_len == 0 ) ||
7692 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7693 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007695 }
7696
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007697 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007698
7699 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007700}
7701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007703{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007704 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007707
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007708void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007710 conf->max_major_ver = major;
7711 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007712}
7713
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007714void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007715{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007716 conf->min_major_ver = major;
7717 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007718}
7719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007721void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007722{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007723 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007724}
7725#endif
7726
Janos Follath088ce432017-04-10 12:42:31 +01007727#if defined(MBEDTLS_SSL_SRV_C)
7728void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7729 char cert_req_ca_list )
7730{
7731 conf->cert_req_ca_list = cert_req_ca_list;
7732}
7733#endif
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007736void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007737{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007738 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007739}
7740#endif
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007743void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007744{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007745 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007746}
7747#endif
7748
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007749#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007750void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007751{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007752 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007753}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007754#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007757int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007760 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007763 }
7764
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007765 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007766
7767 return( 0 );
7768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007772void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007773{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007774 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007779void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007780{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007781 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007782}
7783#endif
7784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007785void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007787 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007788}
7789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007791void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007792{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007793 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007794}
7795
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007796void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007798 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007799}
7800
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007801void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007802 const unsigned char period[8] )
7803{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007804 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007809#if defined(MBEDTLS_SSL_CLI_C)
7810void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007811{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007812 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007813}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007814#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007815
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007816#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007817void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7818 mbedtls_ssl_ticket_write_t *f_ticket_write,
7819 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7820 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007821{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007822 conf->f_ticket_write = f_ticket_write;
7823 conf->f_ticket_parse = f_ticket_parse;
7824 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007825}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007828
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007829#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7830void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7831 mbedtls_ssl_export_keys_t *f_export_keys,
7832 void *p_export_keys )
7833{
7834 conf->f_export_keys = f_export_keys;
7835 conf->p_export_keys = p_export_keys;
7836}
7837#endif
7838
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007839#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007840void mbedtls_ssl_conf_async_private_cb(
7841 mbedtls_ssl_config *conf,
7842 mbedtls_ssl_async_sign_t *f_async_sign,
7843 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7844 mbedtls_ssl_async_resume_t *f_async_resume,
7845 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007846 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007847{
7848 conf->f_async_sign_start = f_async_sign;
7849 conf->f_async_decrypt_start = f_async_decrypt;
7850 conf->f_async_resume = f_async_resume;
7851 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007852 conf->p_async_config_data = async_config_data;
7853}
7854
Gilles Peskine8f97af72018-04-26 11:46:10 +02007855void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7856{
7857 return( conf->p_async_config_data );
7858}
7859
Gilles Peskine1febfef2018-04-30 11:54:39 +02007860void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007861{
7862 if( ssl->handshake == NULL )
7863 return( NULL );
7864 else
7865 return( ssl->handshake->user_async_ctx );
7866}
7867
Gilles Peskine1febfef2018-04-30 11:54:39 +02007868void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007869 void *ctx )
7870{
7871 if( ssl->handshake != NULL )
7872 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007873}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007874#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007875
Paul Bakker5121ce52009-01-03 21:22:43 +00007876/*
7877 * SSL get accessors
7878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007880{
7881 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7882}
7883
Hanno Becker8b170a02017-10-10 11:51:19 +01007884int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7885{
7886 /*
7887 * Case A: We're currently holding back
7888 * a message for further processing.
7889 */
7890
7891 if( ssl->keep_current_message == 1 )
7892 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007894 return( 1 );
7895 }
7896
7897 /*
7898 * Case B: Further records are pending in the current datagram.
7899 */
7900
7901#if defined(MBEDTLS_SSL_PROTO_DTLS)
7902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7903 ssl->in_left > ssl->next_record_offset )
7904 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007906 return( 1 );
7907 }
7908#endif /* MBEDTLS_SSL_PROTO_DTLS */
7909
7910 /*
7911 * Case C: A handshake message is being processed.
7912 */
7913
Hanno Becker8b170a02017-10-10 11:51:19 +01007914 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7915 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007917 return( 1 );
7918 }
7919
7920 /*
7921 * Case D: An application data message is being processed
7922 */
7923 if( ssl->in_offt != NULL )
7924 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007926 return( 1 );
7927 }
7928
7929 /*
7930 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007931 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007932 * we implement support for multiple alerts in single records.
7933 */
7934
7935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7936 return( 0 );
7937}
7938
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007939uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007940{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007941 if( ssl->session != NULL )
7942 return( ssl->session->verify_result );
7943
7944 if( ssl->session_negotiate != NULL )
7945 return( ssl->session_negotiate->verify_result );
7946
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007947 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007948}
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007951{
Paul Bakker926c8e42013-03-06 10:23:34 +01007952 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007953 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007956}
7957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007962 {
7963 switch( ssl->minor_ver )
7964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007966 return( "DTLSv1.0" );
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007969 return( "DTLSv1.2" );
7970
7971 default:
7972 return( "unknown (DTLS)" );
7973 }
7974 }
7975#endif
7976
Paul Bakker43ca69c2011-01-15 17:35:19 +00007977 switch( ssl->minor_ver )
7978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007980 return( "SSLv3.0" );
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007983 return( "TLSv1.0" );
7984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007986 return( "TLSv1.1" );
7987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007989 return( "TLSv1.2" );
7990
Paul Bakker43ca69c2011-01-15 17:35:19 +00007991 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007992 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007993 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007994}
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007997{
Hanno Becker3136ede2018-08-17 15:28:19 +01007998 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008000 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008001
Hanno Becker78640902018-08-13 16:35:15 +01008002 if( transform == NULL )
8003 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005#if defined(MBEDTLS_ZLIB_SUPPORT)
8006 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8007 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008008#endif
8009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 case MBEDTLS_MODE_GCM:
8013 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008014 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008016 transform_expansion = transform->minlen;
8017 break;
8018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008020
8021 block_size = mbedtls_cipher_get_block_size(
8022 &transform->cipher_ctx_enc );
8023
Hanno Becker3136ede2018-08-17 15:28:19 +01008024 /* Expansion due to the addition of the MAC. */
8025 transform_expansion += transform->maclen;
8026
8027 /* Expansion due to the addition of CBC padding;
8028 * Theoretically up to 256 bytes, but we never use
8029 * more than the block size of the underlying cipher. */
8030 transform_expansion += block_size;
8031
8032 /* For TLS 1.1 or higher, an explicit IV is added
8033 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008034#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8035 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008036 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008037#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008038
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008039 break;
8040
8041 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008044 }
8045
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008046 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008047}
8048
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008049#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8050size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8051{
8052 size_t max_len;
8053
8054 /*
8055 * Assume mfl_code is correct since it was checked when set
8056 */
Angus Grattond8213d02016-05-25 20:56:48 +10008057 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008058
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008059 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008060 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008061 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008062 {
Angus Grattond8213d02016-05-25 20:56:48 +10008063 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008064 }
8065
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008066 /* During a handshake, use the value being negotiated */
8067 if( ssl->session_negotiate != NULL &&
8068 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8069 {
8070 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8071 }
8072
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008073 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008074}
8075#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8076
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008077#if defined(MBEDTLS_SSL_PROTO_DTLS)
8078static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8079{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008080 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8081 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8082 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8083 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8084 return ( 0 );
8085
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008086 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8087 return( ssl->mtu );
8088
8089 if( ssl->mtu == 0 )
8090 return( ssl->handshake->mtu );
8091
8092 return( ssl->mtu < ssl->handshake->mtu ?
8093 ssl->mtu : ssl->handshake->mtu );
8094}
8095#endif /* MBEDTLS_SSL_PROTO_DTLS */
8096
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008097int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8098{
8099 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8100
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008101#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8102 !defined(MBEDTLS_SSL_PROTO_DTLS)
8103 (void) ssl;
8104#endif
8105
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008106#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8107 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8108
8109 if( max_len > mfl )
8110 max_len = mfl;
8111#endif
8112
8113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008114 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008115 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008116 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008117 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8118 const size_t overhead = (size_t) ret;
8119
8120 if( ret < 0 )
8121 return( ret );
8122
8123 if( mtu <= overhead )
8124 {
8125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8126 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8127 }
8128
8129 if( max_len > mtu - overhead )
8130 max_len = mtu - overhead;
8131 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008133
Hanno Becker0defedb2018-08-10 12:35:02 +01008134#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8135 !defined(MBEDTLS_SSL_PROTO_DTLS)
8136 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008137#endif
8138
8139 return( (int) max_len );
8140}
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_X509_CRT_PARSE_C)
8143const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008144{
8145 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008146 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008147
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008148 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_CLI_C)
8153int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008154{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008155 if( ssl == NULL ||
8156 dst == NULL ||
8157 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008158 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008161 }
8162
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008163 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008166
Paul Bakker5121ce52009-01-03 21:22:43 +00008167/*
Paul Bakker1961b702013-01-25 14:49:24 +01008168 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008173
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008174 if( ssl == NULL || ssl->conf == NULL )
8175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008182 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008184#endif
8185
Paul Bakker1961b702013-01-25 14:49:24 +01008186 return( ret );
8187}
8188
8189/*
8190 * Perform the SSL handshake
8191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008193{
8194 int ret = 0;
8195
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008196 if( ssl == NULL || ssl->conf == NULL )
8197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008204
8205 if( ret != 0 )
8206 break;
8207 }
8208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008210
8211 return( ret );
8212}
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#if defined(MBEDTLS_SSL_RENEGOTIATION)
8215#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008216/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008217 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008220{
8221 int ret;
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008224
8225 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8227 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008228
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008229 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008230 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008232 return( ret );
8233 }
8234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008236
8237 return( 0 );
8238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008240
8241/*
8242 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 * - any side: calling mbedtls_ssl_renegotiate(),
8244 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8245 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008246 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008247 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008251{
8252 int ret;
8253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008255
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008256 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8257 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008258
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008259 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8260 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008264 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008265 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008266 ssl->handshake->out_msg_seq = 1;
8267 else
8268 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008269 }
8270#endif
8271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8273 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008278 return( ret );
8279 }
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008282
8283 return( 0 );
8284}
8285
8286/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008287 * Renegotiate current connection on client,
8288 * or request renegotiation on server
8289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008293
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008294 if( ssl == NULL || ssl->conf == NULL )
8295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008298 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008299 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008305
8306 /* Did we already try/start sending HelloRequest? */
8307 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008309
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008310 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008311 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008315 /*
8316 * On client, either start the renegotiation process or,
8317 * if already in progress, continue the handshake
8318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008323
8324 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008327 return( ret );
8328 }
8329 }
8330 else
8331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008335 return( ret );
8336 }
8337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008339
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008340 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008341}
8342
8343/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008344 * Check record counters and renegotiate if they're above the limit.
8345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008347{
Andres AG2196c7f2016-12-15 17:01:16 +00008348 size_t ep_len = ssl_ep_len( ssl );
8349 int in_ctr_cmp;
8350 int out_ctr_cmp;
8351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8353 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008354 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008355 {
8356 return( 0 );
8357 }
8358
Andres AG2196c7f2016-12-15 17:01:16 +00008359 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8360 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008361 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008362 ssl->conf->renego_period + ep_len, 8 - ep_len );
8363
8364 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008365 {
8366 return( 0 );
8367 }
8368
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008373
8374/*
8375 * Receive application data decrypted from the SSL layer
8376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008378{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008379 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008380 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008381
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008382 if( ssl == NULL || ssl->conf == NULL )
8383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008388 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008391 return( ret );
8392
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008393 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008395 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008396 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008397 return( ret );
8398 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008399 }
8400#endif
8401
Hanno Becker4a810fb2017-05-24 16:27:30 +01008402 /*
8403 * Check if renegotiation is necessary and/or handshake is
8404 * in process. If yes, perform/continue, and fall through
8405 * if an unexpected packet is received while the client
8406 * is waiting for the ServerHello.
8407 *
8408 * (There is no equivalent to the last condition on
8409 * the server-side as it is not treated as within
8410 * a handshake while waiting for the ClientHello
8411 * after a renegotiation request.)
8412 */
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008415 ret = ssl_check_ctr_renegotiate( ssl );
8416 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8417 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008420 return( ret );
8421 }
8422#endif
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008427 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8428 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008431 return( ret );
8432 }
8433 }
8434
Hanno Beckere41158b2017-10-23 13:30:32 +01008435 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008436 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008437 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008438 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008439 if( ssl->f_get_timer != NULL &&
8440 ssl->f_get_timer( ssl->p_timer ) == -1 )
8441 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008442 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008443 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008444
Hanno Becker327c93b2018-08-15 13:56:18 +01008445 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008446 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008447 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8448 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008449
Hanno Becker4a810fb2017-05-24 16:27:30 +01008450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8451 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008452 }
8453
8454 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008456 {
8457 /*
8458 * OpenSSL sends empty messages to randomize the IV
8459 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008460 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008463 return( 0 );
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008466 return( ret );
8467 }
8468 }
8469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008473
Hanno Becker4a810fb2017-05-24 16:27:30 +01008474 /*
8475 * - For client-side, expect SERVER_HELLO_REQUEST.
8476 * - For server-side, expect CLIENT_HELLO.
8477 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8478 */
8479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008483 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008486
8487 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008490 {
8491 continue;
8492 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008495 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008496#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008497
Hanno Becker4a810fb2017-05-24 16:27:30 +01008498#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008499 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008503
8504 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008506 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008507 {
8508 continue;
8509 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008512 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008513#endif /* MBEDTLS_SSL_SRV_C */
8514
Hanno Becker21df7f92017-10-17 11:03:26 +01008515#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008516 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008517 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8518 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8519 ssl->conf->allow_legacy_renegotiation ==
8520 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8521 {
8522 /*
8523 * Accept renegotiation request
8524 */
Paul Bakker48916f92012-09-16 19:57:18 +00008525
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008526 /* DTLS clients need to know renego is server-initiated */
8527#if defined(MBEDTLS_SSL_PROTO_DTLS)
8528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8529 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8530 {
8531 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8532 }
8533#endif
8534 ret = ssl_start_renegotiation( ssl );
8535 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8536 ret != 0 )
8537 {
8538 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8539 return( ret );
8540 }
8541 }
8542 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008543#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008544 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008545 /*
8546 * Refuse renegotiation
8547 */
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551#if defined(MBEDTLS_SSL_PROTO_SSL3)
8552 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008553 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008554 /* SSLv3 does not have a "no_renegotiation" warning, so
8555 we send a fatal alert and abort the connection. */
8556 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8557 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8558 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008559 }
8560 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8562#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8563 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8564 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8567 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8568 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008569 {
8570 return( ret );
8571 }
Paul Bakker48916f92012-09-16 19:57:18 +00008572 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008573 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8575 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008579 }
Paul Bakker48916f92012-09-16 19:57:18 +00008580 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008581
Hanno Becker90333da2017-10-10 11:27:13 +01008582 /* At this point, we don't know whether the renegotiation has been
8583 * completed or not. The cases to consider are the following:
8584 * 1) The renegotiation is complete. In this case, no new record
8585 * has been read yet.
8586 * 2) The renegotiation is incomplete because the client received
8587 * an application data record while awaiting the ServerHello.
8588 * 3) The renegotiation is incomplete because the client received
8589 * a non-handshake, non-application data message while awaiting
8590 * the ServerHello.
8591 * In each of these case, looping will be the proper action:
8592 * - For 1), the next iteration will read a new record and check
8593 * if it's application data.
8594 * - For 2), the loop condition isn't satisfied as application data
8595 * is present, hence continue is the same as break
8596 * - For 3), the loop condition is satisfied and read_record
8597 * will re-deliver the message that was held back by the client
8598 * when expecting the ServerHello.
8599 */
8600 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008601 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008602#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008604 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008605 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008606 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008607 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008610 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008612 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008613 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8618 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008621 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008622 }
8623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8627 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008628 }
8629
8630 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008631
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008632 /* We're going to return something now, cancel timer,
8633 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008635 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008636
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008638 /* If we requested renego but received AppData, resend HelloRequest.
8639 * Do it now, after setting in_offt, to avoid taking this branch
8640 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008644 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008645 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008648 return( ret );
8649 }
8650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008653 }
8654
8655 n = ( len < ssl->in_msglen )
8656 ? len : ssl->in_msglen;
8657
8658 memcpy( buf, ssl->in_offt, n );
8659 ssl->in_msglen -= n;
8660
gabor-mezei-arm3eaa1032020-07-15 10:55:00 +02008661 /* Zeroising the plaintext buffer to erase unused application data
8662 from the memory. */
8663 mbedtls_platform_zeroize( ssl->in_offt, n );
8664
Paul Bakker5121ce52009-01-03 21:22:43 +00008665 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008666 {
8667 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008668 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008669 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008671 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008672 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008673 /* more data available */
8674 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008678
Paul Bakker23986e52011-04-24 08:57:21 +00008679 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008680}
8681
8682/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008683 * Send application data to be encrypted by the SSL layer, taking care of max
8684 * fragment length and buffer size.
8685 *
8686 * According to RFC 5246 Section 6.2.1:
8687 *
8688 * Zero-length fragments of Application data MAY be sent as they are
8689 * potentially useful as a traffic analysis countermeasure.
8690 *
8691 * Therefore, it is possible that the input message length is 0 and the
8692 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008694static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008695 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008696{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008697 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8698 const size_t max_len = (size_t) ret;
8699
8700 if( ret < 0 )
8701 {
8702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8703 return( ret );
8704 }
8705
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008706 if( len > max_len )
8707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008709 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008712 "maximum fragment length: %d > %d",
8713 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008715 }
8716 else
8717#endif
8718 len = max_len;
8719 }
Paul Bakker887bd502011-06-08 13:10:54 +00008720
Paul Bakker5121ce52009-01-03 21:22:43 +00008721 if( ssl->out_left != 0 )
8722 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008723 /*
8724 * The user has previously tried to send the data and
8725 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8726 * written. In this case, we expect the high-level write function
8727 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 return( ret );
8733 }
8734 }
Paul Bakker887bd502011-06-08 13:10:54 +00008735 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008736 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008737 /*
8738 * The user is trying to send a message the first time, so we need to
8739 * copy the data into the internal buffers and setup the data structure
8740 * to keep track of partial writes
8741 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008742 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008744 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008745
Hanno Becker67bc7c32018-08-06 11:33:50 +01008746 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008749 return( ret );
8750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008751 }
8752
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008753 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008754}
8755
8756/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008757 * Write application data, doing 1/n-1 splitting if necessary.
8758 *
8759 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008760 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008761 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008764static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008765 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008766{
8767 int ret;
8768
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008769 if( ssl->conf->cbc_record_splitting ==
8770 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008771 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8773 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8774 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008775 {
8776 return( ssl_write_real( ssl, buf, len ) );
8777 }
8778
8779 if( ssl->split_done == 0 )
8780 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008781 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008782 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008783 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008784 }
8785
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008786 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8787 return( ret );
8788 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008789
8790 return( ret + 1 );
8791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008793
8794/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008795 * Write application data (public-facing wrapper)
8796 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008797int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008798{
8799 int ret;
8800
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008802
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008803 if( ssl == NULL || ssl->conf == NULL )
8804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8805
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008806#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008807 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8808 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008809 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008810 return( ret );
8811 }
8812#endif
8813
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008814 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008815 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008816 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008817 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008819 return( ret );
8820 }
8821 }
8822
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008823#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008824 ret = ssl_write_split( ssl, buf, len );
8825#else
8826 ret = ssl_write_real( ssl, buf, len );
8827#endif
8828
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008830
8831 return( ret );
8832}
8833
8834/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008835 * Notify the peer that the connection is being closed
8836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008838{
8839 int ret;
8840
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008841 if( ssl == NULL || ssl->conf == NULL )
8842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008845
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008846 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8852 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8853 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008856 return( ret );
8857 }
8858 }
8859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008861
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008862 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008863}
8864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008866{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008867 if( transform == NULL )
8868 return;
8869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008871 deflateEnd( &transform->ctx_deflate );
8872 inflateEnd( &transform->ctx_inflate );
8873#endif
8874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8876 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 mbedtls_md_free( &transform->md_ctx_enc );
8879 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008880
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008881 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008882}
8883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884#if defined(MBEDTLS_X509_CRT_PARSE_C)
8885static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008888
8889 while( cur != NULL )
8890 {
8891 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008893 cur = next;
8894 }
8895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008897
Hanno Becker0271f962018-08-16 13:23:47 +01008898#if defined(MBEDTLS_SSL_PROTO_DTLS)
8899
8900static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8901{
8902 unsigned offset;
8903 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8904
8905 if( hs == NULL )
8906 return;
8907
Hanno Becker283f5ef2018-08-24 09:34:47 +01008908 ssl_free_buffered_record( ssl );
8909
Hanno Becker0271f962018-08-16 13:23:47 +01008910 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008911 ssl_buffering_free_slot( ssl, offset );
8912}
8913
8914static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8915 uint8_t slot )
8916{
8917 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8918 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008919
8920 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8921 return;
8922
Hanno Beckere605b192018-08-21 15:59:07 +01008923 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008924 {
Hanno Beckere605b192018-08-21 15:59:07 +01008925 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008926 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008927 mbedtls_free( hs_buf->data );
8928 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008929 }
8930}
8931
8932#endif /* MBEDTLS_SSL_PROTO_DTLS */
8933
Gilles Peskine9b562d52018-04-25 20:32:43 +02008934void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008935{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008936 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8937
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008938 if( handshake == NULL )
8939 return;
8940
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008941#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8942 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8943 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008944 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008945 handshake->async_in_progress = 0;
8946 }
8947#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8948
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008949#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8950 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8951 mbedtls_md5_free( &handshake->fin_md5 );
8952 mbedtls_sha1_free( &handshake->fin_sha1 );
8953#endif
8954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8955#if defined(MBEDTLS_SHA256_C)
8956 mbedtls_sha256_free( &handshake->fin_sha256 );
8957#endif
8958#if defined(MBEDTLS_SHA512_C)
8959 mbedtls_sha512_free( &handshake->fin_sha512 );
8960#endif
8961#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#if defined(MBEDTLS_DHM_C)
8964 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966#if defined(MBEDTLS_ECDH_C)
8967 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008968#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008969#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008970 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008971#if defined(MBEDTLS_SSL_CLI_C)
8972 mbedtls_free( handshake->ecjpake_cache );
8973 handshake->ecjpake_cache = NULL;
8974 handshake->ecjpake_cache_len = 0;
8975#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008976#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008977
Janos Follath4ae5c292016-02-10 11:27:43 +00008978#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8979 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008980 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008982#endif
8983
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008984#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8985 if( handshake->psk != NULL )
8986 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008987 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008988 mbedtls_free( handshake->psk );
8989 }
8990#endif
8991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8993 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008994 /*
8995 * Free only the linked list wrapper, not the keys themselves
8996 * since the belong to the SNI callback
8997 */
8998 if( handshake->sni_key_cert != NULL )
8999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009001
9002 while( cur != NULL )
9003 {
9004 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009006 cur = next;
9007 }
9008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009010
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009011#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009012 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009013#endif
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015#if defined(MBEDTLS_SSL_PROTO_DTLS)
9016 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009017 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009018 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009019#endif
9020
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009021 mbedtls_platform_zeroize( handshake,
9022 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009023}
9024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009026{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009027 if( session == NULL )
9028 return;
9029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009031 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 mbedtls_x509_crt_free( session->peer_cert );
9034 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009035 }
Paul Bakkered27a042013-04-18 22:46:23 +02009036#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009037
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009038#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009040#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009041
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009042 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009043}
9044
Paul Bakker5121ce52009-01-03 21:22:43 +00009045/*
9046 * Free an SSL context
9047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009049{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009050 if( ssl == NULL )
9051 return;
9052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009054
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009055 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056 {
Angus Grattond8213d02016-05-25 20:56:48 +10009057 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009059 }
9060
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009061 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009062 {
Angus Grattond8213d02016-05-25 20:56:48 +10009063 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009065 }
9066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009068 if( ssl->compress_buf != NULL )
9069 {
Angus Grattond8213d02016-05-25 20:56:48 +10009070 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009072 }
9073#endif
9074
Paul Bakker48916f92012-09-16 19:57:18 +00009075 if( ssl->transform )
9076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 mbedtls_ssl_transform_free( ssl->transform );
9078 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009079 }
9080
9081 if( ssl->handshake )
9082 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009083 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9085 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 mbedtls_free( ssl->handshake );
9088 mbedtls_free( ssl->transform_negotiate );
9089 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009090 }
9091
Paul Bakkerc0463502013-02-14 11:19:38 +01009092 if( ssl->session )
9093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 mbedtls_ssl_session_free( ssl->session );
9095 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009096 }
9097
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009098#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009099 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009100 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009101 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009103 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9107 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9110 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009111 }
9112#endif
9113
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009114#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009116#endif
9117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009119
Paul Bakker86f04f42013-02-14 11:20:09 +01009120 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009121 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009122}
9123
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009124/*
9125 * Initialze mbedtls_ssl_config
9126 */
9127void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9128{
9129 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9130}
9131
Simon Butcherc97b6972015-12-27 23:48:17 +00009132#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009133static int ssl_preset_default_hashes[] = {
9134#if defined(MBEDTLS_SHA512_C)
9135 MBEDTLS_MD_SHA512,
9136 MBEDTLS_MD_SHA384,
9137#endif
9138#if defined(MBEDTLS_SHA256_C)
9139 MBEDTLS_MD_SHA256,
9140 MBEDTLS_MD_SHA224,
9141#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009142#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009143 MBEDTLS_MD_SHA1,
9144#endif
9145 MBEDTLS_MD_NONE
9146};
Simon Butcherc97b6972015-12-27 23:48:17 +00009147#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009148
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009149static int ssl_preset_suiteb_ciphersuites[] = {
9150 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9151 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9152 0
9153};
9154
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009155#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009156static int ssl_preset_suiteb_hashes[] = {
9157 MBEDTLS_MD_SHA256,
9158 MBEDTLS_MD_SHA384,
9159 MBEDTLS_MD_NONE
9160};
9161#endif
9162
9163#if defined(MBEDTLS_ECP_C)
9164static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009165#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009166 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009167#endif
9168#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009169 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009170#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009171 MBEDTLS_ECP_DP_NONE
9172};
9173#endif
9174
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009175/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009176 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009177 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009178int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009179 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009180{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009181#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009182 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009183#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009184
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009185 /* Use the functions here so that they are covered in tests,
9186 * but otherwise access member directly for efficiency */
9187 mbedtls_ssl_conf_endpoint( conf, endpoint );
9188 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009189
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009190 /*
9191 * Things that are common to all presets
9192 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009193#if defined(MBEDTLS_SSL_CLI_C)
9194 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9195 {
9196 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9197#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9198 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9199#endif
9200 }
9201#endif
9202
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009203#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009204 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009205#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009206
9207#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9208 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9209#endif
9210
9211#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9212 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9213#endif
9214
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009215#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9216 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9217#endif
9218
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009219#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009220 conf->f_cookie_write = ssl_cookie_write_dummy;
9221 conf->f_cookie_check = ssl_cookie_check_dummy;
9222#endif
9223
9224#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9225 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9226#endif
9227
Janos Follath088ce432017-04-10 12:42:31 +01009228#if defined(MBEDTLS_SSL_SRV_C)
9229 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9230#endif
9231
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009232#if defined(MBEDTLS_SSL_PROTO_DTLS)
9233 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9234 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9235#endif
9236
9237#if defined(MBEDTLS_SSL_RENEGOTIATION)
9238 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009239 memset( conf->renego_period, 0x00, 2 );
9240 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009241#endif
9242
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009243#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9244 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9245 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009246 const unsigned char dhm_p[] =
9247 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9248 const unsigned char dhm_g[] =
9249 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9250
Hanno Beckera90658f2017-10-04 15:29:08 +01009251 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9252 dhm_p, sizeof( dhm_p ),
9253 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009254 {
9255 return( ret );
9256 }
9257 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009258#endif
9259
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009260 /*
9261 * Preset-specific defaults
9262 */
9263 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009264 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009265 /*
9266 * NSA Suite B
9267 */
9268 case MBEDTLS_SSL_PRESET_SUITEB:
9269 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9270 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9271 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9272 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9273
9274 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9275 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9276 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9277 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9278 ssl_preset_suiteb_ciphersuites;
9279
9280#if defined(MBEDTLS_X509_CRT_PARSE_C)
9281 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009282#endif
9283
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009284#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009285 conf->sig_hashes = ssl_preset_suiteb_hashes;
9286#endif
9287
9288#if defined(MBEDTLS_ECP_C)
9289 conf->curve_list = ssl_preset_suiteb_curves;
9290#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009291 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009292
9293 /*
9294 * Default
9295 */
9296 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009297 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9298 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9299 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9300 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9301 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9302 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9303 MBEDTLS_SSL_MIN_MINOR_VERSION :
9304 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009305 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9306 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9307
9308#if defined(MBEDTLS_SSL_PROTO_DTLS)
9309 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9310 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9311#endif
9312
9313 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9314 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9315 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9316 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9317 mbedtls_ssl_list_ciphersuites();
9318
9319#if defined(MBEDTLS_X509_CRT_PARSE_C)
9320 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9321#endif
9322
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009323#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009324 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009325#endif
9326
9327#if defined(MBEDTLS_ECP_C)
9328 conf->curve_list = mbedtls_ecp_grp_id_list();
9329#endif
9330
9331#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9332 conf->dhm_min_bitlen = 1024;
9333#endif
9334 }
9335
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009336 return( 0 );
9337}
9338
9339/*
9340 * Free mbedtls_ssl_config
9341 */
9342void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9343{
9344#if defined(MBEDTLS_DHM_C)
9345 mbedtls_mpi_free( &conf->dhm_P );
9346 mbedtls_mpi_free( &conf->dhm_G );
9347#endif
9348
9349#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9350 if( conf->psk != NULL )
9351 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009352 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009353 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009354 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009355 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009356 }
9357
9358 if( conf->psk_identity != NULL )
9359 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009360 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009361 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009362 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009363 conf->psk_identity_len = 0;
9364 }
9365#endif
9366
9367#if defined(MBEDTLS_X509_CRT_PARSE_C)
9368 ssl_key_cert_free( conf->key_cert );
9369#endif
9370
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009371 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009372}
9373
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009374#if defined(MBEDTLS_PK_C) && \
9375 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009376/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_RSA_C)
9382 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9383 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#if defined(MBEDTLS_ECDSA_C)
9386 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9387 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009390}
9391
Hanno Becker7e5437a2017-04-28 17:15:26 +01009392unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9393{
9394 switch( type ) {
9395 case MBEDTLS_PK_RSA:
9396 return( MBEDTLS_SSL_SIG_RSA );
9397 case MBEDTLS_PK_ECDSA:
9398 case MBEDTLS_PK_ECKEY:
9399 return( MBEDTLS_SSL_SIG_ECDSA );
9400 default:
9401 return( MBEDTLS_SSL_SIG_ANON );
9402 }
9403}
9404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009406{
9407 switch( sig )
9408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409#if defined(MBEDTLS_RSA_C)
9410 case MBEDTLS_SSL_SIG_RSA:
9411 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_ECDSA_C)
9414 case MBEDTLS_SSL_SIG_ECDSA:
9415 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009416#endif
9417 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009419 }
9420}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009421#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009422
Hanno Becker7e5437a2017-04-28 17:15:26 +01009423#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9424 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9425
9426/* Find an entry in a signature-hash set matching a given hash algorithm. */
9427mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9428 mbedtls_pk_type_t sig_alg )
9429{
9430 switch( sig_alg )
9431 {
9432 case MBEDTLS_PK_RSA:
9433 return( set->rsa );
9434 case MBEDTLS_PK_ECDSA:
9435 return( set->ecdsa );
9436 default:
9437 return( MBEDTLS_MD_NONE );
9438 }
9439}
9440
9441/* Add a signature-hash-pair to a signature-hash set */
9442void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9443 mbedtls_pk_type_t sig_alg,
9444 mbedtls_md_type_t md_alg )
9445{
9446 switch( sig_alg )
9447 {
9448 case MBEDTLS_PK_RSA:
9449 if( set->rsa == MBEDTLS_MD_NONE )
9450 set->rsa = md_alg;
9451 break;
9452
9453 case MBEDTLS_PK_ECDSA:
9454 if( set->ecdsa == MBEDTLS_MD_NONE )
9455 set->ecdsa = md_alg;
9456 break;
9457
9458 default:
9459 break;
9460 }
9461}
9462
9463/* Allow exactly one hash algorithm for each signature. */
9464void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9465 mbedtls_md_type_t md_alg )
9466{
9467 set->rsa = md_alg;
9468 set->ecdsa = md_alg;
9469}
9470
9471#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9472 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9473
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009474/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009475 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009478{
9479 switch( hash )
9480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481#if defined(MBEDTLS_MD5_C)
9482 case MBEDTLS_SSL_HASH_MD5:
9483 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SHA1_C)
9486 case MBEDTLS_SSL_HASH_SHA1:
9487 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009488#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489#if defined(MBEDTLS_SHA256_C)
9490 case MBEDTLS_SSL_HASH_SHA224:
9491 return( MBEDTLS_MD_SHA224 );
9492 case MBEDTLS_SSL_HASH_SHA256:
9493 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495#if defined(MBEDTLS_SHA512_C)
9496 case MBEDTLS_SSL_HASH_SHA384:
9497 return( MBEDTLS_MD_SHA384 );
9498 case MBEDTLS_SSL_HASH_SHA512:
9499 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009500#endif
9501 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009503 }
9504}
9505
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009506/*
9507 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9508 */
9509unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9510{
9511 switch( md )
9512 {
9513#if defined(MBEDTLS_MD5_C)
9514 case MBEDTLS_MD_MD5:
9515 return( MBEDTLS_SSL_HASH_MD5 );
9516#endif
9517#if defined(MBEDTLS_SHA1_C)
9518 case MBEDTLS_MD_SHA1:
9519 return( MBEDTLS_SSL_HASH_SHA1 );
9520#endif
9521#if defined(MBEDTLS_SHA256_C)
9522 case MBEDTLS_MD_SHA224:
9523 return( MBEDTLS_SSL_HASH_SHA224 );
9524 case MBEDTLS_MD_SHA256:
9525 return( MBEDTLS_SSL_HASH_SHA256 );
9526#endif
9527#if defined(MBEDTLS_SHA512_C)
9528 case MBEDTLS_MD_SHA384:
9529 return( MBEDTLS_SSL_HASH_SHA384 );
9530 case MBEDTLS_MD_SHA512:
9531 return( MBEDTLS_SSL_HASH_SHA512 );
9532#endif
9533 default:
9534 return( MBEDTLS_SSL_HASH_NONE );
9535 }
9536}
9537
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009538#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009539/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009540 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009541 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009542 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009543int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009546
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009547 if( ssl->conf->curve_list == NULL )
9548 return( -1 );
9549
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009550 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009551 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009552 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009553
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009554 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009555}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009556#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009557
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009558#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009559/*
9560 * Check if a hash proposed by the peer is in our list.
9561 * Return 0 if we're willing to use it, -1 otherwise.
9562 */
9563int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9564 mbedtls_md_type_t md )
9565{
9566 const int *cur;
9567
9568 if( ssl->conf->sig_hashes == NULL )
9569 return( -1 );
9570
9571 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9572 if( *cur == (int) md )
9573 return( 0 );
9574
9575 return( -1 );
9576}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009577#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#if defined(MBEDTLS_X509_CRT_PARSE_C)
9580int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9581 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009582 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009583 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009584{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009585 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009587 int usage = 0;
9588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009590 const char *ext_oid;
9591 size_t ext_len;
9592#endif
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9595 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009596 ((void) cert);
9597 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009598 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009599#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9602 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009603 {
9604 /* Server part of the key exchange */
9605 switch( ciphersuite->key_exchange )
9606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 case MBEDTLS_KEY_EXCHANGE_RSA:
9608 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009609 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009610 break;
9611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9613 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9614 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9615 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009616 break;
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9619 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009620 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009621 break;
9622
9623 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 case MBEDTLS_KEY_EXCHANGE_NONE:
9625 case MBEDTLS_KEY_EXCHANGE_PSK:
9626 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9627 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009628 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009629 usage = 0;
9630 }
9631 }
9632 else
9633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9635 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009636 }
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009639 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009640 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009641 ret = -1;
9642 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009643#else
9644 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9648 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9651 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009652 }
9653 else
9654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9656 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009657 }
9658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009660 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009661 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009662 ret = -1;
9663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009665
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009666 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009669
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009670/*
9671 * Convert version numbers to/from wire format
9672 * and, for DTLS, to/from TLS equivalent.
9673 *
9674 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009675 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009676 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9677 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009680 unsigned char ver[2] )
9681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682#if defined(MBEDTLS_SSL_PROTO_DTLS)
9683 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009686 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9687
9688 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9689 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9690 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009691 else
9692#else
9693 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009694#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009695 {
9696 ver[0] = (unsigned char) major;
9697 ver[1] = (unsigned char) minor;
9698 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009699}
9700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009702 const unsigned char ver[2] )
9703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704#if defined(MBEDTLS_SSL_PROTO_DTLS)
9705 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009706 {
9707 *major = 255 - ver[0] + 2;
9708 *minor = 255 - ver[1] + 1;
9709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009711 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9712 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009713 else
9714#else
9715 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009716#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009717 {
9718 *major = ver[0];
9719 *minor = ver[1];
9720 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009721}
9722
Simon Butcher99000142016-10-13 17:21:01 +01009723int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9724{
9725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9726 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9727 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9728
9729 switch( md )
9730 {
9731#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9732#if defined(MBEDTLS_MD5_C)
9733 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009734 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009735#endif
9736#if defined(MBEDTLS_SHA1_C)
9737 case MBEDTLS_SSL_HASH_SHA1:
9738 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9739 break;
9740#endif
9741#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9742#if defined(MBEDTLS_SHA512_C)
9743 case MBEDTLS_SSL_HASH_SHA384:
9744 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9745 break;
9746#endif
9747#if defined(MBEDTLS_SHA256_C)
9748 case MBEDTLS_SSL_HASH_SHA256:
9749 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9750 break;
9751#endif
9752 default:
9753 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9754 }
9755
9756 return 0;
9757#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9758 (void) ssl;
9759 (void) md;
9760
9761 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9763}
9764
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009765#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9766 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9767int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9768 unsigned char *output,
9769 unsigned char *data, size_t data_len )
9770{
9771 int ret = 0;
9772 mbedtls_md5_context mbedtls_md5;
9773 mbedtls_sha1_context mbedtls_sha1;
9774
9775 mbedtls_md5_init( &mbedtls_md5 );
9776 mbedtls_sha1_init( &mbedtls_sha1 );
9777
9778 /*
9779 * digitally-signed struct {
9780 * opaque md5_hash[16];
9781 * opaque sha_hash[20];
9782 * };
9783 *
9784 * md5_hash
9785 * MD5(ClientHello.random + ServerHello.random
9786 * + ServerParams);
9787 * sha_hash
9788 * SHA(ClientHello.random + ServerHello.random
9789 * + ServerParams);
9790 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009791 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009792 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009794 goto exit;
9795 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009796 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009797 ssl->handshake->randbytes, 64 ) ) != 0 )
9798 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009800 goto exit;
9801 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009802 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009803 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009805 goto exit;
9806 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009807 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009808 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009810 goto exit;
9811 }
9812
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009813 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009814 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009816 goto exit;
9817 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009818 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009819 ssl->handshake->randbytes, 64 ) ) != 0 )
9820 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009822 goto exit;
9823 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009824 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009825 data_len ) ) != 0 )
9826 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009828 goto exit;
9829 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009830 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009831 output + 16 ) ) != 0 )
9832 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009834 goto exit;
9835 }
9836
9837exit:
9838 mbedtls_md5_free( &mbedtls_md5 );
9839 mbedtls_sha1_free( &mbedtls_sha1 );
9840
9841 if( ret != 0 )
9842 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9843 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9844
9845 return( ret );
9846
9847}
9848#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9849 MBEDTLS_SSL_PROTO_TLS1_1 */
9850
9851#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9852 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9853int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009854 unsigned char *hash, size_t *hashlen,
9855 unsigned char *data, size_t data_len,
9856 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009857{
9858 int ret = 0;
9859 mbedtls_md_context_t ctx;
9860 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009861 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009862
9863 mbedtls_md_init( &ctx );
9864
9865 /*
9866 * digitally-signed struct {
9867 * opaque client_random[32];
9868 * opaque server_random[32];
9869 * ServerDHParams params;
9870 * };
9871 */
9872 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9873 {
9874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9875 goto exit;
9876 }
9877 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9878 {
9879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9880 goto exit;
9881 }
9882 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9883 {
9884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9885 goto exit;
9886 }
9887 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9888 {
9889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9890 goto exit;
9891 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009892 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009893 {
9894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9895 goto exit;
9896 }
9897
9898exit:
9899 mbedtls_md_free( &ctx );
9900
9901 if( ret != 0 )
9902 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9903 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9904
9905 return( ret );
9906}
9907#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9908 MBEDTLS_SSL_PROTO_TLS1_2 */
9909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009910#endif /* MBEDTLS_SSL_TLS_C */