blob: 0cc6c30b1fd5c64882e9b959bcc62728955aa25c [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é-Gonnard590b2d92020-08-25 11:18:11 +02001934
1935/*
1936 * Constant-flow memcpy from variable position in buffer.
1937 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
1938 * - but with execution flow independant from the value of offset_secret.
1939 */
1940void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
1941 const unsigned char *src_base,
1942 size_t offset_secret,
1943 size_t offset_min, size_t offset_max,
1944 size_t len )
1945{
1946 /* WIP - THIS IS NOT ACTUALLY CONSTANT-FLOW!
1947 * This is just to be able to write tests and check they work. */
1948 ssl_read_memory( src_base + offset_min, offset_max - offset_min + len );
1949 memcpy( dst, src_base + offset_secret, len );
1950}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001951#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001956 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001957#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001958 size_t padlen = 0, correct = 1;
1959#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001963 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001967 }
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001970
Paul Bakker48916f92012-09-16 19:57:18 +00001971 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001974 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1979 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001980 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001981 int ret;
1982 size_t olen = 0;
1983
Paul Bakker68884e32013-01-07 18:20:04 +01001984 padlen = 0;
1985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001987 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001988 ssl->transform_in->ivlen,
1989 ssl->in_msg, ssl->in_msglen,
1990 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001993 return( ret );
1994 }
1995
1996 if( ssl->in_msglen != olen )
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 }
Paul Bakker68884e32013-01-07 18:20:04 +01002002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002004#if defined(MBEDTLS_GCM_C) || \
2005 defined(MBEDTLS_CCM_C) || \
2006 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002008 mode == MBEDTLS_MODE_CCM ||
2009 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002010 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002011 int ret;
2012 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002013 unsigned char *dec_msg;
2014 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002015 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016 unsigned char iv[12];
2017 mbedtls_ssl_transform *transform = ssl->transform_in;
2018 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002021
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002022 /*
2023 * Compute and update sizes
2024 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002025 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002028 "+ taglen (%d)", ssl->in_msglen,
2029 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002031 }
2032 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2033
Paul Bakker68884e32013-01-07 18:20:04 +01002034 dec_msg = ssl->in_msg;
2035 dec_msg_result = ssl->in_msg;
2036 ssl->in_msglen = dec_msglen;
2037
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002038 /*
2039 * Prepare additional authenticated data
2040 */
Paul Bakker68884e32013-01-07 18:20:04 +01002041 memcpy( add_data, ssl->in_ctr, 8 );
2042 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002044 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002045 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2046 add_data[12] = ssl->in_msglen & 0xFF;
2047
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002049
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002050 /*
2051 * Prepare IV
2052 */
2053 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2054 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002055 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002056 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2057 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002058
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002059 }
2060 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2061 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002062 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 unsigned char i;
2064
2065 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2066
2067 for( i = 0; i < 8; i++ )
2068 iv[i+4] ^= ssl->in_ctr[i];
2069 }
2070 else
2071 {
2072 /* Reminder if we ever add an AEAD mode with a different size */
2073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2075 }
2076
2077 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002079
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002081 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002084 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002085 add_data, 13,
2086 dec_msg, dec_msglen,
2087 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002088 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2093 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095 return( ret );
2096 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002097 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002098
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002099 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002107#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Paul Bakker45829992013-01-03 14:52:21 +01002110 /*
2111 * Decrypt and check the padding
2112 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002113 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002114 unsigned char *dec_msg;
2115 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002116 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002117 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002118 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002119
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 /*
Paul Bakker45829992013-01-03 14:52:21 +01002121 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2124 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002125 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126#endif
Paul Bakker45829992013-01-03 14:52:21 +01002127
2128 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2129 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002132 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2133 ssl->transform_in->ivlen,
2134 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002136 }
2137
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002138 dec_msglen = ssl->in_msglen;
2139 dec_msg = ssl->in_msg;
2140 dec_msg_result = ssl->in_msg;
2141
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002142 /*
2143 * Authenticate before decrypt if enabled
2144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2146 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 {
Hanno Becker992b6872017-11-09 18:57:39 +00002148 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002149 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002152
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153 dec_msglen -= ssl->transform_in->maclen;
2154 ssl->in_msglen -= ssl->transform_in->maclen;
2155
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002156 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2157 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2158 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2159 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2164 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002165 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002166 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002170 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002171 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002172 ssl->transform_in->maclen );
2173
Hanno Becker992b6872017-11-09 18:57:39 +00002174 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2175 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002180 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002181 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002182 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002184
2185 /*
2186 * Check length sanity
2187 */
2188 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002191 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002193 }
2194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002196 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002197 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002200 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002201 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002202 dec_msglen -= ssl->transform_in->ivlen;
2203 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002204
Paul Bakker48916f92012-09-16 19:57:18 +00002205 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002206 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002211 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002212 ssl->transform_in->ivlen,
2213 dec_msg, dec_msglen,
2214 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002217 return( ret );
2218 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002219
Paul Bakkercca5b812013-08-31 17:40:26 +02002220 if( dec_msglen != olen )
2221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002224 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2227 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002228 {
2229 /*
2230 * Save IV in SSL3 and TLS1
2231 */
2232 memcpy( ssl->transform_in->iv_dec,
2233 ssl->transform_in->cipher_ctx_dec.iv,
2234 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
2238 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002239
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002240 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#if defined(MBEDTLS_SSL_DEBUG_ALL)
2244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002245 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002246#endif
Paul Bakker45829992013-01-03 14:52:21 +01002247 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002248 correct = 0;
2249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#if defined(MBEDTLS_SSL_PROTO_SSL3)
2252 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker48916f92012-09-16 19:57:18 +00002254 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_SSL_DEBUG_ALL)
2257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002259 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002260#endif
Paul Bakker45829992013-01-03 14:52:21 +01002261 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263 }
2264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2266#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2267 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2268 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 {
2270 /*
Paul Bakker45829992013-01-03 14:52:21 +01002271 * TLSv1+: always check the padding up to the first failure
2272 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002274 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002275 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002276 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002277
Paul Bakker956c9e02013-12-19 14:42:28 +01002278 /*
2279 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002280 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002281 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002282 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002283 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002284 *
2285 * In both cases we reset padding_idx to a safe value (0) to
2286 * prevent out-of-buffer reads.
2287 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002288 correct &= ( padlen <= ssl->in_msglen );
2289 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002290 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002291
2292 padding_idx *= correct;
2293
Angus Grattonb512bc12018-06-19 15:57:50 +10002294 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002295 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002296 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002297 pad_count += real_count *
2298 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2299 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002300
2301 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002304 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002306#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002307 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2311 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002315 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002316
2317 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002319 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002320#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002326#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002329#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
2331 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002332 * Authenticate if not done yet.
2333 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002335#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002336 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002337 {
Hanno Becker992b6872017-11-09 18:57:39 +00002338 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002339 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002340
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002341 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002343 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2344 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_SSL_PROTO_SSL3)
2347 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002348 {
2349 ssl_mac( &ssl->transform_in->md_ctx_dec,
2350 ssl->transform_in->mac_dec,
2351 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002352 ssl->in_ctr, ssl->in_msgtype,
2353 mac_expect );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002354 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2355 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002356 }
2357 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2359#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2360 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2361 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002362 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002363 int ret;
2364 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002365
2366 /*
2367 * The next two sizes are the minimum and maximum values of
2368 * in_msglen over all padlen values.
2369 *
2370 * They're independent of padlen, since we previously did
2371 * in_msglen -= padlen.
2372 *
2373 * Note that max_len + maclen is never more than the buffer
2374 * length, as we previously did in_msglen -= maclen too.
2375 */
2376 const size_t max_len = ssl->in_msglen + padlen;
2377 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2378
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002379 memcpy( add_data + 0, ssl->in_ctr, 8 );
2380 memcpy( add_data + 8, ssl->in_hdr, 3 );
2381 memcpy( add_data + 11, ssl->in_len, 2 );
2382
2383 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2384 add_data, sizeof( add_data ),
2385 ssl->in_msg, ssl->in_msglen,
2386 min_len, max_len,
2387 mac_expect );
2388 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002389 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
2391 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02002392 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002393
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002394 mbedtls_ssl_cf_memcpy_offset( mac_peer, ssl->in_msg,
2395 ssl->in_msglen,
2396 min_len, max_len,
2397 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002398 }
2399 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2401 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002407#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002408 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002409 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002412 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002413 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#if defined(MBEDTLS_SSL_DEBUG_ALL)
2416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002417#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002418 correct = 0;
2419 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002420 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002421 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002422
2423 /*
2424 * Finally check the correct flag
2425 */
2426 if( correct == 0 )
2427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002428#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002429
2430 /* Make extra sure authentication was performed, exactly once */
2431 if( auth_done != 1 )
2432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
2437 if( ssl->in_msglen == 0 )
2438 {
Angus Gratton34817922018-06-19 15:58:22 +10002439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2440 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2441 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2442 {
2443 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2445 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2446 }
2447#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2448
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 ssl->nb_zero++;
2450
2451 /*
2452 * Three or more empty messages may be a DoS attack
2453 * (excessive CPU consumption).
2454 */
2455 if( ssl->nb_zero > 3 )
2456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
2461 }
2462 else
2463 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002467 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002468 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002469 }
2470 else
2471#endif
2472 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002473 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002474 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2475 if( ++ssl->in_ctr[i - 1] != 0 )
2476 break;
2477
2478 /* The loop goes to its end iff the counter is wrapping */
2479 if( i == ssl_ep_len( ssl ) )
2480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2482 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002483 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002484 }
2485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 return( 0 );
2489}
2490
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002491#undef MAC_NONE
2492#undef MAC_PLAINTEXT
2493#undef MAC_CIPHERTEXT
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002496/*
2497 * Compression/decompression functions
2498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002500{
2501 int ret;
2502 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002503 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002504 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002505 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002508
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002509 if( len_pre == 0 )
2510 return( 0 );
2511
Paul Bakker2770fbd2012-07-03 13:30:23 +00002512 memcpy( msg_pre, ssl->out_msg, len_pre );
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002515 ssl->out_msglen ) );
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002518 ssl->out_msg, ssl->out_msglen );
2519
Paul Bakker48916f92012-09-16 19:57:18 +00002520 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2521 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2522 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002523 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002524
Paul Bakker48916f92012-09-16 19:57:18 +00002525 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002526 if( ret != Z_OK )
2527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2529 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002530 }
2531
Angus Grattond8213d02016-05-25 20:56:48 +10002532 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002533 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002536 ssl->out_msglen ) );
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002539 ssl->out_msg, ssl->out_msglen );
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002542
2543 return( 0 );
2544}
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002547{
2548 int ret;
2549 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002550 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002551 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002552 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002555
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002556 if( len_pre == 0 )
2557 return( 0 );
2558
Paul Bakker2770fbd2012-07-03 13:30:23 +00002559 memcpy( msg_pre, ssl->in_msg, len_pre );
2560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002562 ssl->in_msglen ) );
2563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002565 ssl->in_msg, ssl->in_msglen );
2566
Paul Bakker48916f92012-09-16 19:57:18 +00002567 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2568 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2569 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002570 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002571 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002572
Paul Bakker48916f92012-09-16 19:57:18 +00002573 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002574 if( ret != Z_OK )
2575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2577 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002578 }
2579
Angus Grattond8213d02016-05-25 20:56:48 +10002580 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002581 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002584 ssl->in_msglen ) );
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002587 ssl->in_msg, ssl->in_msglen );
2588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002590
2591 return( 0 );
2592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2596static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_PROTO_DTLS)
2599static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002600{
2601 /* If renegotiation is not enforced, retransmit until we would reach max
2602 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002603 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002604 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002605 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002606 unsigned char doublings = 1;
2607
2608 while( ratio != 0 )
2609 {
2610 ++doublings;
2611 ratio >>= 1;
2612 }
2613
2614 if( ++ssl->renego_records_seen > doublings )
2615 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002617 return( 0 );
2618 }
2619 }
2620
2621 return( ssl_write_hello_request( ssl ) );
2622}
2623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002625
Paul Bakker5121ce52009-01-03 21:22:43 +00002626/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002627 * Fill the input message buffer by appending data to it.
2628 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002629 *
2630 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2631 * available (from this read and/or a previous one). Otherwise, an error code
2632 * is returned (possibly EOF or WANT_READ).
2633 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002634 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2635 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2636 * since we always read a whole datagram at once.
2637 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002638 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002639 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002642{
Paul Bakker23986e52011-04-24 08:57:21 +00002643 int ret;
2644 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002648 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002651 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002653 }
2654
Angus Grattond8213d02016-05-25 20:56:48 +10002655 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002659 }
2660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002664 uint32_t timeout;
2665
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002666 /* Just to be sure */
2667 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2668 {
2669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2670 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2672 }
2673
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002674 /*
2675 * The point is, we need to always read a full datagram at once, so we
2676 * sometimes read more then requested, and handle the additional data.
2677 * It could be the rest of the current record (while fetching the
2678 * header) and/or some other records in the same datagram.
2679 */
2680
2681 /*
2682 * Move to the next record in the already read datagram if applicable
2683 */
2684 if( ssl->next_record_offset != 0 )
2685 {
2686 if( ssl->in_left < ssl->next_record_offset )
2687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002690 }
2691
2692 ssl->in_left -= ssl->next_record_offset;
2693
2694 if( ssl->in_left != 0 )
2695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002697 ssl->next_record_offset ) );
2698 memmove( ssl->in_hdr,
2699 ssl->in_hdr + ssl->next_record_offset,
2700 ssl->in_left );
2701 }
2702
2703 ssl->next_record_offset = 0;
2704 }
2705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002708
2709 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002710 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002711 */
2712 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002715 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002716 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002717
2718 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002719 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002720 * are not at the beginning of a new record, the caller did something
2721 * wrong.
2722 */
2723 if( ssl->in_left != 0 )
2724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002727 }
2728
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002729 /*
2730 * Don't even try to read if time's out already.
2731 * This avoids by-passing the timer when repeatedly receiving messages
2732 * that will end up being dropped.
2733 */
2734 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002735 {
2736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002737 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002738 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002739 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002740 {
Angus Grattond8213d02016-05-25 20:56:48 +10002741 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002744 timeout = ssl->handshake->retransmit_timeout;
2745 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002746 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002749
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002750 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002751 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2752 timeout );
2753 else
2754 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002757
2758 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002760 }
2761
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002762 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002765 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002768 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002769 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002772 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002773 }
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002778 return( ret );
2779 }
2780
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002781 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002784 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002786 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002787 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002790 return( ret );
2791 }
2792
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002793 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002796 }
2797
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 if( ret < 0 )
2799 return( ret );
2800
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002801 ssl->in_left = ret;
2802 }
2803 else
2804#endif
2805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002807 ssl->in_left, nb_want ) );
2808
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002809 while( ssl->in_left < nb_want )
2810 {
2811 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002812
2813 if( ssl_check_timer( ssl ) != 0 )
2814 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2815 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002816 {
2817 if( ssl->f_recv_timeout != NULL )
2818 {
2819 ret = ssl->f_recv_timeout( ssl->p_bio,
2820 ssl->in_hdr + ssl->in_left, len,
2821 ssl->conf->read_timeout );
2822 }
2823 else
2824 {
2825 ret = ssl->f_recv( ssl->p_bio,
2826 ssl->in_hdr + ssl->in_left, len );
2827 }
2828 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002831 ssl->in_left, nb_want ) );
2832 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002833
2834 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002836
2837 if( ret < 0 )
2838 return( ret );
2839
makise-homura8c3fa632020-08-24 18:33:54 +03002840 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002841 {
Darryl Green11999bb2018-03-13 15:22:58 +00002842 MBEDTLS_SSL_DEBUG_MSG( 1,
2843 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002844 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2846 }
2847
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002848 ssl->in_left += ret;
2849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 }
2851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
2854 return( 0 );
2855}
2856
2857/*
2858 * Flush any data not yet written
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002861{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002862 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002863 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002867 if( ssl->f_send == NULL )
2868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002870 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002872 }
2873
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002874 /* Avoid incrementing counter if data is flushed */
2875 if( ssl->out_left == 0 )
2876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002878 return( 0 );
2879 }
2880
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 while( ssl->out_left > 0 )
2882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2884 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Hanno Becker2b1e3542018-08-06 11:19:13 +01002886 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002887 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
2891 if( ret <= 0 )
2892 return( ret );
2893
makise-homura8c3fa632020-08-24 18:33:54 +03002894 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002895 {
Darryl Green11999bb2018-03-13 15:22:58 +00002896 MBEDTLS_SSL_DEBUG_MSG( 1,
2897 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002898 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2900 }
2901
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 ssl->out_left -= ret;
2903 }
2904
Hanno Becker2b1e3542018-08-06 11:19:13 +01002905#if defined(MBEDTLS_SSL_PROTO_DTLS)
2906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002907 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002908 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002909 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002910 else
2911#endif
2912 {
2913 ssl->out_hdr = ssl->out_buf + 8;
2914 }
2915 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
2919 return( 0 );
2920}
2921
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922/*
2923 * Functions to handle the DTLS retransmission state machine
2924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002926/*
2927 * Append current handshake message to current outgoing flight
2928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2933 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2934 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002935
2936 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002937 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002938 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002941 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002942 }
2943
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002944 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002945 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002948 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002949 }
2950
2951 /* Copy current handshake message with headers */
2952 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2953 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002954 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002955 msg->next = NULL;
2956
2957 /* Append to the current flight */
2958 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002959 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002960 else
2961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002963 while( cur->next != NULL )
2964 cur = cur->next;
2965 cur->next = msg;
2966 }
2967
Hanno Becker3b235902018-08-06 09:54:53 +01002968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002969 return( 0 );
2970}
2971
2972/*
2973 * Free the current flight of handshake messages
2974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 mbedtls_ssl_flight_item *cur = flight;
2978 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002979
2980 while( cur != NULL )
2981 {
2982 next = cur->next;
2983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 mbedtls_free( cur->p );
2985 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002986
2987 cur = next;
2988 }
2989}
2990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2992static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002993#endif
2994
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002995/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002996 * Swap transform_out and out_ctr with the alternative ones
2997 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00002998static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003001 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003002#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3003 int ret;
3004#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003005
3006 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003009 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003010 }
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003013
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003014 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003015 tmp_transform = ssl->transform_out;
3016 ssl->transform_out = ssl->handshake->alt_transform_out;
3017 ssl->handshake->alt_transform_out = tmp_transform;
3018
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003019 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003020 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3021 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003022 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003023
3024 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003025 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3028 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3033 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003034 }
3035 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003036#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3037
3038 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003039}
3040
3041/*
3042 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003043 */
3044int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3045{
3046 int ret = 0;
3047
3048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3049
3050 ret = mbedtls_ssl_flight_transmit( ssl );
3051
3052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3053
3054 return( ret );
3055}
3056
3057/*
3058 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003059 *
3060 * Need to remember the current message in case flush_output returns
3061 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003062 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003063 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003064int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003065{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003066 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003070 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003072
3073 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003074 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003075 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3076 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080
3081 while( ssl->handshake->cur_msg != NULL )
3082 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003083 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003084 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003085
Hanno Beckere1dcb032018-08-17 16:47:58 +01003086 int const is_finished =
3087 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3088 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3089
Hanno Becker04da1892018-08-14 13:22:10 +01003090 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3091 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3092
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003093 /* Swap epochs before sending Finished: we can't do it after
3094 * sending ChangeCipherSpec, in case write returns WANT_READ.
3095 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003096 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003097 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003099 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3100 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003101 }
3102
Hanno Becker67bc7c32018-08-06 11:33:50 +01003103 ret = ssl_get_remaining_payload_in_datagram( ssl );
3104 if( ret < 0 )
3105 return( ret );
3106 max_frag_len = (size_t) ret;
3107
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003108 /* CCS is copied as is, while HS messages may need fragmentation */
3109 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3110 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003111 if( max_frag_len == 0 )
3112 {
3113 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3114 return( ret );
3115
3116 continue;
3117 }
3118
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003119 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003120 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003121 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003122
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003123 /* Update position inside current message */
3124 ssl->handshake->cur_msg_p += cur->len;
3125 }
3126 else
3127 {
3128 const unsigned char * const p = ssl->handshake->cur_msg_p;
3129 const size_t hs_len = cur->len - 12;
3130 const size_t frag_off = p - ( cur->p + 12 );
3131 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003132 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003133
Hanno Beckere1dcb032018-08-17 16:47:58 +01003134 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003135 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003136 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003137 {
3138 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3139 return( ret );
3140 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003141
Hanno Becker67bc7c32018-08-06 11:33:50 +01003142 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3143 return( ret );
3144
3145 continue;
3146 }
3147 max_hs_frag_len = max_frag_len - 12;
3148
3149 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3150 max_hs_frag_len : rem_len;
3151
3152 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003153 {
3154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003155 (unsigned) cur_hs_frag_len,
3156 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003157 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003158
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003159 /* Messages are stored with handshake headers as if not fragmented,
3160 * copy beginning of headers then fill fragmentation fields.
3161 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3162 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003164 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3165 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3166 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3167
Hanno Becker67bc7c32018-08-06 11:33:50 +01003168 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3169 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3170 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003171
3172 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3173
Hanno Becker3f7b9732018-08-28 09:53:25 +01003174 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003175 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3176 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003177 ssl->out_msgtype = cur->type;
3178
3179 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003180 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003181 }
3182
3183 /* If done with the current message move to the next one if any */
3184 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3185 {
3186 if( cur->next != NULL )
3187 {
3188 ssl->handshake->cur_msg = cur->next;
3189 ssl->handshake->cur_msg_p = cur->next->p + 12;
3190 }
3191 else
3192 {
3193 ssl->handshake->cur_msg = NULL;
3194 ssl->handshake->cur_msg_p = NULL;
3195 }
3196 }
3197
3198 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003199 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202 return( ret );
3203 }
3204 }
3205
Hanno Becker67bc7c32018-08-06 11:33:50 +01003206 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3207 return( ret );
3208
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003209 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3211 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003212 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003215 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3216 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003217
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003219
3220 return( 0 );
3221}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003222
3223/*
3224 * To be called when the last message of an incoming flight is received.
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003227{
3228 /* We won't need to resend that one any more */
3229 ssl_flight_free( ssl->handshake->flight );
3230 ssl->handshake->flight = NULL;
3231 ssl->handshake->cur_msg = NULL;
3232
3233 /* The next incoming flight will start with this msg_seq */
3234 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3235
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003236 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003237 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003238
Hanno Becker0271f962018-08-16 13:23:47 +01003239 /* Clear future message buffering structure. */
3240 ssl_buffering_free( ssl );
3241
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003242 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003243 ssl_set_timer( ssl, 0 );
3244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3246 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003249 }
3250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003253
3254/*
3255 * To be called when the last message of an outgoing flight is send.
3256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003258{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003259 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003260 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3263 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003266 }
3267 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003271
Paul Bakker5121ce52009-01-03 21:22:43 +00003272/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003273 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003275
3276/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003277 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003278 *
3279 * - fill in handshake headers
3280 * - update handshake checksum
3281 * - DTLS: save message for resending
3282 * - then pass to the record layer
3283 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003284 * DTLS: except for HelloRequest, messages are only queued, and will only be
3285 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003286 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003287 * Inputs:
3288 * - ssl->out_msglen: 4 + actual handshake message len
3289 * (4 is the size of handshake headers for TLS)
3290 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3291 * - ssl->out_msg + 4: the handshake message body
3292 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003293 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003294 * - ssl->out_msglen: the length of the record contents
3295 * (including handshake headers but excluding record headers)
3296 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003297 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003298int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003300 int ret;
3301 const size_t hs_len = ssl->out_msglen - 4;
3302 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3305
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003306 /*
3307 * Sanity checks
3308 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003309 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003310 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3311 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003312 /* In SSLv3, the client might send a NoCertificate alert. */
3313#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3314 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3315 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3316 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3317#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3318 {
3319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3321 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003324 /* Whenever we send anything different from a
3325 * HelloRequest we should be in a handshake - double check. */
3326 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3327 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003328 ssl->handshake == NULL )
3329 {
3330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003341 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003343
Hanno Beckerb50a2532018-08-06 11:52:54 +01003344 /* Double-check that we did not exceed the bounds
3345 * of the outgoing record buffer.
3346 * This should never fail as the various message
3347 * writing functions must obey the bounds of the
3348 * outgoing record buffer, but better be safe.
3349 *
3350 * Note: We deliberately do not check for the MTU or MFL here.
3351 */
3352 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3353 {
3354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3355 "size %u, maximum %u",
3356 (unsigned) ssl->out_msglen,
3357 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3359 }
3360
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003361 /*
3362 * Fill handshake headers
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003366 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3367 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3368 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003370 /*
3371 * DTLS has additional fields in the Handshake layer,
3372 * between the length field and the actual payload:
3373 * uint16 message_seq;
3374 * uint24 fragment_offset;
3375 * uint24 fragment_length;
3376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003379 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003380 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003381 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003382 {
3383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3384 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003385 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003386 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3388 }
3389
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003390 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003391 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003392
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003393 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003394 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003395 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003396 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3397 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3398 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003399 }
3400 else
3401 {
3402 ssl->out_msg[4] = 0;
3403 ssl->out_msg[5] = 0;
3404 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003405
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003406 /* Handshake hashes are computed without fragmentation,
3407 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003408 memset( ssl->out_msg + 6, 0x00, 3 );
3409 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003412
Hanno Becker0207e532018-08-28 10:28:28 +01003413 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003414 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3415 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416 }
3417
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003418 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003421 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3422 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003423 {
3424 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 return( ret );
3428 }
3429 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003430 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003431#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003433 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003434 {
3435 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3436 return( ret );
3437 }
3438 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003439
3440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3441
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003442 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003443}
3444
3445/*
3446 * Record layer functions
3447 */
3448
3449/*
3450 * Write current record.
3451 *
3452 * Uses:
3453 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3454 * - ssl->out_msglen: length of the record content (excl headers)
3455 * - ssl->out_msg: record content
3456 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003457int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003458{
3459 int ret, done = 0;
3460 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003461 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462
3463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003466 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003468 {
3469 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003472 return( ret );
3473 }
3474
3475 len = ssl->out_msglen;
3476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3480 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 ret = mbedtls_ssl_hw_record_write( ssl );
3485 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3488 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003489 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003490
3491 if( ret == 0 )
3492 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003495 if( !done )
3496 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003497 unsigned i;
3498 size_t protected_record_size;
3499
Paul Bakker05ef8352012-05-08 09:17:57 +00003500 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003502 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003503
Hanno Becker19859472018-08-06 09:40:20 +01003504 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003505 ssl->out_len[0] = (unsigned char)( len >> 8 );
3506 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003507
Paul Bakker48916f92012-09-16 19:57:18 +00003508 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003509 {
3510 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003513 return( ret );
3514 }
3515
3516 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003517 ssl->out_len[0] = (unsigned char)( len >> 8 );
3518 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003519 }
3520
Hanno Becker2b1e3542018-08-06 11:19:13 +01003521 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3522
3523#if defined(MBEDTLS_SSL_PROTO_DTLS)
3524 /* In case of DTLS, double-check that we don't exceed
3525 * the remaining space in the datagram. */
3526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3527 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003528 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003529 if( ret < 0 )
3530 return( ret );
3531
3532 if( protected_record_size > (size_t) ret )
3533 {
3534 /* Should never happen */
3535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3536 }
3537 }
3538#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003541 "version = [%d:%d], msglen = %d",
3542 ssl->out_hdr[0], ssl->out_hdr[1],
3543 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003546 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003547
3548 ssl->out_left += protected_record_size;
3549 ssl->out_hdr += protected_record_size;
3550 ssl_update_out_pointers( ssl, ssl->transform_out );
3551
Hanno Becker04484622018-08-06 09:49:38 +01003552 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3553 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3554 break;
3555
3556 /* The loop goes to its end iff the counter is wrapping */
3557 if( i == ssl_ep_len( ssl ) )
3558 {
3559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3560 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 }
3563
Hanno Becker67bc7c32018-08-06 11:33:50 +01003564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3566 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003567 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003568 size_t remaining;
3569 ret = ssl_get_remaining_payload_in_datagram( ssl );
3570 if( ret < 0 )
3571 {
3572 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3573 ret );
3574 return( ret );
3575 }
3576
3577 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003578 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003579 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003580 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003581 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003582 else
3583 {
Hanno Becker513815a2018-08-20 11:56:09 +01003584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003585 }
3586 }
3587#endif /* MBEDTLS_SSL_PROTO_DTLS */
3588
3589 if( ( flush == SSL_FORCE_FLUSH ) &&
3590 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 return( ret );
3594 }
3595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
3598 return( 0 );
3599}
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003602
3603static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3604{
3605 if( ssl->in_msglen < ssl->in_hslen ||
3606 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3607 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3608 {
3609 return( 1 );
3610 }
3611 return( 0 );
3612}
Hanno Becker44650b72018-08-16 12:51:11 +01003613
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003614static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003615{
3616 return( ( ssl->in_msg[9] << 16 ) |
3617 ( ssl->in_msg[10] << 8 ) |
3618 ssl->in_msg[11] );
3619}
3620
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003621static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003622{
3623 return( ( ssl->in_msg[6] << 16 ) |
3624 ( ssl->in_msg[7] << 8 ) |
3625 ssl->in_msg[8] );
3626}
3627
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003628static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003629{
3630 uint32_t msg_len, frag_off, frag_len;
3631
3632 msg_len = ssl_get_hs_total_len( ssl );
3633 frag_off = ssl_get_hs_frag_off( ssl );
3634 frag_len = ssl_get_hs_frag_len( ssl );
3635
3636 if( frag_off > msg_len )
3637 return( -1 );
3638
3639 if( frag_len > msg_len - frag_off )
3640 return( -1 );
3641
3642 if( frag_len + 12 > ssl->in_msglen )
3643 return( -1 );
3644
3645 return( 0 );
3646}
3647
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003648/*
3649 * Mark bits in bitmask (used for DTLS HS reassembly)
3650 */
3651static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3652{
3653 unsigned int start_bits, end_bits;
3654
3655 start_bits = 8 - ( offset % 8 );
3656 if( start_bits != 8 )
3657 {
3658 size_t first_byte_idx = offset / 8;
3659
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003660 /* Special case */
3661 if( len <= start_bits )
3662 {
3663 for( ; len != 0; len-- )
3664 mask[first_byte_idx] |= 1 << ( start_bits - len );
3665
3666 /* Avoid potential issues with offset or len becoming invalid */
3667 return;
3668 }
3669
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003670 offset += start_bits; /* Now offset % 8 == 0 */
3671 len -= start_bits;
3672
3673 for( ; start_bits != 0; start_bits-- )
3674 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3675 }
3676
3677 end_bits = len % 8;
3678 if( end_bits != 0 )
3679 {
3680 size_t last_byte_idx = ( offset + len ) / 8;
3681
3682 len -= end_bits; /* Now len % 8 == 0 */
3683
3684 for( ; end_bits != 0; end_bits-- )
3685 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3686 }
3687
3688 memset( mask + offset / 8, 0xFF, len / 8 );
3689}
3690
3691/*
3692 * Check that bitmask is full
3693 */
3694static int ssl_bitmask_check( unsigned char *mask, size_t len )
3695{
3696 size_t i;
3697
3698 for( i = 0; i < len / 8; i++ )
3699 if( mask[i] != 0xFF )
3700 return( -1 );
3701
3702 for( i = 0; i < len % 8; i++ )
3703 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3704 return( -1 );
3705
3706 return( 0 );
3707}
3708
Hanno Becker56e205e2018-08-16 09:06:12 +01003709/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003710static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003711 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003712{
Hanno Becker56e205e2018-08-16 09:06:12 +01003713 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003714
Hanno Becker56e205e2018-08-16 09:06:12 +01003715 alloc_len = 12; /* Handshake header */
3716 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003717
Hanno Beckerd07df862018-08-16 09:14:58 +01003718 if( add_bitmap )
3719 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003720
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003721 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003722}
Hanno Becker56e205e2018-08-16 09:06:12 +01003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003725
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003726static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003727{
3728 return( ( ssl->in_msg[1] << 16 ) |
3729 ( ssl->in_msg[2] << 8 ) |
3730 ssl->in_msg[3] );
3731}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003732
Simon Butcher99000142016-10-13 17:21:01 +01003733int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003738 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003740 }
3741
Hanno Becker12555c62018-08-16 12:47:53 +01003742 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003745 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003746 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003750 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003751 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003752 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003753
Hanno Becker44650b72018-08-16 12:51:11 +01003754 if( ssl_check_hs_header( ssl ) != 0 )
3755 {
3756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3757 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3758 }
3759
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003760 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003761 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3762 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3763 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3764 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003765 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003766 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3767 {
3768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3769 recv_msg_seq,
3770 ssl->handshake->in_msg_seq ) );
3771 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3772 }
3773
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003774 /* Retransmit only on last message from previous flight, to avoid
3775 * too many retransmissions.
3776 * Besides, No sane server ever retransmits HelloVerifyRequest */
3777 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003781 "message_seq = %d, start_of_flight = %d",
3782 recv_msg_seq,
3783 ssl->handshake->in_flight_start_seq ) );
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003788 return( ret );
3789 }
3790 }
3791 else
3792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003794 "message_seq = %d, expected = %d",
3795 recv_msg_seq,
3796 ssl->handshake->in_msg_seq ) );
3797 }
3798
Hanno Becker90333da2017-10-10 11:27:13 +01003799 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003800 }
3801 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003802
Hanno Becker6d97ef52018-08-16 13:09:04 +01003803 /* Message reassembly is handled alongside buffering of future
3804 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003805 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003806 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003807 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003810 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003811 }
3812 }
3813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003815 /* With TLS we don't handle fragmentation (for now) */
3816 if( ssl->in_msglen < ssl->in_hslen )
3817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3819 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003820 }
3821
Simon Butcher99000142016-10-13 17:21:01 +01003822 return( 0 );
3823}
3824
3825void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3826{
Hanno Becker0271f962018-08-16 13:23:47 +01003827 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003828
Hanno Becker0271f962018-08-16 13:23:47 +01003829 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003830 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003831 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003832 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003833
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003834 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003837 ssl->handshake != NULL )
3838 {
Hanno Becker0271f962018-08-16 13:23:47 +01003839 unsigned offset;
3840 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003841
Hanno Becker0271f962018-08-16 13:23:47 +01003842 /* Increment handshake sequence number */
3843 hs->in_msg_seq++;
3844
3845 /*
3846 * Clear up handshake buffering and reassembly structure.
3847 */
3848
3849 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003850 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003851
3852 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003853 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3854 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003855 offset++, hs_buf++ )
3856 {
3857 *hs_buf = *(hs_buf + 1);
3858 }
3859
3860 /* Create a fresh last entry */
3861 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003862 }
3863#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003864}
3865
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003866/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003867 * DTLS anti-replay: RFC 6347 4.1.2.6
3868 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003869 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3870 * Bit n is set iff record number in_window_top - n has been seen.
3871 *
3872 * Usually, in_window_top is the last record number seen and the lsb of
3873 * in_window is set. The only exception is the initial state (record number 0
3874 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3877static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003878{
3879 ssl->in_window_top = 0;
3880 ssl->in_window = 0;
3881}
3882
3883static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3884{
3885 return( ( (uint64_t) buf[0] << 40 ) |
3886 ( (uint64_t) buf[1] << 32 ) |
3887 ( (uint64_t) buf[2] << 24 ) |
3888 ( (uint64_t) buf[3] << 16 ) |
3889 ( (uint64_t) buf[4] << 8 ) |
3890 ( (uint64_t) buf[5] ) );
3891}
3892
3893/*
3894 * Return 0 if sequence number is acceptable, -1 otherwise
3895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003897{
3898 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3899 uint64_t bit;
3900
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003901 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003902 return( 0 );
3903
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003904 if( rec_seqnum > ssl->in_window_top )
3905 return( 0 );
3906
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003907 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003908
3909 if( bit >= 64 )
3910 return( -1 );
3911
3912 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3913 return( -1 );
3914
3915 return( 0 );
3916}
3917
3918/*
3919 * Update replay window on new validated record
3920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003922{
3923 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3924
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003925 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003926 return;
3927
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003928 if( rec_seqnum > ssl->in_window_top )
3929 {
3930 /* Update window_top and the contents of the window */
3931 uint64_t shift = rec_seqnum - ssl->in_window_top;
3932
3933 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003934 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003935 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003936 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003937 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003938 ssl->in_window |= 1;
3939 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003940
3941 ssl->in_window_top = rec_seqnum;
3942 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003943 else
3944 {
3945 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003946 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003947
3948 if( bit < 64 ) /* Always true, but be extra sure */
3949 ssl->in_window |= (uint64_t) 1 << bit;
3950 }
3951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003953
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003954#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003955/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003956static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3957
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003958/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003959 * Without any SSL context, check if a datagram looks like a ClientHello with
3960 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003961 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003962 *
3963 * - if cookie is valid, return 0
3964 * - if ClientHello looks superficially valid but cookie is not,
3965 * fill obuf and set olen, then
3966 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3967 * - otherwise return a specific error code
3968 */
3969static int ssl_check_dtls_clihlo_cookie(
3970 mbedtls_ssl_cookie_write_t *f_cookie_write,
3971 mbedtls_ssl_cookie_check_t *f_cookie_check,
3972 void *p_cookie,
3973 const unsigned char *cli_id, size_t cli_id_len,
3974 const unsigned char *in, size_t in_len,
3975 unsigned char *obuf, size_t buf_len, size_t *olen )
3976{
3977 size_t sid_len, cookie_len;
3978 unsigned char *p;
3979
3980 if( f_cookie_write == NULL || f_cookie_check == NULL )
3981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3982
3983 /*
3984 * Structure of ClientHello with record and handshake headers,
3985 * and expected values. We don't need to check a lot, more checks will be
3986 * done when actually parsing the ClientHello - skipping those checks
3987 * avoids code duplication and does not make cookie forging any easier.
3988 *
3989 * 0-0 ContentType type; copied, must be handshake
3990 * 1-2 ProtocolVersion version; copied
3991 * 3-4 uint16 epoch; copied, must be 0
3992 * 5-10 uint48 sequence_number; copied
3993 * 11-12 uint16 length; (ignored)
3994 *
3995 * 13-13 HandshakeType msg_type; (ignored)
3996 * 14-16 uint24 length; (ignored)
3997 * 17-18 uint16 message_seq; copied
3998 * 19-21 uint24 fragment_offset; copied, must be 0
3999 * 22-24 uint24 fragment_length; (ignored)
4000 *
4001 * 25-26 ProtocolVersion client_version; (ignored)
4002 * 27-58 Random random; (ignored)
4003 * 59-xx SessionID session_id; 1 byte len + sid_len content
4004 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4005 * ...
4006 *
4007 * Minimum length is 61 bytes.
4008 */
4009 if( in_len < 61 ||
4010 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4011 in[3] != 0 || in[4] != 0 ||
4012 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4013 {
4014 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4015 }
4016
4017 sid_len = in[59];
4018 if( sid_len > in_len - 61 )
4019 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4020
4021 cookie_len = in[60 + sid_len];
4022 if( cookie_len > in_len - 60 )
4023 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4024
4025 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4026 cli_id, cli_id_len ) == 0 )
4027 {
4028 /* Valid cookie */
4029 return( 0 );
4030 }
4031
4032 /*
4033 * If we get here, we've got an invalid cookie, let's prepare HVR.
4034 *
4035 * 0-0 ContentType type; copied
4036 * 1-2 ProtocolVersion version; copied
4037 * 3-4 uint16 epoch; copied
4038 * 5-10 uint48 sequence_number; copied
4039 * 11-12 uint16 length; olen - 13
4040 *
4041 * 13-13 HandshakeType msg_type; hello_verify_request
4042 * 14-16 uint24 length; olen - 25
4043 * 17-18 uint16 message_seq; copied
4044 * 19-21 uint24 fragment_offset; copied
4045 * 22-24 uint24 fragment_length; olen - 25
4046 *
4047 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4048 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4049 *
4050 * Minimum length is 28.
4051 */
4052 if( buf_len < 28 )
4053 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4054
4055 /* Copy most fields and adapt others */
4056 memcpy( obuf, in, 25 );
4057 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4058 obuf[25] = 0xfe;
4059 obuf[26] = 0xff;
4060
4061 /* Generate and write actual cookie */
4062 p = obuf + 28;
4063 if( f_cookie_write( p_cookie,
4064 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4065 {
4066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4067 }
4068
4069 *olen = p - obuf;
4070
4071 /* Go back and fill length fields */
4072 obuf[27] = (unsigned char)( *olen - 28 );
4073
4074 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4075 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4076 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4077
4078 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4079 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4080
4081 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4082}
4083
4084/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004085 * Handle possible client reconnect with the same UDP quadruplet
4086 * (RFC 6347 Section 4.2.8).
4087 *
4088 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4089 * that looks like a ClientHello.
4090 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004091 * - if the input looks like a ClientHello without cookies,
4092 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004093 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004094 * - if the input looks like a ClientHello with a valid cookie,
4095 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004096 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004097 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004098 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004099 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004100 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4101 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004102 */
4103static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4104{
4105 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004106 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004107
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004108 ret = ssl_check_dtls_clihlo_cookie(
4109 ssl->conf->f_cookie_write,
4110 ssl->conf->f_cookie_check,
4111 ssl->conf->p_cookie,
4112 ssl->cli_id, ssl->cli_id_len,
4113 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004114 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004115
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004116 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4117
4118 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004119 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004120 int send_ret;
4121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4122 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4123 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004124 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004125 * If the error is permanent we'll catch it later,
4126 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004127 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4128 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4129 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004130
4131 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004132 }
4133
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004134 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004135 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004137 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4138 {
4139 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4140 return( ret );
4141 }
4142
4143 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004144 }
4145
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004146 return( ret );
4147}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004148#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004149
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004150/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004151 * ContentType type;
4152 * ProtocolVersion version;
4153 * uint16 epoch; // DTLS only
4154 * uint48 sequence_number; // DTLS only
4155 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004156 *
4157 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004158 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004159 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4160 *
4161 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004162 * 1. proceed with the record if this function returns 0
4163 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4164 * 3. return CLIENT_RECONNECT if this function return that value
4165 * 4. drop the whole datagram if this function returns anything else.
4166 * Point 2 is needed when the peer is resending, and we have already received
4167 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004170{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004171 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 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 +02004174
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004176 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004177 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004180 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004181 ssl->in_msgtype,
4182 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004183
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004184 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4186 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4187 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4188 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004191
4192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004193 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4194 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004195 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4196#endif /* MBEDTLS_SSL_PROTO_DTLS */
4197 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4198 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004201 }
4202
4203 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004204 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4207 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004208 }
4209
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004210 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4213 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 }
4215
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004216 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004217 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004218 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4221 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004222 }
4223
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004224 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004225 * DTLS-related tests.
4226 * Check epoch before checking length constraint because
4227 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4228 * message gets duplicated before the corresponding Finished message,
4229 * the second ChangeCipherSpec should be discarded because it belongs
4230 * to an old epoch, but not because its length is shorter than
4231 * the minimum record length for packets using the new record transform.
4232 * Note that these two kinds of failures are handled differently,
4233 * as an unexpected record is silently skipped but an invalid
4234 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004235 */
4236#if defined(MBEDTLS_SSL_PROTO_DTLS)
4237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4238 {
4239 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4240
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004241 /* Check epoch (and sequence number) with DTLS */
4242 if( rec_epoch != ssl->in_epoch )
4243 {
4244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4245 "expected %d, received %d",
4246 ssl->in_epoch, rec_epoch ) );
4247
4248#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4249 /*
4250 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4251 * access the first byte of record content (handshake type), as we
4252 * have an active transform (possibly iv_len != 0), so use the
4253 * fact that the record header len is 13 instead.
4254 */
4255 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4256 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4257 rec_epoch == 0 &&
4258 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4259 ssl->in_left > 13 &&
4260 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4261 {
4262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4263 "from the same port" ) );
4264 return( ssl_handle_possible_reconnect( ssl ) );
4265 }
4266 else
4267#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004268 {
4269 /* Consider buffering the record. */
4270 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4271 {
4272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4273 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4274 }
4275
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004276 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004277 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004278 }
4279
4280#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4281 /* Replay detection only works for the current epoch */
4282 if( rec_epoch == ssl->in_epoch &&
4283 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4284 {
4285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4286 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4287 }
4288#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004289
Hanno Becker52c6dc62017-05-26 16:07:36 +01004290 /* Drop unexpected ApplicationData records,
4291 * except at the beginning of renegotiations */
4292 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4293 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4294#if defined(MBEDTLS_SSL_RENEGOTIATION)
4295 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4296 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4297#endif
4298 )
4299 {
4300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4301 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4302 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004303 }
4304#endif /* MBEDTLS_SSL_PROTO_DTLS */
4305
Hanno Becker52c6dc62017-05-26 16:07:36 +01004306
4307 /* Check length against bounds of the current transform and version */
4308 if( ssl->transform_in == NULL )
4309 {
4310 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004311 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004312 {
4313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4314 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4315 }
4316 }
4317 else
4318 {
4319 if( ssl->in_msglen < ssl->transform_in->minlen )
4320 {
4321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4322 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4323 }
4324
4325#if defined(MBEDTLS_SSL_PROTO_SSL3)
4326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004327 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
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#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4334 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4335 /*
4336 * TLS encrypted messages can have up to 256 bytes of padding
4337 */
4338 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4339 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004340 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004341 {
4342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4343 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4344 }
4345#endif
4346 }
4347
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004348 return( 0 );
4349}
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004351/*
4352 * If applicable, decrypt (and decompress) record content
4353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004355{
4356 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4359 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4362 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 ret = mbedtls_ssl_hw_record_read( ssl );
4367 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4370 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004371 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004372
4373 if( ret == 0 )
4374 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004377 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 {
4379 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 return( ret );
4383 }
4384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 ssl->in_msg, ssl->in_msglen );
4387
Angus Grattond8213d02016-05-25 20:56:48 +10004388 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
4393 }
4394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004396 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004398 {
4399 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004402 return( ret );
4403 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004411 }
4412#endif
4413
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004414 return( 0 );
4415}
4416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004418
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004419/*
4420 * Read a record.
4421 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004422 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4423 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4424 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004425 */
Hanno Becker1097b342018-08-15 14:09:41 +01004426
4427/* Helper functions for mbedtls_ssl_read_record(). */
4428static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004429static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4430static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004431
Hanno Becker327c93b2018-08-15 13:56:18 +01004432int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004433 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004434{
4435 int ret;
4436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004438
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004439 if( ssl->keep_current_message == 0 )
4440 {
4441 do {
Simon Butcher99000142016-10-13 17:21:01 +01004442
Hanno Becker26994592018-08-15 14:14:59 +01004443 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004444 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004445 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004446
Hanno Beckere74d5562018-08-15 14:26:08 +01004447 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004448 {
Hanno Becker40f50842018-08-15 14:48:01 +01004449#if defined(MBEDTLS_SSL_PROTO_DTLS)
4450 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004451
Hanno Becker40f50842018-08-15 14:48:01 +01004452 /* We only check for buffered messages if the
4453 * current datagram is fully consumed. */
4454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004455 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004456 {
Hanno Becker40f50842018-08-15 14:48:01 +01004457 if( ssl_load_buffered_message( ssl ) == 0 )
4458 have_buffered = 1;
4459 }
4460
4461 if( have_buffered == 0 )
4462#endif /* MBEDTLS_SSL_PROTO_DTLS */
4463 {
4464 ret = ssl_get_next_record( ssl );
4465 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4466 continue;
4467
4468 if( ret != 0 )
4469 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004470 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004471 return( ret );
4472 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004473 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004474 }
4475
4476 ret = mbedtls_ssl_handle_message_type( ssl );
4477
Hanno Becker40f50842018-08-15 14:48:01 +01004478#if defined(MBEDTLS_SSL_PROTO_DTLS)
4479 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4480 {
4481 /* Buffer future message */
4482 ret = ssl_buffer_message( ssl );
4483 if( ret != 0 )
4484 return( ret );
4485
4486 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4487 }
4488#endif /* MBEDTLS_SSL_PROTO_DTLS */
4489
Hanno Becker90333da2017-10-10 11:27:13 +01004490 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4491 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004492
4493 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004494 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004495 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004496 return( ret );
4497 }
4498
Hanno Becker327c93b2018-08-15 13:56:18 +01004499 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004500 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004501 {
4502 mbedtls_ssl_update_handshake_status( ssl );
4503 }
Simon Butcher99000142016-10-13 17:21:01 +01004504 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004505 else
Simon Butcher99000142016-10-13 17:21:01 +01004506 {
Hanno Becker02f59072018-08-15 14:00:24 +01004507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004508 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004509 }
4510
4511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4512
4513 return( 0 );
4514}
4515
Hanno Becker40f50842018-08-15 14:48:01 +01004516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004517static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004518{
Hanno Becker40f50842018-08-15 14:48:01 +01004519 if( ssl->in_left > ssl->next_record_offset )
4520 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004521
Hanno Becker40f50842018-08-15 14:48:01 +01004522 return( 0 );
4523}
4524
4525static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4526{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004527 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004528 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004529 int ret = 0;
4530
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004531 if( hs == NULL )
4532 return( -1 );
4533
Hanno Beckere00ae372018-08-20 09:39:42 +01004534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4535
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004536 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4537 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4538 {
4539 /* Check if we have seen a ChangeCipherSpec before.
4540 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004541 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004542 {
4543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4544 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004545 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004546 }
4547
Hanno Becker39b8bc92018-08-28 17:17:13 +01004548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004549 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4550 ssl->in_msglen = 1;
4551 ssl->in_msg[0] = 1;
4552
4553 /* As long as they are equal, the exact value doesn't matter. */
4554 ssl->in_left = 0;
4555 ssl->next_record_offset = 0;
4556
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004557 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004558 goto exit;
4559 }
Hanno Becker37f95322018-08-16 13:55:32 +01004560
Hanno Beckerb8f50142018-08-28 10:01:34 +01004561#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004562 /* Debug only */
4563 {
4564 unsigned offset;
4565 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4566 {
4567 hs_buf = &hs->buffering.hs[offset];
4568 if( hs_buf->is_valid == 1 )
4569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4571 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004572 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004573 }
4574 }
4575 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004576#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004577
4578 /* Check if we have buffered and/or fully reassembled the
4579 * next handshake message. */
4580 hs_buf = &hs->buffering.hs[0];
4581 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4582 {
4583 /* Synthesize a record containing the buffered HS message. */
4584 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4585 ( hs_buf->data[2] << 8 ) |
4586 hs_buf->data[3];
4587
4588 /* Double-check that we haven't accidentally buffered
4589 * a message that doesn't fit into the input buffer. */
4590 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4591 {
4592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4594 }
4595
4596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4597 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4598 hs_buf->data, msg_len + 12 );
4599
4600 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4601 ssl->in_hslen = msg_len + 12;
4602 ssl->in_msglen = msg_len + 12;
4603 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4604
4605 ret = 0;
4606 goto exit;
4607 }
4608 else
4609 {
4610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4611 hs->in_msg_seq ) );
4612 }
4613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004614 ret = -1;
4615
4616exit:
4617
4618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4619 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004620}
4621
Hanno Beckera02b0b42018-08-21 17:20:27 +01004622static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4623 size_t desired )
4624{
4625 int offset;
4626 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4628 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004629
Hanno Becker01315ea2018-08-21 17:22:17 +01004630 /* Get rid of future records epoch first, if such exist. */
4631 ssl_free_buffered_record( ssl );
4632
4633 /* Check if we have enough space available now. */
4634 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4635 hs->buffering.total_bytes_buffered ) )
4636 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004638 return( 0 );
4639 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004640
Hanno Becker4f432ad2018-08-28 10:02:32 +01004641 /* We don't have enough space to buffer the next expected handshake
4642 * message. Remove buffers used for future messages to gain space,
4643 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004644 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4645 offset >= 0; offset-- )
4646 {
4647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4648 offset ) );
4649
Hanno Beckerb309b922018-08-23 13:18:05 +01004650 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004651
4652 /* Check if we have enough space available now. */
4653 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4654 hs->buffering.total_bytes_buffered ) )
4655 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004657 return( 0 );
4658 }
4659 }
4660
4661 return( -1 );
4662}
4663
Hanno Becker40f50842018-08-15 14:48:01 +01004664static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4665{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004666 int ret = 0;
4667 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4668
4669 if( hs == NULL )
4670 return( 0 );
4671
4672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4673
4674 switch( ssl->in_msgtype )
4675 {
4676 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004678
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004679 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004680 break;
4681
4682 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004683 {
4684 unsigned recv_msg_seq_offset;
4685 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4686 mbedtls_ssl_hs_buffer *hs_buf;
4687 size_t msg_len = ssl->in_hslen - 12;
4688
4689 /* We should never receive an old handshake
4690 * message - double-check nonetheless. */
4691 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4692 {
4693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4695 }
4696
4697 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4698 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4699 {
4700 /* Silently ignore -- message too far in the future */
4701 MBEDTLS_SSL_DEBUG_MSG( 2,
4702 ( "Ignore future HS message with sequence number %u, "
4703 "buffering window %u - %u",
4704 recv_msg_seq, ssl->handshake->in_msg_seq,
4705 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4706
4707 goto exit;
4708 }
4709
4710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4711 recv_msg_seq, recv_msg_seq_offset ) );
4712
4713 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4714
4715 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004716 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004717 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004718 size_t reassembly_buf_sz;
4719
Hanno Becker37f95322018-08-16 13:55:32 +01004720 hs_buf->is_fragmented =
4721 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4722
4723 /* We copy the message back into the input buffer
4724 * after reassembly, so check that it's not too large.
4725 * This is an implementation-specific limitation
4726 * and not one from the standard, hence it is not
4727 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004728 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004729 {
4730 /* Ignore message */
4731 goto exit;
4732 }
4733
Hanno Beckere0b150f2018-08-21 15:51:03 +01004734 /* Check if we have enough space to buffer the message. */
4735 if( hs->buffering.total_bytes_buffered >
4736 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4737 {
4738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4740 }
4741
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004742 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4743 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004744
4745 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4746 hs->buffering.total_bytes_buffered ) )
4747 {
4748 if( recv_msg_seq_offset > 0 )
4749 {
4750 /* If we can't buffer a future message because
4751 * of space limitations -- ignore. */
4752 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",
4753 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4754 (unsigned) hs->buffering.total_bytes_buffered ) );
4755 goto exit;
4756 }
Hanno Beckere1801392018-08-21 16:51:05 +01004757 else
4758 {
4759 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",
4760 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4761 (unsigned) hs->buffering.total_bytes_buffered ) );
4762 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004763
Hanno Beckera02b0b42018-08-21 17:20:27 +01004764 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004765 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004766 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",
4767 (unsigned) msg_len,
4768 (unsigned) reassembly_buf_sz,
4769 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004770 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004771 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4772 goto exit;
4773 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004774 }
4775
4776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4777 msg_len ) );
4778
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004779 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4780 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004781 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004782 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004783 goto exit;
4784 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004785 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004786
4787 /* Prepare final header: copy msg_type, length and message_seq,
4788 * then add standardised fragment_offset and fragment_length */
4789 memcpy( hs_buf->data, ssl->in_msg, 6 );
4790 memset( hs_buf->data + 6, 0, 3 );
4791 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4792
4793 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004794
4795 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004796 }
4797 else
4798 {
4799 /* Make sure msg_type and length are consistent */
4800 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4801 {
4802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4803 /* Ignore */
4804 goto exit;
4805 }
4806 }
4807
Hanno Becker4422bbb2018-08-20 09:40:19 +01004808 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004809 {
4810 size_t frag_len, frag_off;
4811 unsigned char * const msg = hs_buf->data + 12;
4812
4813 /*
4814 * Check and copy current fragment
4815 */
4816
4817 /* Validation of header fields already done in
4818 * mbedtls_ssl_prepare_handshake_record(). */
4819 frag_off = ssl_get_hs_frag_off( ssl );
4820 frag_len = ssl_get_hs_frag_len( ssl );
4821
4822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4823 frag_off, frag_len ) );
4824 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4825
4826 if( hs_buf->is_fragmented )
4827 {
4828 unsigned char * const bitmask = msg + msg_len;
4829 ssl_bitmask_set( bitmask, frag_off, frag_len );
4830 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4831 msg_len ) == 0 );
4832 }
4833 else
4834 {
4835 hs_buf->is_complete = 1;
4836 }
4837
4838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4839 hs_buf->is_complete ? "" : "not yet " ) );
4840 }
4841
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004842 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004843 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004844
4845 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004846 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004847 break;
4848 }
4849
4850exit:
4851
4852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4853 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004854}
4855#endif /* MBEDTLS_SSL_PROTO_DTLS */
4856
Hanno Becker1097b342018-08-15 14:09:41 +01004857static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004858{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004859 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004860 * Consume last content-layer message and potentially
4861 * update in_msglen which keeps track of the contents'
4862 * consumption state.
4863 *
4864 * (1) Handshake messages:
4865 * Remove last handshake message, move content
4866 * and adapt in_msglen.
4867 *
4868 * (2) Alert messages:
4869 * Consume whole record content, in_msglen = 0.
4870 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004871 * (3) Change cipher spec:
4872 * Consume whole record content, in_msglen = 0.
4873 *
4874 * (4) Application data:
4875 * Don't do anything - the record layer provides
4876 * the application data as a stream transport
4877 * and consumes through mbedtls_ssl_read only.
4878 *
4879 */
4880
4881 /* Case (1): Handshake messages */
4882 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004883 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004884 /* Hard assertion to be sure that no application data
4885 * is in flight, as corrupting ssl->in_msglen during
4886 * ssl->in_offt != NULL is fatal. */
4887 if( ssl->in_offt != NULL )
4888 {
4889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4891 }
4892
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004893 /*
4894 * Get next Handshake message in the current record
4895 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004896
Hanno Becker4a810fb2017-05-24 16:27:30 +01004897 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004898 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004899 * current handshake content: If DTLS handshake
4900 * fragmentation is used, that's the fragment
4901 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004902 * size here is faulty and should be changed at
4903 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004904 * (2) While it doesn't seem to cause problems, one
4905 * has to be very careful not to assume that in_hslen
4906 * is always <= in_msglen in a sensible communication.
4907 * Again, it's wrong for DTLS handshake fragmentation.
4908 * The following check is therefore mandatory, and
4909 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004910 * Additionally, ssl->in_hslen might be arbitrarily out of
4911 * bounds after handling a DTLS message with an unexpected
4912 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004913 */
4914 if( ssl->in_hslen < ssl->in_msglen )
4915 {
4916 ssl->in_msglen -= ssl->in_hslen;
4917 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4918 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004919
Hanno Becker4a810fb2017-05-24 16:27:30 +01004920 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4921 ssl->in_msg, ssl->in_msglen );
4922 }
4923 else
4924 {
4925 ssl->in_msglen = 0;
4926 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004927
Hanno Becker4a810fb2017-05-24 16:27:30 +01004928 ssl->in_hslen = 0;
4929 }
4930 /* Case (4): Application data */
4931 else if( ssl->in_offt != NULL )
4932 {
4933 return( 0 );
4934 }
4935 /* Everything else (CCS & Alerts) */
4936 else
4937 {
4938 ssl->in_msglen = 0;
4939 }
4940
Hanno Becker1097b342018-08-15 14:09:41 +01004941 return( 0 );
4942}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004943
Hanno Beckere74d5562018-08-15 14:26:08 +01004944static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4945{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004946 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004947 return( 1 );
4948
4949 return( 0 );
4950}
4951
Hanno Becker5f066e72018-08-16 14:56:31 +01004952#if defined(MBEDTLS_SSL_PROTO_DTLS)
4953
4954static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4955{
4956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4957 if( hs == NULL )
4958 return;
4959
Hanno Becker01315ea2018-08-21 17:22:17 +01004960 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004961 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004962 hs->buffering.total_bytes_buffered -=
4963 hs->buffering.future_record.len;
4964
4965 mbedtls_free( hs->buffering.future_record.data );
4966 hs->buffering.future_record.data = NULL;
4967 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004968}
4969
4970static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4971{
4972 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4973 unsigned char * rec;
4974 size_t rec_len;
4975 unsigned rec_epoch;
4976
4977 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4978 return( 0 );
4979
4980 if( hs == NULL )
4981 return( 0 );
4982
Hanno Becker5f066e72018-08-16 14:56:31 +01004983 rec = hs->buffering.future_record.data;
4984 rec_len = hs->buffering.future_record.len;
4985 rec_epoch = hs->buffering.future_record.epoch;
4986
4987 if( rec == NULL )
4988 return( 0 );
4989
Hanno Becker4cb782d2018-08-20 11:19:05 +01004990 /* Only consider loading future records if the
4991 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004992 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004993 return( 0 );
4994
Hanno Becker5f066e72018-08-16 14:56:31 +01004995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4996
4997 if( rec_epoch != ssl->in_epoch )
4998 {
4999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5000 goto exit;
5001 }
5002
5003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5004
5005 /* Double-check that the record is not too large */
5006 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5007 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5008 {
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5011 }
5012
5013 memcpy( ssl->in_hdr, rec, rec_len );
5014 ssl->in_left = rec_len;
5015 ssl->next_record_offset = 0;
5016
5017 ssl_free_buffered_record( ssl );
5018
5019exit:
5020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5021 return( 0 );
5022}
5023
5024static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5025{
5026 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5027 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005028 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005029
5030 /* Don't buffer future records outside handshakes. */
5031 if( hs == NULL )
5032 return( 0 );
5033
5034 /* Only buffer handshake records (we are only interested
5035 * in Finished messages). */
5036 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5037 return( 0 );
5038
5039 /* Don't buffer more than one future epoch record. */
5040 if( hs->buffering.future_record.data != NULL )
5041 return( 0 );
5042
Hanno Becker01315ea2018-08-21 17:22:17 +01005043 /* Don't buffer record if there's not enough buffering space remaining. */
5044 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5045 hs->buffering.total_bytes_buffered ) )
5046 {
5047 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",
5048 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5049 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005050 return( 0 );
5051 }
5052
Hanno Becker5f066e72018-08-16 14:56:31 +01005053 /* Buffer record */
5054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5055 ssl->in_epoch + 1 ) );
5056 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5057 rec_hdr_len + ssl->in_msglen );
5058
5059 /* ssl_parse_record_header() only considers records
5060 * of the next epoch as candidates for buffering. */
5061 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005062 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005063
5064 hs->buffering.future_record.data =
5065 mbedtls_calloc( 1, hs->buffering.future_record.len );
5066 if( hs->buffering.future_record.data == NULL )
5067 {
5068 /* If we run out of RAM trying to buffer a
5069 * record from the next epoch, just ignore. */
5070 return( 0 );
5071 }
5072
Hanno Becker01315ea2018-08-21 17:22:17 +01005073 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005074
Hanno Becker01315ea2018-08-21 17:22:17 +01005075 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005076 return( 0 );
5077}
5078
5079#endif /* MBEDTLS_SSL_PROTO_DTLS */
5080
Hanno Beckere74d5562018-08-15 14:26:08 +01005081static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005082{
5083 int ret;
5084
Hanno Becker5f066e72018-08-16 14:56:31 +01005085#if defined(MBEDTLS_SSL_PROTO_DTLS)
5086 /* We might have buffered a future record; if so,
5087 * and if the epoch matches now, load it.
5088 * On success, this call will set ssl->in_left to
5089 * the length of the buffered record, so that
5090 * the calls to ssl_fetch_input() below will
5091 * essentially be no-ops. */
5092 ret = ssl_load_buffered_record( ssl );
5093 if( ret != 0 )
5094 return( ret );
5095#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005100 return( ret );
5101 }
5102
5103 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5107 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005108 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005109 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5110 {
5111 ret = ssl_buffer_future_record( ssl );
5112 if( ret != 0 )
5113 return( ret );
5114
5115 /* Fall through to handling of unexpected records */
5116 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5117 }
5118
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005119 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5120 {
5121 /* Skip unexpected record (but not whole datagram) */
5122 ssl->next_record_offset = ssl->in_msglen
5123 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005124
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5126 "(header)" ) );
5127 }
5128 else
5129 {
5130 /* Skip invalid record and the rest of the datagram */
5131 ssl->next_record_offset = 0;
5132 ssl->in_left = 0;
5133
5134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5135 "(header)" ) );
5136 }
5137
5138 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005139 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005140 }
5141#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005142 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005143 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005144
5145 /*
5146 * Read and optionally decrypt the message contents
5147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5149 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005152 return( ret );
5153 }
5154
5155 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005160 if( ssl->next_record_offset < ssl->in_left )
5161 {
5162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5163 }
5164 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005165 else
5166#endif
5167 ssl->in_left = 0;
5168
5169 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005173 {
5174 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5176 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005177 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005178 /* Except when waiting for Finished as a bad mac here
5179 * probably means something went wrong in the handshake
5180 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5181 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5182 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5183 {
5184#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5185 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5186 {
5187 mbedtls_ssl_send_alert_message( ssl,
5188 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5189 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5190 }
5191#endif
5192 return( ret );
5193 }
5194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005196 if( ssl->conf->badmac_limit != 0 &&
5197 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5200 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005201 }
5202#endif
5203
Hanno Becker4a810fb2017-05-24 16:27:30 +01005204 /* As above, invalid records cause
5205 * dismissal of the whole datagram. */
5206
5207 ssl->next_record_offset = 0;
5208 ssl->in_left = 0;
5209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005211 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005212 }
5213
5214 return( ret );
5215 }
5216 else
5217#endif
5218 {
5219 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5221 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 mbedtls_ssl_send_alert_message( ssl,
5224 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5225 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005226 }
5227#endif
5228 return( ret );
5229 }
5230 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005231
Simon Butcher99000142016-10-13 17:21:01 +01005232 return( 0 );
5233}
5234
5235int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5236{
5237 int ret;
5238
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005239 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005240 * Handle particular types of records
5241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 {
Simon Butcher99000142016-10-13 17:21:01 +01005244 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5245 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005246 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005248 }
5249
Hanno Beckere678eaa2018-08-21 14:57:46 +01005250 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005252 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005253 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5255 ssl->in_msglen ) );
5256 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005257 }
5258
Hanno Beckere678eaa2018-08-21 14:57:46 +01005259 if( ssl->in_msg[0] != 1 )
5260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5262 ssl->in_msg[0] ) );
5263 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5264 }
5265
5266#if defined(MBEDTLS_SSL_PROTO_DTLS)
5267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5268 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5269 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5270 {
5271 if( ssl->handshake == NULL )
5272 {
5273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5274 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5275 }
5276
5277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5278 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5279 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005280#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005281 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005284 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005285 if( ssl->in_msglen != 2 )
5286 {
5287 /* Note: Standard allows for more than one 2 byte alert
5288 to be packed in a single message, but Mbed TLS doesn't
5289 currently support this. */
5290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5291 ssl->in_msglen ) );
5292 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5293 }
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005296 ssl->in_msg[0], ssl->in_msg[1] ) );
5297
5298 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005299 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005304 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005306 }
5307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5309 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5312 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005314
5315#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5316 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5317 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5318 {
Hanno Becker90333da2017-10-10 11:27:13 +01005319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005320 /* Will be handled when trying to parse ServerHello */
5321 return( 0 );
5322 }
5323#endif
5324
5325#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5327 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5328 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5329 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5330 {
5331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5332 /* Will be handled in mbedtls_ssl_parse_certificate() */
5333 return( 0 );
5334 }
5335#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5336
5337 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005338 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 }
5340
Hanno Beckerc76c6192017-06-06 10:03:17 +01005341#if defined(MBEDTLS_SSL_PROTO_DTLS)
5342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5343 ssl->handshake != NULL &&
5344 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5345 {
5346 ssl_handshake_wrapup_free_hs_transform( ssl );
5347 }
5348#endif
5349
Paul Bakker5121ce52009-01-03 21:22:43 +00005350 return( 0 );
5351}
5352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005354{
5355 int ret;
5356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5358 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5359 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005360 {
5361 return( ret );
5362 }
5363
5364 return( 0 );
5365}
5366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005368 unsigned char level,
5369 unsigned char message )
5370{
5371 int ret;
5372
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005373 if( ssl == NULL || ssl->conf == NULL )
5374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005380 ssl->out_msglen = 2;
5381 ssl->out_msg[0] = level;
5382 ssl->out_msg[1] = message;
5383
Hanno Becker67bc7c32018-08-06 11:33:50 +01005384 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005387 return( ret );
5388 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005390
5391 return( 0 );
5392}
5393
Paul Bakker5121ce52009-01-03 21:22:43 +00005394/*
5395 * Handshake functions
5396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5398 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5399 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5400 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5401 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5402 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5403 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005404/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5412 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005413 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5414 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005417 ssl->state++;
5418 return( 0 );
5419 }
5420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005423}
5424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5432 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005433 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5434 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005437 ssl->state++;
5438 return( 0 );
5439 }
5440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005443}
Gilles Peskinef9828522017-05-03 12:28:43 +02005444
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005445#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005446/* Some certificate support -> implement write and parse */
5447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005451 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 const mbedtls_x509_crt *crt;
5453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5458 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005459 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5460 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005463 ssl->state++;
5464 return( 0 );
5465 }
5466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 {
5470 if( ssl->client_auth == 0 )
5471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 ssl->state++;
5474 return( 0 );
5475 }
5476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 /*
5479 * If using SSLv3 and got no cert, send an Alert message
5480 * (otherwise an empty Certificate message will be sent).
5481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5483 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 {
5485 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5487 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5488 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 goto write_msg;
5492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495#endif /* MBEDTLS_SSL_CLI_C */
5496#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005497 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5502 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 }
5504 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
5509 /*
5510 * 0 . 0 handshake type
5511 * 1 . 3 handshake length
5512 * 4 . 6 length of all certs
5513 * 7 . 9 length of cert. 1
5514 * 10 . n-1 peer certificate
5515 * n . n+2 length of cert. 2
5516 * n+3 . ... upper level cert, etc.
5517 */
5518 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005520
Paul Bakker29087132010-03-21 21:03:34 +00005521 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
5523 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005524 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005527 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 }
5530
5531 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5532 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5533 ssl->out_msg[i + 2] = (unsigned char)( n );
5534
5535 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5536 i += n; crt = crt->next;
5537 }
5538
5539 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5540 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5541 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5542
5543 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5545 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005546
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005547#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005548write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
5551 ssl->state++;
5552
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005553 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 return( ret );
5557 }
5558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005560
Paul Bakkered27a042013-04-18 22:46:23 +02005561 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562}
5563
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005564/*
5565 * Once the certificate message is read, parse it into a cert chain and
5566 * perform basic checks, but leave actual verification to the caller
5567 */
5568static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005569{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005570 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005571 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005572 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#if defined(MBEDTLS_SSL_SRV_C)
5575#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 /*
5577 * Check if the client sent an empty certificate
5578 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005579 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005582 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5584 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5585 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005589 /* The client was asked for a certificate but didn't send
5590 one. The client should know what's going on, so we
5591 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005592 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005593 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 }
5595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5599 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005600 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5604 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5605 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5606 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005609
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005610 /* The client was asked for a certificate but didn't send
5611 one. The client should know what's going on, so we
5612 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005613 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005614 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005615 }
5616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5618 MBEDTLS_SSL_PROTO_TLS1_2 */
5619#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005624 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5625 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005627 }
5628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5630 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005633 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5634 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 }
5637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005639
Paul Bakker5121ce52009-01-03 21:22:43 +00005640 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005643 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005644
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005645 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5650 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 }
5653
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005654 /* In case we tried to reuse a session but it failed */
5655 if( ssl->session_negotiate->peer_cert != NULL )
5656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5658 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005659 }
5660
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005661 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005666 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5667 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005668 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 }
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005672
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005673 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005674
5675 while( i < ssl->in_hslen )
5676 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005677 if ( i + 3 > ssl->in_hslen ) {
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5680 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5681 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 if( ssl->in_msg[i] != 0 )
5684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005686 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5687 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 }
5690
5691 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5692 | (unsigned int) ssl->in_msg[i + 2];
5693 i += 3;
5694
5695 if( n < 128 || i + n > ssl->in_hslen )
5696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5699 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 }
5702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005704 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005705 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005706 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005707 case 0: /*ok*/
5708 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5709 /* Ignore certificate with an unknown algorithm: maybe a
5710 prior certificate was already trusted. */
5711 break;
5712
5713 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5714 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5715 goto crt_parse_der_failed;
5716
5717 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5718 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5719 goto crt_parse_der_failed;
5720
5721 default:
5722 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5723 crt_parse_der_failed:
5724 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 return( ret );
5727 }
5728
5729 i += n;
5730 }
5731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005733
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005734 /*
5735 * On client, make sure the server cert doesn't change during renego to
5736 * avoid "triple handshake" attack: https://secure-resumption.com/
5737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005741 {
5742 if( ssl->session->peer_cert == NULL )
5743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5746 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005748 }
5749
5750 if( ssl->session->peer_cert->raw.len !=
5751 ssl->session_negotiate->peer_cert->raw.len ||
5752 memcmp( ssl->session->peer_cert->raw.p,
5753 ssl->session_negotiate->peer_cert->raw.p,
5754 ssl->session->peer_cert->raw.len ) != 0 )
5755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5758 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005760 }
5761 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005763
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005764 return( 0 );
5765}
5766
5767int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5768{
5769 int ret;
5770 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5771 ssl->transform_negotiate->ciphersuite_info;
5772#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5773 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5774 ? ssl->handshake->sni_authmode
5775 : ssl->conf->authmode;
5776#else
5777 const int authmode = ssl->conf->authmode;
5778#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005779 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005780
5781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5782
5783 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5784 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5785 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5786 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5787 {
5788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5789 ssl->state++;
5790 return( 0 );
5791 }
5792
5793#if defined(MBEDTLS_SSL_SRV_C)
5794 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5795 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5796 {
5797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5798 ssl->state++;
5799 return( 0 );
5800 }
5801
5802 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5803 authmode == MBEDTLS_SSL_VERIFY_NONE )
5804 {
5805 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005807
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005808 ssl->state++;
5809 return( 0 );
5810 }
5811#endif
5812
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005813#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5814 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005815 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005816 {
5817 goto crt_verify;
5818 }
5819#endif
5820
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005821 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005822 {
5823 /* mbedtls_ssl_read_record may have sent an alert already. We
5824 let it decide whether to alert. */
5825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5826 return( ret );
5827 }
5828
5829 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5830 {
5831#if defined(MBEDTLS_SSL_SRV_C)
5832 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5833 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5834 {
5835 ret = 0;
5836 }
5837#endif
5838
5839 ssl->state++;
5840 return( ret );
5841 }
5842
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005843#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5844 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005845 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005846
5847crt_verify:
5848 if( ssl->handshake->ecrs_enabled)
5849 rs_ctx = &ssl->handshake->ecrs_ctx;
5850#endif
5851
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005852 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005853 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005854 mbedtls_x509_crt *ca_chain;
5855 mbedtls_x509_crl *ca_crl;
5856
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005857#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005858 if( ssl->handshake->sni_ca_chain != NULL )
5859 {
5860 ca_chain = ssl->handshake->sni_ca_chain;
5861 ca_crl = ssl->handshake->sni_ca_crl;
5862 }
5863 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005864#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005865 {
5866 ca_chain = ssl->conf->ca_chain;
5867 ca_crl = ssl->conf->ca_crl;
5868 }
5869
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005870 /*
5871 * Main check: verify certificate
5872 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005873 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005874 ssl->session_negotiate->peer_cert,
5875 ca_chain, ca_crl,
5876 ssl->conf->cert_profile,
5877 ssl->hostname,
5878 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005879 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880
5881 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005884 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005885
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005886#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5887 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005888 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005889#endif
5890
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005891 /*
5892 * Secondary checks: always done, but change 'ret' only if it was 0
5893 */
5894
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005895#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005898
5899 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005901 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005902 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005903 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005906 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005908 }
5909 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005910#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005913 ciphersuite_info,
5914 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005915 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005918 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005920 }
5921
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005922 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5923 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5924 * with details encoded in the verification flags. All other kinds
5925 * of error codes, including those from the user provided f_vrfy
5926 * functions, are treated as fatal and lead to a failure of
5927 * ssl_parse_certificate even if verification was optional. */
5928 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5929 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5930 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5931 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005932 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005933 }
5934
5935 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5936 {
5937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5938 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5939 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005940
5941 if( ret != 0 )
5942 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005943 uint8_t alert;
5944
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005945 /* The certificate may have been rejected for several reasons.
5946 Pick one and send the corresponding alert. Which alert to send
5947 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005948 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5949 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5950 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5951 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5952 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5953 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5954 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5955 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5956 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5957 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5958 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5959 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5960 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5961 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5962 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5963 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5964 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5965 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5966 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5967 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005968 else
5969 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5971 alert );
5972 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005973
Hanno Beckere6706e62017-05-15 16:05:15 +01005974#if defined(MBEDTLS_DEBUG_C)
5975 if( ssl->session_negotiate->verify_result != 0 )
5976 {
5977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5978 ssl->session_negotiate->verify_result ) );
5979 }
5980 else
5981 {
5982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5983 }
5984#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005985 }
5986
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005987 ssl->state++;
5988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005990
5991 return( ret );
5992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5994 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5995 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5996 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5997 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5998 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5999 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006002{
6003 int ret;
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006008 ssl->out_msglen = 1;
6009 ssl->out_msg[0] = 1;
6010
Paul Bakker5121ce52009-01-03 21:22:43 +00006011 ssl->state++;
6012
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006013 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 return( ret );
6017 }
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
6021 return( 0 );
6022}
6023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006025{
6026 int ret;
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
Hanno Becker327c93b2018-08-15 13:56:18 +01006030 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 return( ret );
6034 }
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6040 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 }
6043
Hanno Beckere678eaa2018-08-21 14:57:46 +01006044 /* CCS records are only accepted if they have length 1 and content '1',
6045 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006046
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006047 /*
6048 * Switch to our negotiated transform and session parameters for inbound
6049 * data.
6050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006052 ssl->transform_in = ssl->transform_negotiate;
6053 ssl->session_in = ssl->session_negotiate;
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006059 ssl_dtls_replay_reset( ssl );
6060#endif
6061
6062 /* Increment epoch */
6063 if( ++ssl->in_epoch == 0 )
6064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006066 /* This is highly unlikely to happen for legitimate reasons, so
6067 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006069 }
6070 }
6071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006073 memset( ssl->in_ctr, 0, 8 );
6074
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006075 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6078 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6084 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006086 }
6087 }
6088#endif
6089
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 ssl->state++;
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006093
6094 return( 0 );
6095}
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6098 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006099{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006100 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6103 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6104 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006105 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006106 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6109#if defined(MBEDTLS_SHA512_C)
6110 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006111 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6112 else
6113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SHA256_C)
6115 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006116 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006117 else
6118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006122 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006123 }
Paul Bakker380da532012-04-18 16:10:25 +00006124}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6129 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006130 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6131 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6134#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006135 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006138 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006141}
6142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006144 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6147 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006148 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6149 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6152#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006153 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006156 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006159}
6160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6162 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6163static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006164 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006165{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006166 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6167 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006168}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006169#endif
Paul Bakker380da532012-04-18 16:10:25 +00006170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6172#if defined(MBEDTLS_SHA256_C)
6173static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006174 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006175{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006176 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006177}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006178#endif
Paul Bakker380da532012-04-18 16:10:25 +00006179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180#if defined(MBEDTLS_SHA512_C)
6181static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006182 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006183{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006184 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006185}
Paul Bakker769075d2012-11-24 11:26:46 +01006186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006190static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006192{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006193 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006194 mbedtls_md5_context md5;
6195 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006196
Paul Bakker5121ce52009-01-03 21:22:43 +00006197 unsigned char padbuf[48];
6198 unsigned char md5sum[16];
6199 unsigned char sha1sum[20];
6200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006202 if( !session )
6203 session = ssl->session;
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006206
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006207 mbedtls_md5_init( &md5 );
6208 mbedtls_sha1_init( &sha1 );
6209
6210 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6211 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212
6213 /*
6214 * SSLv3:
6215 * hash =
6216 * MD5( master + pad2 +
6217 * MD5( handshake + sender + master + pad1 ) )
6218 * + SHA1( master + pad2 +
6219 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 */
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006223 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6224 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006228 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6229 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006233 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006234
Paul Bakker1ef83d62012-04-11 12:09:53 +00006235 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006236
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006237 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6238 mbedtls_md5_update_ret( &md5, session->master, 48 );
6239 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6240 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006242 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6243 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6244 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6245 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Paul Bakker1ef83d62012-04-11 12:09:53 +00006247 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006249 mbedtls_md5_starts_ret( &md5 );
6250 mbedtls_md5_update_ret( &md5, session->master, 48 );
6251 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6252 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6253 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006255 mbedtls_sha1_starts_ret( &sha1 );
6256 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6257 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6258 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6259 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006262
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006263 mbedtls_md5_free( &md5 );
6264 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006266 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6267 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6268 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006271}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006275static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006277{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006278 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006279 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006280 mbedtls_md5_context md5;
6281 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006282 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006285 if( !session )
6286 session = ssl->session;
6287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006289
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006290 mbedtls_md5_init( &md5 );
6291 mbedtls_sha1_init( &sha1 );
6292
6293 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6294 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006295
Paul Bakker1ef83d62012-04-11 12:09:53 +00006296 /*
6297 * TLSv1:
6298 * hash = PRF( master, finished_label,
6299 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6300 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006303 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6304 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006305#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006308 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6309 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006310#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006313 ? "client finished"
6314 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006315
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006316 mbedtls_md5_finish_ret( &md5, padbuf );
6317 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006318
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006319 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006320 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006323
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006324 mbedtls_md5_free( &md5 );
6325 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006326
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006327 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6334#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006335static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006337{
6338 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006339 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006340 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006341 unsigned char padbuf[32];
6342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006344 if( !session )
6345 session = ssl->session;
6346
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006347 mbedtls_sha256_init( &sha256 );
6348
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006350
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006351 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006352
6353 /*
6354 * TLSv1.2:
6355 * hash = PRF( master, finished_label,
6356 * Hash( handshake ) )[0.11]
6357 */
6358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359#if !defined(MBEDTLS_SHA256_ALT)
6360 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006361 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006362#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006365 ? "client finished"
6366 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006367
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006368 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006369
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006370 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006371 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006374
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006375 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006376
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006377 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006384static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006386{
6387 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006388 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006389 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006390 unsigned char padbuf[48];
6391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006393 if( !session )
6394 session = ssl->session;
6395
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006396 mbedtls_sha512_init( &sha512 );
6397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006399
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006400 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006401
6402 /*
6403 * TLSv1.2:
6404 * hash = PRF( master, finished_label,
6405 * Hash( handshake ) )[0.11]
6406 */
6407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006409 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6410 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006411#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006414 ? "client finished"
6415 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006416
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006417 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006418
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006419 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006420 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006423
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006424 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006425
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006426 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430#endif /* MBEDTLS_SHA512_C */
6431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006436
6437 /*
6438 * Free our handshake params
6439 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006440 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006442 ssl->handshake = NULL;
6443
6444 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006445 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006446 */
6447 if( ssl->transform )
6448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 mbedtls_ssl_transform_free( ssl->transform );
6450 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006451 }
6452 ssl->transform = ssl->transform_negotiate;
6453 ssl->transform_negotiate = NULL;
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006456}
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006459{
6460 int resume = ssl->handshake->resume;
6461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_RENEGOTIATION)
6465 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006468 ssl->renego_records_seen = 0;
6469 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006470#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006471
6472 /*
6473 * Free the previous session and switch in the current one
6474 */
Paul Bakker0a597072012-09-25 21:55:46 +00006475 if( ssl->session )
6476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006478 /* RFC 7366 3.1: keep the EtM state */
6479 ssl->session_negotiate->encrypt_then_mac =
6480 ssl->session->encrypt_then_mac;
6481#endif
6482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 mbedtls_ssl_session_free( ssl->session );
6484 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006485 }
6486 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006487 ssl->session_negotiate = NULL;
6488
Paul Bakker0a597072012-09-25 21:55:46 +00006489 /*
6490 * Add cache entry
6491 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006492 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006493 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006494 resume == 0 )
6495 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006496 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006498 }
Paul Bakker0a597072012-09-25 21:55:46 +00006499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006502 ssl->handshake->flight != NULL )
6503 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006504 /* Cancel handshake timer */
6505 ssl_set_timer( ssl, 0 );
6506
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006507 /* Keep last flight around in case we need to resend it:
6508 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006510 }
6511 else
6512#endif
6513 ssl_handshake_wrapup_free_hs_transform( ssl );
6514
Paul Bakker48916f92012-09-16 19:57:18 +00006515 ssl->state++;
6516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006518}
6519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006521{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006522 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006525
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006526 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006527
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006528 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006529
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006530 /*
6531 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6532 * may define some other value. Currently (early 2016), no defined
6533 * ciphersuite does this (and this is unlikely to change as activity has
6534 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006539 ssl->verify_data_len = hash_len;
6540 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006541#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006542
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6545 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
6547 /*
6548 * In case of session resuming, invert the client and server
6549 * ChangeCipherSpec messages order.
6550 */
Paul Bakker0a597072012-09-25 21:55:46 +00006551 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006554 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006558 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006561 }
6562 else
6563 ssl->state++;
6564
Paul Bakker48916f92012-09-16 19:57:18 +00006565 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006566 * Switch to our negotiated transform and session parameters for outbound
6567 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006573 {
6574 unsigned char i;
6575
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006576 /* Remember current epoch settings for resending */
6577 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006578 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006579
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006580 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006581 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006582
6583 /* Increment epoch */
6584 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006585 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006586 break;
6587
6588 /* The loop goes to its end iff the counter is wrapping */
6589 if( i == 0 )
6590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6592 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006593 }
6594 }
6595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006597 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006598
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006599 ssl->transform_out = ssl->transform_negotiate;
6600 ssl->session_out = ssl->session_negotiate;
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6603 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6608 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006609 }
6610 }
6611#endif
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006616#endif
6617
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006618 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 return( ret );
6622 }
6623
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006624#if defined(MBEDTLS_SSL_PROTO_DTLS)
6625 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6626 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6627 {
6628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6629 return( ret );
6630 }
6631#endif
6632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634
6635 return( 0 );
6636}
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006639#define SSL_MAX_HASH_LEN 36
6640#else
6641#define SSL_MAX_HASH_LEN 12
6642#endif
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006645{
Paul Bakker23986e52011-04-24 08:57:21 +00006646 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006647 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006648 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006652 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
Hanno Becker327c93b2018-08-15 13:56:18 +01006654 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006657 return( ret );
6658 }
6659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6664 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 }
6667
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006668 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669#if defined(MBEDTLS_SSL_PROTO_SSL3)
6670 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006671 hash_len = 36;
6672 else
6673#endif
6674 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6677 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006680 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6681 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 }
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006686 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006689 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6690 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 }
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006695 ssl->verify_data_len = hash_len;
6696 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006697#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006698
Paul Bakker0a597072012-09-25 21:55:46 +00006699 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006702 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006706 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 }
6710 else
6711 ssl->state++;
6712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006714 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006716#endif
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006719
6720 return( 0 );
6721}
6722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6728 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6729 mbedtls_md5_init( &handshake->fin_md5 );
6730 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006731 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6732 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6735#if defined(MBEDTLS_SHA256_C)
6736 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006737 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SHA512_C)
6740 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006741 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006744
6745 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006746
6747#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6748 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6749 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6750#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#if defined(MBEDTLS_DHM_C)
6753 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_ECDH_C)
6756 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006757#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006758#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006759 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006760#if defined(MBEDTLS_SSL_CLI_C)
6761 handshake->ecjpake_cache = NULL;
6762 handshake->ecjpake_cache_len = 0;
6763#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006764#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006765
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006766#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006767 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006768#endif
6769
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006770#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6771 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6772#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006773}
6774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6780 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 mbedtls_md_init( &transform->md_ctx_enc );
6783 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006784}
6785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006789}
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006792{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006793 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006794 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006796 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006798 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006799 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006800
6801 /*
6802 * Either the pointers are now NULL or cleared properly and can be freed.
6803 * Now allocate missing structures.
6804 */
6805 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006806 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006807 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006808 }
Paul Bakker48916f92012-09-16 19:57:18 +00006809
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006810 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006811 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006812 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006813 }
Paul Bakker48916f92012-09-16 19:57:18 +00006814
Paul Bakker82788fb2014-10-20 13:59:19 +02006815 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006816 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006817 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006818 }
Paul Bakker48916f92012-09-16 19:57:18 +00006819
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006820 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006821 if( ssl->handshake == NULL ||
6822 ssl->transform_negotiate == NULL ||
6823 ssl->session_negotiate == NULL )
6824 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 mbedtls_free( ssl->handshake );
6828 mbedtls_free( ssl->transform_negotiate );
6829 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006830
6831 ssl->handshake = NULL;
6832 ssl->transform_negotiate = NULL;
6833 ssl->session_negotiate = NULL;
6834
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006835 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006836 }
6837
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006838 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006840 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006841 ssl_handshake_params_init( ssl->handshake );
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006844 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6845 {
6846 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006847
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006848 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6849 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6850 else
6851 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006852
6853 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006854 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006855#endif
6856
Paul Bakker48916f92012-09-16 19:57:18 +00006857 return( 0 );
6858}
6859
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006860#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006861/* Dummy cookie callbacks for defaults */
6862static int ssl_cookie_write_dummy( void *ctx,
6863 unsigned char **p, unsigned char *end,
6864 const unsigned char *cli_id, size_t cli_id_len )
6865{
6866 ((void) ctx);
6867 ((void) p);
6868 ((void) end);
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}
6874
6875static int ssl_cookie_check_dummy( void *ctx,
6876 const unsigned char *cookie, size_t cookie_len,
6877 const unsigned char *cli_id, size_t cli_id_len )
6878{
6879 ((void) ctx);
6880 ((void) cookie);
6881 ((void) cookie_len);
6882 ((void) cli_id);
6883 ((void) cli_id_len);
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006886}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006887#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006888
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006889/* Once ssl->out_hdr as the address of the beginning of the
6890 * next outgoing record is set, deduce the other pointers.
6891 *
6892 * Note: For TLS, we save the implicit record sequence number
6893 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6894 * and the caller has to make sure there's space for this.
6895 */
6896
6897static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6898 mbedtls_ssl_transform *transform )
6899{
6900#if defined(MBEDTLS_SSL_PROTO_DTLS)
6901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6902 {
6903 ssl->out_ctr = ssl->out_hdr + 3;
6904 ssl->out_len = ssl->out_hdr + 11;
6905 ssl->out_iv = ssl->out_hdr + 13;
6906 }
6907 else
6908#endif
6909 {
6910 ssl->out_ctr = ssl->out_hdr - 8;
6911 ssl->out_len = ssl->out_hdr + 3;
6912 ssl->out_iv = ssl->out_hdr + 5;
6913 }
6914
6915 /* Adjust out_msg to make space for explicit IV, if used. */
6916 if( transform != NULL &&
6917 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6918 {
6919 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6920 }
6921 else
6922 ssl->out_msg = ssl->out_iv;
6923}
6924
6925/* Once ssl->in_hdr as the address of the beginning of the
6926 * next incoming record is set, deduce the other pointers.
6927 *
6928 * Note: For TLS, we save the implicit record sequence number
6929 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6930 * and the caller has to make sure there's space for this.
6931 */
6932
6933static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6934 mbedtls_ssl_transform *transform )
6935{
6936#if defined(MBEDTLS_SSL_PROTO_DTLS)
6937 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6938 {
6939 ssl->in_ctr = ssl->in_hdr + 3;
6940 ssl->in_len = ssl->in_hdr + 11;
6941 ssl->in_iv = ssl->in_hdr + 13;
6942 }
6943 else
6944#endif
6945 {
6946 ssl->in_ctr = ssl->in_hdr - 8;
6947 ssl->in_len = ssl->in_hdr + 3;
6948 ssl->in_iv = ssl->in_hdr + 5;
6949 }
6950
6951 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6952 if( transform != NULL &&
6953 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6954 {
6955 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6956 }
6957 else
6958 ssl->in_msg = ssl->in_iv;
6959}
6960
Paul Bakker5121ce52009-01-03 21:22:43 +00006961/*
6962 * Initialize an SSL context
6963 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006964void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6965{
6966 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6967}
6968
6969/*
6970 * Setup an SSL context
6971 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006972
6973static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6974{
6975 /* Set the incoming and outgoing record pointers. */
6976#if defined(MBEDTLS_SSL_PROTO_DTLS)
6977 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6978 {
6979 ssl->out_hdr = ssl->out_buf;
6980 ssl->in_hdr = ssl->in_buf;
6981 }
6982 else
6983#endif /* MBEDTLS_SSL_PROTO_DTLS */
6984 {
6985 ssl->out_hdr = ssl->out_buf + 8;
6986 ssl->in_hdr = ssl->in_buf + 8;
6987 }
6988
6989 /* Derive other internal pointers. */
6990 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6991 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6992}
6993
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006994int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006995 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006996{
Paul Bakker48916f92012-09-16 19:57:18 +00006997 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006998
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006999 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007000
7001 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007002 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007003 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007004
7005 /* Set to NULL in case of an error condition */
7006 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007007
Angus Grattond8213d02016-05-25 20:56:48 +10007008 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7009 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007010 {
Angus Grattond8213d02016-05-25 20:56:48 +10007011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007012 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007013 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007014 }
7015
7016 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7017 if( ssl->out_buf == NULL )
7018 {
7019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007020 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007021 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007022 }
7023
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007024 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007025
Paul Bakker48916f92012-09-16 19:57:18 +00007026 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007027 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
7029 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007030
7031error:
7032 mbedtls_free( ssl->in_buf );
7033 mbedtls_free( ssl->out_buf );
7034
7035 ssl->conf = NULL;
7036
7037 ssl->in_buf = NULL;
7038 ssl->out_buf = NULL;
7039
7040 ssl->in_hdr = NULL;
7041 ssl->in_ctr = NULL;
7042 ssl->in_len = NULL;
7043 ssl->in_iv = NULL;
7044 ssl->in_msg = NULL;
7045
7046 ssl->out_hdr = NULL;
7047 ssl->out_ctr = NULL;
7048 ssl->out_len = NULL;
7049 ssl->out_iv = NULL;
7050 ssl->out_msg = NULL;
7051
k-stachowiak9f7798e2018-07-31 16:52:32 +02007052 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007053}
7054
7055/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007056 * Reset an initialized and used SSL context for re-use while retaining
7057 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007058 *
7059 * If partial is non-zero, keep data in the input buffer and client ID.
7060 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007061 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007062static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007063{
Paul Bakker48916f92012-09-16 19:57:18 +00007064 int ret;
7065
Hanno Becker7e772132018-08-10 12:38:21 +01007066#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7067 !defined(MBEDTLS_SSL_SRV_C)
7068 ((void) partial);
7069#endif
7070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007072
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007073 /* Cancel any possibly running timer */
7074 ssl_set_timer( ssl, 0 );
7075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076#if defined(MBEDTLS_SSL_RENEGOTIATION)
7077 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007078 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007079
7080 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7082 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007085
Paul Bakker7eb013f2011-10-06 12:37:39 +00007086 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007087 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007088
7089 ssl->in_msgtype = 0;
7090 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007092 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007093 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007096 ssl_dtls_replay_reset( ssl );
7097#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007098
7099 ssl->in_hslen = 0;
7100 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007101
7102 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007103
7104 ssl->out_msgtype = 0;
7105 ssl->out_msglen = 0;
7106 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7108 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007109 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007110#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007111
Hanno Becker19859472018-08-06 09:40:20 +01007112 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7113
Paul Bakker48916f92012-09-16 19:57:18 +00007114 ssl->transform_in = NULL;
7115 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007116
Hanno Becker78640902018-08-13 16:35:15 +01007117 ssl->session_in = NULL;
7118 ssl->session_out = NULL;
7119
Angus Grattond8213d02016-05-25 20:56:48 +10007120 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007121
7122#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007123 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007124#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7125 {
7126 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007127 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007128 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7131 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7134 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7137 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007138 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007139 }
7140#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007141
Paul Bakker48916f92012-09-16 19:57:18 +00007142 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 mbedtls_ssl_transform_free( ssl->transform );
7145 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007146 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007147 }
Paul Bakker48916f92012-09-16 19:57:18 +00007148
Paul Bakkerc0463502013-02-14 11:19:38 +01007149 if( ssl->session )
7150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 mbedtls_ssl_session_free( ssl->session );
7152 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007153 ssl->session = NULL;
7154 }
7155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007157 ssl->alpn_chosen = NULL;
7158#endif
7159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007161#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007162 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007163#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007164 {
7165 mbedtls_free( ssl->cli_id );
7166 ssl->cli_id = NULL;
7167 ssl->cli_id_len = 0;
7168 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007169#endif
7170
Paul Bakker48916f92012-09-16 19:57:18 +00007171 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7172 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007173
7174 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007175}
7176
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007177/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007178 * Reset an initialized and used SSL context for re-use while retaining
7179 * all application-set variables, function pointers and data.
7180 */
7181int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7182{
7183 return( ssl_session_reset_int( ssl, 0 ) );
7184}
7185
7186/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007187 * SSL set accessors
7188 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007189void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007191 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007192}
7193
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007194void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007196 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007197}
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007200void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007201{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007202 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007203}
7204#endif
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007207void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007208{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007209 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007210}
7211#endif
7212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007214
Hanno Becker1841b0a2018-08-24 11:13:57 +01007215void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7216 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007217{
7218 ssl->disable_datagram_packing = !allow_packing;
7219}
7220
7221void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7222 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007224 conf->hs_timeout_min = min;
7225 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007226}
7227#endif
7228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007229void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007230{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007231 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007232}
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007235void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007237 void *p_vrfy )
7238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007239 conf->f_vrfy = f_vrfy;
7240 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007244void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007245 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 void *p_rng )
7247{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007248 conf->f_rng = f_rng;
7249 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007250}
7251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007252void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007253 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007254 void *p_dbg )
7255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007256 conf->f_dbg = f_dbg;
7257 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007258}
7259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007261 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007262 mbedtls_ssl_send_t *f_send,
7263 mbedtls_ssl_recv_t *f_recv,
7264 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007265{
7266 ssl->p_bio = p_bio;
7267 ssl->f_send = f_send;
7268 ssl->f_recv = f_recv;
7269 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007270}
7271
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007272#if defined(MBEDTLS_SSL_PROTO_DTLS)
7273void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7274{
7275 ssl->mtu = mtu;
7276}
7277#endif
7278
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007279void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007280{
7281 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007282}
7283
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007284void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7285 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007286 mbedtls_ssl_set_timer_t *f_set_timer,
7287 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007288{
7289 ssl->p_timer = p_timer;
7290 ssl->f_set_timer = f_set_timer;
7291 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007292
7293 /* Make sure we start with no timer running */
7294 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007295}
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007298void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007299 void *p_cache,
7300 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7301 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007302{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007303 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007304 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007305 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#if defined(MBEDTLS_SSL_CLI_C)
7310int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007311{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007312 int ret;
7313
7314 if( ssl == NULL ||
7315 session == NULL ||
7316 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007317 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007320 }
7321
7322 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7323 return( ret );
7324
Paul Bakker0a597072012-09-25 21:55:46 +00007325 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007326
7327 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007330
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007331void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007332 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007334 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7335 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7336 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7337 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007338}
7339
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007340void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007341 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007342 int major, int minor )
7343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007345 return;
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007348 return;
7349
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007350 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007351}
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007354void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007355 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007356{
7357 conf->cert_profile = profile;
7358}
7359
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007360/* Append a new keycert entry to a (possibly empty) list */
7361static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7362 mbedtls_x509_crt *cert,
7363 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007364{
niisato8ee24222018-06-25 19:05:48 +09007365 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007366
niisato8ee24222018-06-25 19:05:48 +09007367 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7368 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007370
niisato8ee24222018-06-25 19:05:48 +09007371 new_cert->cert = cert;
7372 new_cert->key = key;
7373 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007374
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007375 /* Update head is the list was null, else add to the end */
7376 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007377 {
niisato8ee24222018-06-25 19:05:48 +09007378 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007379 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007380 else
7381 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007382 mbedtls_ssl_key_cert *cur = *head;
7383 while( cur->next != NULL )
7384 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007385 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007386 }
7387
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007388 return( 0 );
7389}
7390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007391int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007392 mbedtls_x509_crt *own_cert,
7393 mbedtls_pk_context *pk_key )
7394{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007395 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007396}
7397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007398void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007399 mbedtls_x509_crt *ca_chain,
7400 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007401{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007402 conf->ca_chain = ca_chain;
7403 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007406
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007407#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7408int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7409 mbedtls_x509_crt *own_cert,
7410 mbedtls_pk_context *pk_key )
7411{
7412 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7413 own_cert, pk_key ) );
7414}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007415
7416void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7417 mbedtls_x509_crt *ca_chain,
7418 mbedtls_x509_crl *ca_crl )
7419{
7420 ssl->handshake->sni_ca_chain = ca_chain;
7421 ssl->handshake->sni_ca_crl = ca_crl;
7422}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007423
7424void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7425 int authmode )
7426{
7427 ssl->handshake->sni_authmode = authmode;
7428}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007429#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7430
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007431#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007432/*
7433 * Set EC J-PAKE password for current handshake
7434 */
7435int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7436 const unsigned char *pw,
7437 size_t pw_len )
7438{
7439 mbedtls_ecjpake_role role;
7440
Janos Follath8eb64132016-06-03 15:40:57 +01007441 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7443
7444 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7445 role = MBEDTLS_ECJPAKE_SERVER;
7446 else
7447 role = MBEDTLS_ECJPAKE_CLIENT;
7448
7449 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7450 role,
7451 MBEDTLS_MD_SHA256,
7452 MBEDTLS_ECP_DP_SECP256R1,
7453 pw, pw_len ) );
7454}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007455#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007458int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007459 const unsigned char *psk, size_t psk_len,
7460 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007461{
Paul Bakker6db455e2013-09-18 17:29:31 +02007462 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007467
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007468 /* Identity len will be encoded on two bytes */
7469 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007470 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007471 {
7472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7473 }
7474
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007475 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007476 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007477 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007478
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007479 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007480 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007481 conf->psk_len = 0;
7482 }
7483 if( conf->psk_identity != NULL )
7484 {
7485 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007486 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007487 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007488 }
7489
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007490 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7491 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007492 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007493 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007494 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007495 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007496 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007497 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007498 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007499
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007500 conf->psk_len = psk_len;
7501 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007502
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007503 memcpy( conf->psk, psk, conf->psk_len );
7504 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007505
7506 return( 0 );
7507}
7508
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007509int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7510 const unsigned char *psk, size_t psk_len )
7511{
7512 if( psk == NULL || ssl->handshake == NULL )
7513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7514
7515 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7517
7518 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007519 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007520 mbedtls_platform_zeroize( ssl->handshake->psk,
7521 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007522 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007523 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007524 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007525
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007526 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007527 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007528
7529 ssl->handshake->psk_len = psk_len;
7530 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7531
7532 return( 0 );
7533}
7534
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007535void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007537 size_t),
7538 void *p_psk )
7539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007540 conf->f_psk = f_psk;
7541 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007544
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007545#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007546
7547#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007548int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007549{
7550 int ret;
7551
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007552 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7553 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7554 {
7555 mbedtls_mpi_free( &conf->dhm_P );
7556 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007557 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007559
7560 return( 0 );
7561}
Hanno Becker470a8c42017-10-04 15:28:46 +01007562#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007563
Hanno Beckera90658f2017-10-04 15:29:08 +01007564int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7565 const unsigned char *dhm_P, size_t P_len,
7566 const unsigned char *dhm_G, size_t G_len )
7567{
7568 int ret;
7569
7570 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7571 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7572 {
7573 mbedtls_mpi_free( &conf->dhm_P );
7574 mbedtls_mpi_free( &conf->dhm_G );
7575 return( ret );
7576 }
7577
7578 return( 0 );
7579}
Paul Bakker5121ce52009-01-03 21:22:43 +00007580
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007581int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007582{
7583 int ret;
7584
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007585 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7586 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7587 {
7588 mbedtls_mpi_free( &conf->dhm_P );
7589 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007590 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007591 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007592
7593 return( 0 );
7594}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007595#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007596
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007597#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7598/*
7599 * Set the minimum length for Diffie-Hellman parameters
7600 */
7601void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7602 unsigned int bitlen )
7603{
7604 conf->dhm_min_bitlen = bitlen;
7605}
7606#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7607
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007608#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007609/*
7610 * Set allowed/preferred hashes for handshake signatures
7611 */
7612void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7613 const int *hashes )
7614{
7615 conf->sig_hashes = hashes;
7616}
Hanno Becker947194e2017-04-07 13:25:49 +01007617#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007618
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007619#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007620/*
7621 * Set the allowed elliptic curves
7622 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007623void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007624 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007626 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007627}
Hanno Becker947194e2017-04-07 13:25:49 +01007628#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007629
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007630#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007632{
Hanno Becker947194e2017-04-07 13:25:49 +01007633 /* Initialize to suppress unnecessary compiler warning */
7634 size_t hostname_len = 0;
7635
7636 /* Check if new hostname is valid before
7637 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007638 if( hostname != NULL )
7639 {
7640 hostname_len = strlen( hostname );
7641
7642 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7644 }
7645
7646 /* Now it's clear that we will overwrite the old hostname,
7647 * so we can free it safely */
7648
7649 if( ssl->hostname != NULL )
7650 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007651 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007652 mbedtls_free( ssl->hostname );
7653 }
7654
7655 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007656
Paul Bakker5121ce52009-01-03 21:22:43 +00007657 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007658 {
7659 ssl->hostname = NULL;
7660 }
7661 else
7662 {
7663 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007664 if( ssl->hostname == NULL )
7665 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007666
Hanno Becker947194e2017-04-07 13:25:49 +01007667 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007668
Hanno Becker947194e2017-04-07 13:25:49 +01007669 ssl->hostname[hostname_len] = '\0';
7670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007671
7672 return( 0 );
7673}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007674#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007675
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007677void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007679 const unsigned char *, size_t),
7680 void *p_sni )
7681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007682 conf->f_sni = f_sni;
7683 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007688int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007689{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007690 size_t cur_len, tot_len;
7691 const char **p;
7692
7693 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007694 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7695 * MUST NOT be truncated."
7696 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007697 */
7698 tot_len = 0;
7699 for( p = protos; *p != NULL; p++ )
7700 {
7701 cur_len = strlen( *p );
7702 tot_len += cur_len;
7703
Ronald Cron157cffe2020-04-23 16:41:44 +02007704 if( ( cur_len == 0 ) ||
7705 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7706 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007708 }
7709
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007710 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007711
7712 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007713}
7714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007716{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007717 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007720
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007721void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007722{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007723 conf->max_major_ver = major;
7724 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007725}
7726
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007727void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007729 conf->min_major_ver = major;
7730 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007731}
7732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007734void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007735{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007736 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007737}
7738#endif
7739
Janos Follath088ce432017-04-10 12:42:31 +01007740#if defined(MBEDTLS_SSL_SRV_C)
7741void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7742 char cert_req_ca_list )
7743{
7744 conf->cert_req_ca_list = cert_req_ca_list;
7745}
7746#endif
7747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007749void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007750{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007751 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007752}
7753#endif
7754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007756void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007758 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007759}
7760#endif
7761
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007762#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007763void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007765 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007766}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007767#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007770int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007773 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007776 }
7777
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007778 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007779
7780 return( 0 );
7781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007785void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007787 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007792void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007793{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007794 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007795}
7796#endif
7797
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007798void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007799{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007800 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007801}
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007804void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007806 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007807}
7808
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007809void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007810{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007811 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007812}
7813
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007814void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007815 const unsigned char period[8] )
7816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007817 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007822#if defined(MBEDTLS_SSL_CLI_C)
7823void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007824{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007825 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007826}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007827#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007828
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007829#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007830void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7831 mbedtls_ssl_ticket_write_t *f_ticket_write,
7832 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7833 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007834{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007835 conf->f_ticket_write = f_ticket_write;
7836 conf->f_ticket_parse = f_ticket_parse;
7837 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007838}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007841
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007842#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7843void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7844 mbedtls_ssl_export_keys_t *f_export_keys,
7845 void *p_export_keys )
7846{
7847 conf->f_export_keys = f_export_keys;
7848 conf->p_export_keys = p_export_keys;
7849}
7850#endif
7851
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007852#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007853void mbedtls_ssl_conf_async_private_cb(
7854 mbedtls_ssl_config *conf,
7855 mbedtls_ssl_async_sign_t *f_async_sign,
7856 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7857 mbedtls_ssl_async_resume_t *f_async_resume,
7858 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007859 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007860{
7861 conf->f_async_sign_start = f_async_sign;
7862 conf->f_async_decrypt_start = f_async_decrypt;
7863 conf->f_async_resume = f_async_resume;
7864 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007865 conf->p_async_config_data = async_config_data;
7866}
7867
Gilles Peskine8f97af72018-04-26 11:46:10 +02007868void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7869{
7870 return( conf->p_async_config_data );
7871}
7872
Gilles Peskine1febfef2018-04-30 11:54:39 +02007873void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007874{
7875 if( ssl->handshake == NULL )
7876 return( NULL );
7877 else
7878 return( ssl->handshake->user_async_ctx );
7879}
7880
Gilles Peskine1febfef2018-04-30 11:54:39 +02007881void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007882 void *ctx )
7883{
7884 if( ssl->handshake != NULL )
7885 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007886}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007887#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007888
Paul Bakker5121ce52009-01-03 21:22:43 +00007889/*
7890 * SSL get accessors
7891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007893{
7894 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7895}
7896
Hanno Becker8b170a02017-10-10 11:51:19 +01007897int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7898{
7899 /*
7900 * Case A: We're currently holding back
7901 * a message for further processing.
7902 */
7903
7904 if( ssl->keep_current_message == 1 )
7905 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007907 return( 1 );
7908 }
7909
7910 /*
7911 * Case B: Further records are pending in the current datagram.
7912 */
7913
7914#if defined(MBEDTLS_SSL_PROTO_DTLS)
7915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7916 ssl->in_left > ssl->next_record_offset )
7917 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007919 return( 1 );
7920 }
7921#endif /* MBEDTLS_SSL_PROTO_DTLS */
7922
7923 /*
7924 * Case C: A handshake message is being processed.
7925 */
7926
Hanno Becker8b170a02017-10-10 11:51:19 +01007927 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7928 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007930 return( 1 );
7931 }
7932
7933 /*
7934 * Case D: An application data message is being processed
7935 */
7936 if( ssl->in_offt != NULL )
7937 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007939 return( 1 );
7940 }
7941
7942 /*
7943 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007944 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007945 * we implement support for multiple alerts in single records.
7946 */
7947
7948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7949 return( 0 );
7950}
7951
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007952uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007953{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007954 if( ssl->session != NULL )
7955 return( ssl->session->verify_result );
7956
7957 if( ssl->session_negotiate != NULL )
7958 return( ssl->session_negotiate->verify_result );
7959
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007960 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007961}
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007964{
Paul Bakker926c8e42013-03-06 10:23:34 +01007965 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007966 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007969}
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007975 {
7976 switch( ssl->minor_ver )
7977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007979 return( "DTLSv1.0" );
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007982 return( "DTLSv1.2" );
7983
7984 default:
7985 return( "unknown (DTLS)" );
7986 }
7987 }
7988#endif
7989
Paul Bakker43ca69c2011-01-15 17:35:19 +00007990 switch( ssl->minor_ver )
7991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007993 return( "SSLv3.0" );
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007996 return( "TLSv1.0" );
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007999 return( "TLSv1.1" );
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008002 return( "TLSv1.2" );
8003
Paul Bakker43ca69c2011-01-15 17:35:19 +00008004 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008005 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008006 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008007}
8008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008010{
Hanno Becker3136ede2018-08-17 15:28:19 +01008011 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008013 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008014
Hanno Becker78640902018-08-13 16:35:15 +01008015 if( transform == NULL )
8016 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#if defined(MBEDTLS_ZLIB_SUPPORT)
8019 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8020 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008021#endif
8022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 case MBEDTLS_MODE_GCM:
8026 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008027 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008029 transform_expansion = transform->minlen;
8030 break;
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008033
8034 block_size = mbedtls_cipher_get_block_size(
8035 &transform->cipher_ctx_enc );
8036
Hanno Becker3136ede2018-08-17 15:28:19 +01008037 /* Expansion due to the addition of the MAC. */
8038 transform_expansion += transform->maclen;
8039
8040 /* Expansion due to the addition of CBC padding;
8041 * Theoretically up to 256 bytes, but we never use
8042 * more than the block size of the underlying cipher. */
8043 transform_expansion += block_size;
8044
8045 /* For TLS 1.1 or higher, an explicit IV is added
8046 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008047#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8048 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008049 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008050#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008051
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008052 break;
8053
8054 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008057 }
8058
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008059 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008060}
8061
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008062#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8063size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8064{
8065 size_t max_len;
8066
8067 /*
8068 * Assume mfl_code is correct since it was checked when set
8069 */
Angus Grattond8213d02016-05-25 20:56:48 +10008070 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008071
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008072 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008073 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008074 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008075 {
Angus Grattond8213d02016-05-25 20:56:48 +10008076 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008077 }
8078
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008079 /* During a handshake, use the value being negotiated */
8080 if( ssl->session_negotiate != NULL &&
8081 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8082 {
8083 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8084 }
8085
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008086 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008087}
8088#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8089
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008090#if defined(MBEDTLS_SSL_PROTO_DTLS)
8091static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8092{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008093 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8094 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8095 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8096 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8097 return ( 0 );
8098
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008099 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8100 return( ssl->mtu );
8101
8102 if( ssl->mtu == 0 )
8103 return( ssl->handshake->mtu );
8104
8105 return( ssl->mtu < ssl->handshake->mtu ?
8106 ssl->mtu : ssl->handshake->mtu );
8107}
8108#endif /* MBEDTLS_SSL_PROTO_DTLS */
8109
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008110int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8111{
8112 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8113
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008114#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8115 !defined(MBEDTLS_SSL_PROTO_DTLS)
8116 (void) ssl;
8117#endif
8118
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008119#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8120 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8121
8122 if( max_len > mfl )
8123 max_len = mfl;
8124#endif
8125
8126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008127 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008128 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008129 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008130 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8131 const size_t overhead = (size_t) ret;
8132
8133 if( ret < 0 )
8134 return( ret );
8135
8136 if( mtu <= overhead )
8137 {
8138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8139 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8140 }
8141
8142 if( max_len > mtu - overhead )
8143 max_len = mtu - overhead;
8144 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008145#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008146
Hanno Becker0defedb2018-08-10 12:35:02 +01008147#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8148 !defined(MBEDTLS_SSL_PROTO_DTLS)
8149 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008150#endif
8151
8152 return( (int) max_len );
8153}
8154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_X509_CRT_PARSE_C)
8156const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008157{
8158 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008159 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008160
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008161 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_CLI_C)
8166int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008167{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008168 if( ssl == NULL ||
8169 dst == NULL ||
8170 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008171 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008174 }
8175
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008176 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008179
Paul Bakker5121ce52009-01-03 21:22:43 +00008180/*
Paul Bakker1961b702013-01-25 14:49:24 +01008181 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008186
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008187 if( ssl == NULL || ssl->conf == NULL )
8188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008191 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008197#endif
8198
Paul Bakker1961b702013-01-25 14:49:24 +01008199 return( ret );
8200}
8201
8202/*
8203 * Perform the SSL handshake
8204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008206{
8207 int ret = 0;
8208
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008209 if( ssl == NULL || ssl->conf == NULL )
8210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008217
8218 if( ret != 0 )
8219 break;
8220 }
8221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008223
8224 return( ret );
8225}
8226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#if defined(MBEDTLS_SSL_RENEGOTIATION)
8228#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008229/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008230 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008233{
8234 int ret;
8235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008237
8238 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8240 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008241
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008242 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008243 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008245 return( ret );
8246 }
8247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008249
8250 return( 0 );
8251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008253
8254/*
8255 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 * - any side: calling mbedtls_ssl_renegotiate(),
8257 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8258 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008259 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008260 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008264{
8265 int ret;
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008268
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008269 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8270 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008271
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008272 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8273 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008277 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008278 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008279 ssl->handshake->out_msg_seq = 1;
8280 else
8281 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008282 }
8283#endif
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8286 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008291 return( ret );
8292 }
8293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008295
8296 return( 0 );
8297}
8298
8299/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008300 * Renegotiate current connection on client,
8301 * or request renegotiation on server
8302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008306
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008307 if( ssl == NULL || ssl->conf == NULL )
8308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008311 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008312 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008318
8319 /* Did we already try/start sending HelloRequest? */
8320 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008322
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008323 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008328 /*
8329 * On client, either start the renegotiation process or,
8330 * if already in progress, continue the handshake
8331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008336
8337 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008340 return( ret );
8341 }
8342 }
8343 else
8344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008348 return( ret );
8349 }
8350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008352
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008353 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008354}
8355
8356/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008357 * Check record counters and renegotiate if they're above the limit.
8358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008360{
Andres AG2196c7f2016-12-15 17:01:16 +00008361 size_t ep_len = ssl_ep_len( ssl );
8362 int in_ctr_cmp;
8363 int out_ctr_cmp;
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8366 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008367 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008368 {
8369 return( 0 );
8370 }
8371
Andres AG2196c7f2016-12-15 17:01:16 +00008372 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8373 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008374 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008375 ssl->conf->renego_period + ep_len, 8 - ep_len );
8376
8377 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008378 {
8379 return( 0 );
8380 }
8381
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008386
8387/*
8388 * Receive application data decrypted from the SSL layer
8389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008391{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008392 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008393 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008394
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008395 if( ssl == NULL || ssl->conf == NULL )
8396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008404 return( ret );
8405
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008406 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008408 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008409 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008410 return( ret );
8411 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008412 }
8413#endif
8414
Hanno Becker4a810fb2017-05-24 16:27:30 +01008415 /*
8416 * Check if renegotiation is necessary and/or handshake is
8417 * in process. If yes, perform/continue, and fall through
8418 * if an unexpected packet is received while the client
8419 * is waiting for the ServerHello.
8420 *
8421 * (There is no equivalent to the last condition on
8422 * the server-side as it is not treated as within
8423 * a handshake while waiting for the ClientHello
8424 * after a renegotiation request.)
8425 */
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008428 ret = ssl_check_ctr_renegotiate( ssl );
8429 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8430 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008433 return( ret );
8434 }
8435#endif
8436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008440 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8441 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008444 return( ret );
8445 }
8446 }
8447
Hanno Beckere41158b2017-10-23 13:30:32 +01008448 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008449 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008451 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008452 if( ssl->f_get_timer != NULL &&
8453 ssl->f_get_timer( ssl->p_timer ) == -1 )
8454 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008455 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008456 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008457
Hanno Becker327c93b2018-08-15 13:56:18 +01008458 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008459 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008460 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8461 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008462
Hanno Becker4a810fb2017-05-24 16:27:30 +01008463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8464 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008465 }
8466
8467 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008469 {
8470 /*
8471 * OpenSSL sends empty messages to randomize the IV
8472 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008473 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008476 return( 0 );
8477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008479 return( ret );
8480 }
8481 }
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008486
Hanno Becker4a810fb2017-05-24 16:27:30 +01008487 /*
8488 * - For client-side, expect SERVER_HELLO_REQUEST.
8489 * - For server-side, expect CLIENT_HELLO.
8490 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8491 */
8492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008494 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008496 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008499
8500 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008503 {
8504 continue;
8505 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008508 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008509#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008510
Hanno Becker4a810fb2017-05-24 16:27:30 +01008511#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008512 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008516
8517 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008520 {
8521 continue;
8522 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008525 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008526#endif /* MBEDTLS_SSL_SRV_C */
8527
Hanno Becker21df7f92017-10-17 11:03:26 +01008528#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008529 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008530 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8531 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8532 ssl->conf->allow_legacy_renegotiation ==
8533 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8534 {
8535 /*
8536 * Accept renegotiation request
8537 */
Paul Bakker48916f92012-09-16 19:57:18 +00008538
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008539 /* DTLS clients need to know renego is server-initiated */
8540#if defined(MBEDTLS_SSL_PROTO_DTLS)
8541 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8542 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8543 {
8544 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8545 }
8546#endif
8547 ret = ssl_start_renegotiation( ssl );
8548 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8549 ret != 0 )
8550 {
8551 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8552 return( ret );
8553 }
8554 }
8555 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008556#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008557 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008558 /*
8559 * Refuse renegotiation
8560 */
8561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_SSL_PROTO_SSL3)
8565 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008566 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008567 /* SSLv3 does not have a "no_renegotiation" warning, so
8568 we send a fatal alert and abort the connection. */
8569 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8570 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8571 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008572 }
8573 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8575#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8576 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8577 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8580 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8581 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008582 {
8583 return( ret );
8584 }
Paul Bakker48916f92012-09-16 19:57:18 +00008585 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008586 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8588 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008592 }
Paul Bakker48916f92012-09-16 19:57:18 +00008593 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008594
Hanno Becker90333da2017-10-10 11:27:13 +01008595 /* At this point, we don't know whether the renegotiation has been
8596 * completed or not. The cases to consider are the following:
8597 * 1) The renegotiation is complete. In this case, no new record
8598 * has been read yet.
8599 * 2) The renegotiation is incomplete because the client received
8600 * an application data record while awaiting the ServerHello.
8601 * 3) The renegotiation is incomplete because the client received
8602 * a non-handshake, non-application data message while awaiting
8603 * the ServerHello.
8604 * In each of these case, looping will be the proper action:
8605 * - For 1), the next iteration will read a new record and check
8606 * if it's application data.
8607 * - For 2), the loop condition isn't satisfied as application data
8608 * is present, hence continue is the same as break
8609 * - For 3), the loop condition is satisfied and read_record
8610 * will re-deliver the message that was held back by the client
8611 * when expecting the ServerHello.
8612 */
8613 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008614 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008615#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008617 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008618 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008619 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008620 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008623 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008625 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008626 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008627 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8631 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008634 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008635 }
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8640 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008641 }
8642
8643 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008644
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008645 /* We're going to return something now, cancel timer,
8646 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008648 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008649
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008650#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008651 /* If we requested renego but received AppData, resend HelloRequest.
8652 * Do it now, after setting in_offt, to avoid taking this branch
8653 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008657 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008658 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008661 return( ret );
8662 }
8663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008665#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008666 }
8667
8668 n = ( len < ssl->in_msglen )
8669 ? len : ssl->in_msglen;
8670
8671 memcpy( buf, ssl->in_offt, n );
8672 ssl->in_msglen -= n;
8673
gabor-mezei-arm3eaa1032020-07-15 10:55:00 +02008674 /* Zeroising the plaintext buffer to erase unused application data
8675 from the memory. */
8676 mbedtls_platform_zeroize( ssl->in_offt, n );
8677
Paul Bakker5121ce52009-01-03 21:22:43 +00008678 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008679 {
8680 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008681 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008682 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008684 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008685 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008686 /* more data available */
8687 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008691
Paul Bakker23986e52011-04-24 08:57:21 +00008692 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008693}
8694
8695/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008696 * Send application data to be encrypted by the SSL layer, taking care of max
8697 * fragment length and buffer size.
8698 *
8699 * According to RFC 5246 Section 6.2.1:
8700 *
8701 * Zero-length fragments of Application data MAY be sent as they are
8702 * potentially useful as a traffic analysis countermeasure.
8703 *
8704 * Therefore, it is possible that the input message length is 0 and the
8705 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008706 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008707static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008708 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008709{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008710 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8711 const size_t max_len = (size_t) ret;
8712
8713 if( ret < 0 )
8714 {
8715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8716 return( ret );
8717 }
8718
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008719 if( len > max_len )
8720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008725 "maximum fragment length: %d > %d",
8726 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008728 }
8729 else
8730#endif
8731 len = max_len;
8732 }
Paul Bakker887bd502011-06-08 13:10:54 +00008733
Paul Bakker5121ce52009-01-03 21:22:43 +00008734 if( ssl->out_left != 0 )
8735 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008736 /*
8737 * The user has previously tried to send the data and
8738 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8739 * written. In this case, we expect the high-level write function
8740 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008745 return( ret );
8746 }
8747 }
Paul Bakker887bd502011-06-08 13:10:54 +00008748 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008749 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008750 /*
8751 * The user is trying to send a message the first time, so we need to
8752 * copy the data into the internal buffers and setup the data structure
8753 * to keep track of partial writes
8754 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008755 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008757 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008758
Hanno Becker67bc7c32018-08-06 11:33:50 +01008759 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008762 return( ret );
8763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008764 }
8765
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008766 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008767}
8768
8769/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008770 * Write application data, doing 1/n-1 splitting if necessary.
8771 *
8772 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008773 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008774 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008777static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008778 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008779{
8780 int ret;
8781
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008782 if( ssl->conf->cbc_record_splitting ==
8783 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008784 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8786 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8787 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008788 {
8789 return( ssl_write_real( ssl, buf, len ) );
8790 }
8791
8792 if( ssl->split_done == 0 )
8793 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008794 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008795 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008796 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008797 }
8798
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008799 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8800 return( ret );
8801 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008802
8803 return( ret + 1 );
8804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008806
8807/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008808 * Write application data (public-facing wrapper)
8809 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008810int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008811{
8812 int ret;
8813
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008815
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008816 if( ssl == NULL || ssl->conf == NULL )
8817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8818
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008819#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008820 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8821 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008822 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008823 return( ret );
8824 }
8825#endif
8826
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008827 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008828 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008829 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008830 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008832 return( ret );
8833 }
8834 }
8835
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008836#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008837 ret = ssl_write_split( ssl, buf, len );
8838#else
8839 ret = ssl_write_real( ssl, buf, len );
8840#endif
8841
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008843
8844 return( ret );
8845}
8846
8847/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008848 * Notify the peer that the connection is being closed
8849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008851{
8852 int ret;
8853
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008854 if( ssl == NULL || ssl->conf == NULL )
8855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008859 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8865 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8866 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008869 return( ret );
8870 }
8871 }
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008874
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008875 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008876}
8877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008879{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008880 if( transform == NULL )
8881 return;
8882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008884 deflateEnd( &transform->ctx_deflate );
8885 inflateEnd( &transform->ctx_inflate );
8886#endif
8887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8889 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 mbedtls_md_free( &transform->md_ctx_enc );
8892 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008893
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008894 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008895}
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#if defined(MBEDTLS_X509_CRT_PARSE_C)
8898static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008901
8902 while( cur != NULL )
8903 {
8904 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008906 cur = next;
8907 }
8908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008910
Hanno Becker0271f962018-08-16 13:23:47 +01008911#if defined(MBEDTLS_SSL_PROTO_DTLS)
8912
8913static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8914{
8915 unsigned offset;
8916 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8917
8918 if( hs == NULL )
8919 return;
8920
Hanno Becker283f5ef2018-08-24 09:34:47 +01008921 ssl_free_buffered_record( ssl );
8922
Hanno Becker0271f962018-08-16 13:23:47 +01008923 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008924 ssl_buffering_free_slot( ssl, offset );
8925}
8926
8927static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8928 uint8_t slot )
8929{
8930 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8931 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008932
8933 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8934 return;
8935
Hanno Beckere605b192018-08-21 15:59:07 +01008936 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008937 {
Hanno Beckere605b192018-08-21 15:59:07 +01008938 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008939 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008940 mbedtls_free( hs_buf->data );
8941 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008942 }
8943}
8944
8945#endif /* MBEDTLS_SSL_PROTO_DTLS */
8946
Gilles Peskine9b562d52018-04-25 20:32:43 +02008947void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008948{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008949 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8950
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008951 if( handshake == NULL )
8952 return;
8953
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008954#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8955 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8956 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008957 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008958 handshake->async_in_progress = 0;
8959 }
8960#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8961
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008962#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8963 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8964 mbedtls_md5_free( &handshake->fin_md5 );
8965 mbedtls_sha1_free( &handshake->fin_sha1 );
8966#endif
8967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8968#if defined(MBEDTLS_SHA256_C)
8969 mbedtls_sha256_free( &handshake->fin_sha256 );
8970#endif
8971#if defined(MBEDTLS_SHA512_C)
8972 mbedtls_sha512_free( &handshake->fin_sha512 );
8973#endif
8974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#if defined(MBEDTLS_DHM_C)
8977 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008979#if defined(MBEDTLS_ECDH_C)
8980 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008981#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008982#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008983 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008984#if defined(MBEDTLS_SSL_CLI_C)
8985 mbedtls_free( handshake->ecjpake_cache );
8986 handshake->ecjpake_cache = NULL;
8987 handshake->ecjpake_cache_len = 0;
8988#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008989#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008990
Janos Follath4ae5c292016-02-10 11:27:43 +00008991#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8992 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008993 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008995#endif
8996
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008997#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8998 if( handshake->psk != NULL )
8999 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009000 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009001 mbedtls_free( handshake->psk );
9002 }
9003#endif
9004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9006 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009007 /*
9008 * Free only the linked list wrapper, not the keys themselves
9009 * since the belong to the SNI callback
9010 */
9011 if( handshake->sni_key_cert != NULL )
9012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009014
9015 while( cur != NULL )
9016 {
9017 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009019 cur = next;
9020 }
9021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009023
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009024#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009025 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009026#endif
9027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028#if defined(MBEDTLS_SSL_PROTO_DTLS)
9029 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009030 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009031 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009032#endif
9033
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009034 mbedtls_platform_zeroize( handshake,
9035 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009036}
9037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009039{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009040 if( session == NULL )
9041 return;
9042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009044 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 mbedtls_x509_crt_free( session->peer_cert );
9047 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009048 }
Paul Bakkered27a042013-04-18 22:46:23 +02009049#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009050
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009051#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009053#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009054
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009055 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009056}
9057
Paul Bakker5121ce52009-01-03 21:22:43 +00009058/*
9059 * Free an SSL context
9060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009062{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009063 if( ssl == NULL )
9064 return;
9065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009067
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009068 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009069 {
Angus Grattond8213d02016-05-25 20:56:48 +10009070 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009072 }
9073
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009074 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009075 {
Angus Grattond8213d02016-05-25 20:56:48 +10009076 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009078 }
9079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009081 if( ssl->compress_buf != NULL )
9082 {
Angus Grattond8213d02016-05-25 20:56:48 +10009083 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009085 }
9086#endif
9087
Paul Bakker48916f92012-09-16 19:57:18 +00009088 if( ssl->transform )
9089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 mbedtls_ssl_transform_free( ssl->transform );
9091 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009092 }
9093
9094 if( ssl->handshake )
9095 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009096 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9098 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 mbedtls_free( ssl->handshake );
9101 mbedtls_free( ssl->transform_negotiate );
9102 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009103 }
9104
Paul Bakkerc0463502013-02-14 11:19:38 +01009105 if( ssl->session )
9106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 mbedtls_ssl_session_free( ssl->session );
9108 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009109 }
9110
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009111#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009112 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009113 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009114 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009116 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9120 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9123 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009124 }
9125#endif
9126
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009127#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009129#endif
9130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009132
Paul Bakker86f04f42013-02-14 11:20:09 +01009133 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009134 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009135}
9136
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009137/*
9138 * Initialze mbedtls_ssl_config
9139 */
9140void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9141{
9142 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9143}
9144
Simon Butcherc97b6972015-12-27 23:48:17 +00009145#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009146static int ssl_preset_default_hashes[] = {
9147#if defined(MBEDTLS_SHA512_C)
9148 MBEDTLS_MD_SHA512,
9149 MBEDTLS_MD_SHA384,
9150#endif
9151#if defined(MBEDTLS_SHA256_C)
9152 MBEDTLS_MD_SHA256,
9153 MBEDTLS_MD_SHA224,
9154#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009155#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009156 MBEDTLS_MD_SHA1,
9157#endif
9158 MBEDTLS_MD_NONE
9159};
Simon Butcherc97b6972015-12-27 23:48:17 +00009160#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009161
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009162static int ssl_preset_suiteb_ciphersuites[] = {
9163 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9164 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9165 0
9166};
9167
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009168#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009169static int ssl_preset_suiteb_hashes[] = {
9170 MBEDTLS_MD_SHA256,
9171 MBEDTLS_MD_SHA384,
9172 MBEDTLS_MD_NONE
9173};
9174#endif
9175
9176#if defined(MBEDTLS_ECP_C)
9177static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009178#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009179 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009180#endif
9181#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009182 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009183#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009184 MBEDTLS_ECP_DP_NONE
9185};
9186#endif
9187
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009188/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009189 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009190 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009191int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009192 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009193{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009194#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009195 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009196#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009197
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009198 /* Use the functions here so that they are covered in tests,
9199 * but otherwise access member directly for efficiency */
9200 mbedtls_ssl_conf_endpoint( conf, endpoint );
9201 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009202
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009203 /*
9204 * Things that are common to all presets
9205 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009206#if defined(MBEDTLS_SSL_CLI_C)
9207 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9208 {
9209 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9210#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9211 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9212#endif
9213 }
9214#endif
9215
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009216#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009217 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009218#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009219
9220#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9221 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9222#endif
9223
9224#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9225 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9226#endif
9227
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009228#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9229 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9230#endif
9231
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009232#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009233 conf->f_cookie_write = ssl_cookie_write_dummy;
9234 conf->f_cookie_check = ssl_cookie_check_dummy;
9235#endif
9236
9237#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9238 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9239#endif
9240
Janos Follath088ce432017-04-10 12:42:31 +01009241#if defined(MBEDTLS_SSL_SRV_C)
9242 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9243#endif
9244
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009245#if defined(MBEDTLS_SSL_PROTO_DTLS)
9246 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9247 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9248#endif
9249
9250#if defined(MBEDTLS_SSL_RENEGOTIATION)
9251 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009252 memset( conf->renego_period, 0x00, 2 );
9253 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009254#endif
9255
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009256#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9257 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9258 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009259 const unsigned char dhm_p[] =
9260 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9261 const unsigned char dhm_g[] =
9262 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9263
Hanno Beckera90658f2017-10-04 15:29:08 +01009264 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9265 dhm_p, sizeof( dhm_p ),
9266 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009267 {
9268 return( ret );
9269 }
9270 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009271#endif
9272
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009273 /*
9274 * Preset-specific defaults
9275 */
9276 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009277 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009278 /*
9279 * NSA Suite B
9280 */
9281 case MBEDTLS_SSL_PRESET_SUITEB:
9282 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9283 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9284 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9285 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9286
9287 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9288 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9289 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9290 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9291 ssl_preset_suiteb_ciphersuites;
9292
9293#if defined(MBEDTLS_X509_CRT_PARSE_C)
9294 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009295#endif
9296
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009297#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009298 conf->sig_hashes = ssl_preset_suiteb_hashes;
9299#endif
9300
9301#if defined(MBEDTLS_ECP_C)
9302 conf->curve_list = ssl_preset_suiteb_curves;
9303#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009304 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009305
9306 /*
9307 * Default
9308 */
9309 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009310 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9311 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9312 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9313 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9314 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9315 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9316 MBEDTLS_SSL_MIN_MINOR_VERSION :
9317 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009318 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9319 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9320
9321#if defined(MBEDTLS_SSL_PROTO_DTLS)
9322 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9323 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9324#endif
9325
9326 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9327 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9328 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9329 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9330 mbedtls_ssl_list_ciphersuites();
9331
9332#if defined(MBEDTLS_X509_CRT_PARSE_C)
9333 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9334#endif
9335
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009336#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009337 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009338#endif
9339
9340#if defined(MBEDTLS_ECP_C)
9341 conf->curve_list = mbedtls_ecp_grp_id_list();
9342#endif
9343
9344#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9345 conf->dhm_min_bitlen = 1024;
9346#endif
9347 }
9348
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009349 return( 0 );
9350}
9351
9352/*
9353 * Free mbedtls_ssl_config
9354 */
9355void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9356{
9357#if defined(MBEDTLS_DHM_C)
9358 mbedtls_mpi_free( &conf->dhm_P );
9359 mbedtls_mpi_free( &conf->dhm_G );
9360#endif
9361
9362#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9363 if( conf->psk != NULL )
9364 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009365 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009366 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009367 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009368 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009369 }
9370
9371 if( conf->psk_identity != NULL )
9372 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009373 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009374 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009375 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009376 conf->psk_identity_len = 0;
9377 }
9378#endif
9379
9380#if defined(MBEDTLS_X509_CRT_PARSE_C)
9381 ssl_key_cert_free( conf->key_cert );
9382#endif
9383
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009384 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009385}
9386
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009387#if defined(MBEDTLS_PK_C) && \
9388 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009389/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394#if defined(MBEDTLS_RSA_C)
9395 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9396 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398#if defined(MBEDTLS_ECDSA_C)
9399 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9400 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009403}
9404
Hanno Becker7e5437a2017-04-28 17:15:26 +01009405unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9406{
9407 switch( type ) {
9408 case MBEDTLS_PK_RSA:
9409 return( MBEDTLS_SSL_SIG_RSA );
9410 case MBEDTLS_PK_ECDSA:
9411 case MBEDTLS_PK_ECKEY:
9412 return( MBEDTLS_SSL_SIG_ECDSA );
9413 default:
9414 return( MBEDTLS_SSL_SIG_ANON );
9415 }
9416}
9417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009419{
9420 switch( sig )
9421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422#if defined(MBEDTLS_RSA_C)
9423 case MBEDTLS_SSL_SIG_RSA:
9424 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#if defined(MBEDTLS_ECDSA_C)
9427 case MBEDTLS_SSL_SIG_ECDSA:
9428 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009429#endif
9430 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009432 }
9433}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009434#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009435
Hanno Becker7e5437a2017-04-28 17:15:26 +01009436#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9437 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9438
9439/* Find an entry in a signature-hash set matching a given hash algorithm. */
9440mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9441 mbedtls_pk_type_t sig_alg )
9442{
9443 switch( sig_alg )
9444 {
9445 case MBEDTLS_PK_RSA:
9446 return( set->rsa );
9447 case MBEDTLS_PK_ECDSA:
9448 return( set->ecdsa );
9449 default:
9450 return( MBEDTLS_MD_NONE );
9451 }
9452}
9453
9454/* Add a signature-hash-pair to a signature-hash set */
9455void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9456 mbedtls_pk_type_t sig_alg,
9457 mbedtls_md_type_t md_alg )
9458{
9459 switch( sig_alg )
9460 {
9461 case MBEDTLS_PK_RSA:
9462 if( set->rsa == MBEDTLS_MD_NONE )
9463 set->rsa = md_alg;
9464 break;
9465
9466 case MBEDTLS_PK_ECDSA:
9467 if( set->ecdsa == MBEDTLS_MD_NONE )
9468 set->ecdsa = md_alg;
9469 break;
9470
9471 default:
9472 break;
9473 }
9474}
9475
9476/* Allow exactly one hash algorithm for each signature. */
9477void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9478 mbedtls_md_type_t md_alg )
9479{
9480 set->rsa = md_alg;
9481 set->ecdsa = md_alg;
9482}
9483
9484#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9485 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9486
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009487/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009488 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009491{
9492 switch( hash )
9493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_MD5_C)
9495 case MBEDTLS_SSL_HASH_MD5:
9496 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498#if defined(MBEDTLS_SHA1_C)
9499 case MBEDTLS_SSL_HASH_SHA1:
9500 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#if defined(MBEDTLS_SHA256_C)
9503 case MBEDTLS_SSL_HASH_SHA224:
9504 return( MBEDTLS_MD_SHA224 );
9505 case MBEDTLS_SSL_HASH_SHA256:
9506 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_SHA512_C)
9509 case MBEDTLS_SSL_HASH_SHA384:
9510 return( MBEDTLS_MD_SHA384 );
9511 case MBEDTLS_SSL_HASH_SHA512:
9512 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009513#endif
9514 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009516 }
9517}
9518
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009519/*
9520 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9521 */
9522unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9523{
9524 switch( md )
9525 {
9526#if defined(MBEDTLS_MD5_C)
9527 case MBEDTLS_MD_MD5:
9528 return( MBEDTLS_SSL_HASH_MD5 );
9529#endif
9530#if defined(MBEDTLS_SHA1_C)
9531 case MBEDTLS_MD_SHA1:
9532 return( MBEDTLS_SSL_HASH_SHA1 );
9533#endif
9534#if defined(MBEDTLS_SHA256_C)
9535 case MBEDTLS_MD_SHA224:
9536 return( MBEDTLS_SSL_HASH_SHA224 );
9537 case MBEDTLS_MD_SHA256:
9538 return( MBEDTLS_SSL_HASH_SHA256 );
9539#endif
9540#if defined(MBEDTLS_SHA512_C)
9541 case MBEDTLS_MD_SHA384:
9542 return( MBEDTLS_SSL_HASH_SHA384 );
9543 case MBEDTLS_MD_SHA512:
9544 return( MBEDTLS_SSL_HASH_SHA512 );
9545#endif
9546 default:
9547 return( MBEDTLS_SSL_HASH_NONE );
9548 }
9549}
9550
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009551#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009552/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009553 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009554 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009555 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009556int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009559
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009560 if( ssl->conf->curve_list == NULL )
9561 return( -1 );
9562
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009563 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009564 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009565 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009566
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009567 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009568}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009569#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009570
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009571#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009572/*
9573 * Check if a hash proposed by the peer is in our list.
9574 * Return 0 if we're willing to use it, -1 otherwise.
9575 */
9576int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9577 mbedtls_md_type_t md )
9578{
9579 const int *cur;
9580
9581 if( ssl->conf->sig_hashes == NULL )
9582 return( -1 );
9583
9584 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9585 if( *cur == (int) md )
9586 return( 0 );
9587
9588 return( -1 );
9589}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009590#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592#if defined(MBEDTLS_X509_CRT_PARSE_C)
9593int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9594 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009595 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009596 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009597{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009598 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009600 int usage = 0;
9601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009603 const char *ext_oid;
9604 size_t ext_len;
9605#endif
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9608 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009609 ((void) cert);
9610 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009611 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009612#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9615 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009616 {
9617 /* Server part of the key exchange */
9618 switch( ciphersuite->key_exchange )
9619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 case MBEDTLS_KEY_EXCHANGE_RSA:
9621 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009622 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009623 break;
9624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9626 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9627 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9628 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009629 break;
9630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9632 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009633 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009634 break;
9635
9636 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 case MBEDTLS_KEY_EXCHANGE_NONE:
9638 case MBEDTLS_KEY_EXCHANGE_PSK:
9639 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9640 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009641 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009642 usage = 0;
9643 }
9644 }
9645 else
9646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9648 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009649 }
9650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009652 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009653 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009654 ret = -1;
9655 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009656#else
9657 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9661 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9664 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009665 }
9666 else
9667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9669 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009670 }
9671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009673 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009674 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009675 ret = -1;
9676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009678
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009679 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009682
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009683/*
9684 * Convert version numbers to/from wire format
9685 * and, for DTLS, to/from TLS equivalent.
9686 *
9687 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009688 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009689 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9690 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009693 unsigned char ver[2] )
9694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#if defined(MBEDTLS_SSL_PROTO_DTLS)
9696 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009699 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9700
9701 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9702 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9703 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009704 else
9705#else
9706 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009707#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009708 {
9709 ver[0] = (unsigned char) major;
9710 ver[1] = (unsigned char) minor;
9711 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009712}
9713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009715 const unsigned char ver[2] )
9716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717#if defined(MBEDTLS_SSL_PROTO_DTLS)
9718 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009719 {
9720 *major = 255 - ver[0] + 2;
9721 *minor = 255 - ver[1] + 1;
9722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009724 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9725 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009726 else
9727#else
9728 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009729#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009730 {
9731 *major = ver[0];
9732 *minor = ver[1];
9733 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009734}
9735
Simon Butcher99000142016-10-13 17:21:01 +01009736int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9737{
9738#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9739 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9740 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9741
9742 switch( md )
9743 {
9744#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9745#if defined(MBEDTLS_MD5_C)
9746 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009747 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009748#endif
9749#if defined(MBEDTLS_SHA1_C)
9750 case MBEDTLS_SSL_HASH_SHA1:
9751 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9752 break;
9753#endif
9754#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9755#if defined(MBEDTLS_SHA512_C)
9756 case MBEDTLS_SSL_HASH_SHA384:
9757 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9758 break;
9759#endif
9760#if defined(MBEDTLS_SHA256_C)
9761 case MBEDTLS_SSL_HASH_SHA256:
9762 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9763 break;
9764#endif
9765 default:
9766 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9767 }
9768
9769 return 0;
9770#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9771 (void) ssl;
9772 (void) md;
9773
9774 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9775#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9776}
9777
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009778#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9779 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9780int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9781 unsigned char *output,
9782 unsigned char *data, size_t data_len )
9783{
9784 int ret = 0;
9785 mbedtls_md5_context mbedtls_md5;
9786 mbedtls_sha1_context mbedtls_sha1;
9787
9788 mbedtls_md5_init( &mbedtls_md5 );
9789 mbedtls_sha1_init( &mbedtls_sha1 );
9790
9791 /*
9792 * digitally-signed struct {
9793 * opaque md5_hash[16];
9794 * opaque sha_hash[20];
9795 * };
9796 *
9797 * md5_hash
9798 * MD5(ClientHello.random + ServerHello.random
9799 * + ServerParams);
9800 * sha_hash
9801 * SHA(ClientHello.random + ServerHello.random
9802 * + ServerParams);
9803 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009804 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009805 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009807 goto exit;
9808 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009809 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009810 ssl->handshake->randbytes, 64 ) ) != 0 )
9811 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009813 goto exit;
9814 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009815 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009818 goto exit;
9819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009820 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009821 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009823 goto exit;
9824 }
9825
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009826 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009827 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009829 goto exit;
9830 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009831 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009832 ssl->handshake->randbytes, 64 ) ) != 0 )
9833 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009835 goto exit;
9836 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009837 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009838 data_len ) ) != 0 )
9839 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009841 goto exit;
9842 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009843 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009844 output + 16 ) ) != 0 )
9845 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009847 goto exit;
9848 }
9849
9850exit:
9851 mbedtls_md5_free( &mbedtls_md5 );
9852 mbedtls_sha1_free( &mbedtls_sha1 );
9853
9854 if( ret != 0 )
9855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9856 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9857
9858 return( ret );
9859
9860}
9861#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9862 MBEDTLS_SSL_PROTO_TLS1_1 */
9863
9864#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9865 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9866int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009867 unsigned char *hash, size_t *hashlen,
9868 unsigned char *data, size_t data_len,
9869 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009870{
9871 int ret = 0;
9872 mbedtls_md_context_t ctx;
9873 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009874 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009875
9876 mbedtls_md_init( &ctx );
9877
9878 /*
9879 * digitally-signed struct {
9880 * opaque client_random[32];
9881 * opaque server_random[32];
9882 * ServerDHParams params;
9883 * };
9884 */
9885 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9886 {
9887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9888 goto exit;
9889 }
9890 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9891 {
9892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9893 goto exit;
9894 }
9895 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9896 {
9897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9898 goto exit;
9899 }
9900 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9901 {
9902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9903 goto exit;
9904 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009905 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009906 {
9907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9908 goto exit;
9909 }
9910
9911exit:
9912 mbedtls_md_free( &ctx );
9913
9914 if( ret != 0 )
9915 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9916 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9917
9918 return( ret );
9919}
9920#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9921 MBEDTLS_SSL_PROTO_TLS1_2 */
9922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923#endif /* MBEDTLS_SSL_TLS_C */