blob: ae8f34ec9ca3f3681226027de3d62f49f77cdb5e [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 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100452 {
453 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
454 goto exit;
455 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100458 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100459
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100460 ret = mbedtls_md_hmac_starts( &md_ctx, S1, hs );
461 if( ret != 0 )
462 goto exit;
463 ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
464 if( ret != 0 )
465 goto exit;
466 ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
467 if( ret != 0 )
468 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 for( i = 0; i < dlen; i += 16 )
471 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100472 ret = mbedtls_md_hmac_reset ( &md_ctx );
473 if( ret != 0 )
474 goto exit;
475 ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
476 if( ret != 0 )
477 goto exit;
478 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
479 if( ret != 0 )
480 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100481
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100482 ret = mbedtls_md_hmac_reset ( &md_ctx );
483 if( ret != 0 )
484 goto exit;
485 ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
486 if( ret != 0 )
487 goto exit;
488 ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
489 if( ret != 0 )
490 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
493
494 for( j = 0; j < k; j++ )
495 dstbuf[i + j] = h_i[j];
496 }
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100499
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 /*
501 * XOR out with P_sha1(secret,label+random)[0..dlen]
502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100504 {
505 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
506 goto exit;
507 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100510 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100511
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100512 ret = mbedtls_md_hmac_starts( &md_ctx, S2, hs );
513 if( ret != 0 )
514 goto exit;
515 ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
516 if( ret != 0 )
517 goto exit;
518 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
519 if( ret != 0 )
520 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 for( i = 0; i < dlen; i += 20 )
523 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100524 ret = mbedtls_md_hmac_reset ( &md_ctx );
525 if( ret != 0 )
526 goto exit;
527 ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
528 if( ret != 0 )
529 goto exit;
530 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
531 if( ret != 0 )
532 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100533
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100534 ret = mbedtls_md_hmac_reset ( &md_ctx );
535 if( ret != 0 )
536 goto exit;
537 ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
538 if( ret != 0 )
539 goto exit;
540 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
541 if( ret != 0 )
542 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
545
546 for( j = 0; j < k; j++ )
547 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
548 }
549
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100550exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100552
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500553 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
554 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 return( 0 );
557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
561static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100562 const unsigned char *secret, size_t slen,
563 const char *label,
564 const unsigned char *random, size_t rlen,
565 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000566{
567 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100568 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000569 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
571 const mbedtls_md_info_t *md_info;
572 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100573 int ret;
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100581
582 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000584
585 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100586 memcpy( tmp + md_len, label, nb );
587 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000588 nb += rlen;
589
590 /*
591 * Compute P_<hash>(secret, label + random)[0..dlen]
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100594 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100595
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100596 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
597 if( ret != 0 )
598 goto exit;
599 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
600 if( ret != 0 )
601 goto exit;
602 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
603 if( ret != 0 )
604 goto exit;
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100605
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000607 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100608 ret = mbedtls_md_hmac_reset ( &md_ctx );
609 if( ret != 0 )
610 goto exit;
611 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
612 if( ret != 0 )
613 goto exit;
614 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
615 if( ret != 0 )
616 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100617
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100618 ret = mbedtls_md_hmac_reset ( &md_ctx );
619 if( ret != 0 )
620 goto exit;
621 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
622 if( ret != 0 )
623 goto exit;
624 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
625 if( ret != 0 )
626 goto exit;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100628 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 for( j = 0; j < k; j++ )
631 dstbuf[i + j] = h_i[j];
632 }
633
Gilles Peskinea89bdf02021-12-10 21:35:10 +0100634exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100636
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500637 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
638 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639
640 return( 0 );
641}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100644static int tls_prf_sha256( const unsigned char *secret, size_t slen,
645 const char *label,
646 const unsigned char *random, size_t rlen,
647 unsigned char *dstbuf, size_t dlen )
648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650 label, random, rlen, dstbuf, dlen ) );
651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200655static int tls_prf_sha384( const unsigned char *secret, size_t slen,
656 const char *label,
657 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000658 unsigned char *dstbuf, size_t dlen )
659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_SHA512_C */
664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
669 defined(MBEDTLS_SSL_PROTO_TLS1_1)
670static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200671#endif
Paul Bakker380da532012-04-18 16:10:25 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_SSL_PROTO_SSL3)
674static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
675static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200676#endif
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300679static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200681#endif
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
684#if defined(MBEDTLS_SHA256_C)
685static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300686static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SHA512_C)
691static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -0300692static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200699 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 unsigned char keyblk[256];
702 unsigned char *key1;
703 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100704 unsigned char *mac_enc;
705 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000706 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200707 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 const mbedtls_cipher_info_t *cipher_info;
709 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_ssl_session *session = ssl->session_negotiate;
712 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
713 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100718 if( cipher_info == NULL )
719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100721 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100723 }
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100726 if( md_info == NULL )
727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100729 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100731 }
732
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000734 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_SSL_PROTO_SSL3)
737 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000738 {
Paul Bakker48916f92012-09-16 19:57:18 +0000739 handshake->tls_prf = ssl3_prf;
740 handshake->calc_verify = ssl_calc_verify_ssl;
741 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000742 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200743 else
744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
746 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000747 {
Paul Bakker48916f92012-09-16 19:57:18 +0000748 handshake->tls_prf = tls1_prf;
749 handshake->calc_verify = ssl_calc_verify_tls;
750 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000751 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200752 else
753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
755#if defined(MBEDTLS_SHA512_C)
756 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
757 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000758 {
Paul Bakker48916f92012-09-16 19:57:18 +0000759 handshake->tls_prf = tls_prf_sha384;
760 handshake->calc_verify = ssl_calc_verify_tls_sha384;
761 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000762 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000763 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_SHA256_C)
766 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767 {
Paul Bakker48916f92012-09-16 19:57:18 +0000768 handshake->tls_prf = tls_prf_sha256;
769 handshake->calc_verify = ssl_calc_verify_tls_sha256;
770 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000771 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200772 else
773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200778 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 * SSLv3:
782 * master =
783 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
784 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
785 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200786 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 * master = PRF( premaster, "master secret", randbytes )[0..47]
789 */
Paul Bakker0a597072012-09-25 21:55:46 +0000790 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000793 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
796 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200797 {
798 unsigned char session_hash[48];
799 size_t hash_len;
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200802
803 ssl->handshake->calc_verify( ssl, session_hash );
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
806 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200809 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200811 {
812 hash_len = 48;
813 }
814 else
815#endif
816 hash_len = 32;
817 }
818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200820 hash_len = 36;
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200823
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100824 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
825 "extended master secret",
826 session_hash, hash_len,
827 session->master, 48 );
828 if( ret != 0 )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100831 return( ret );
832 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200833
834 }
835 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200836#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100837 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
838 "master secret",
839 handshake->randbytes, 64,
840 session->master, 48 );
841 if( ret != 0 )
842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100844 return( ret );
845 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500847 mbedtls_platform_zeroize( handshake->premaster,
848 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 }
850 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
853 /*
854 * Swap the client and server random values.
855 */
Paul Bakker48916f92012-09-16 19:57:18 +0000856 memcpy( tmp, handshake->randbytes, 64 );
857 memcpy( handshake->randbytes, tmp + 32, 32 );
858 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500859 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
861 /*
862 * SSLv3:
863 * key block =
864 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
865 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
866 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
867 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
868 * ...
869 *
870 * TLSv1:
871 * key block = PRF( master, "key expansion", randbytes )
872 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100873 ret = handshake->tls_prf( session->master, 48, "key expansion",
874 handshake->randbytes, 64, keyblk, 256 );
875 if( ret != 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100878 return( ret );
879 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
882 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
883 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
884 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
885 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500887 mbedtls_platform_zeroize( handshake->randbytes,
888 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 /*
891 * Determine the appropriate key, IV and MAC length.
892 */
Paul Bakker68884e32013-01-07 18:20:04 +0100893
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200894 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200897 cipher_info->mode == MBEDTLS_MODE_CCM ||
898 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200900 size_t taglen, explicit_ivlen;
901
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000903 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200904
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200905 /* All modes haves 96-bit IVs;
906 * GCM and CCM has 4 implicit and 8 explicit bytes
907 * ChachaPoly has all 12 bytes implicit
908 */
Paul Bakker68884e32013-01-07 18:20:04 +0100909 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200910 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
911 transform->fixed_ivlen = 12;
912 else
913 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200915 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
916 taglen = transform->ciphersuite_info->flags &
917 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
918
919
920 /* Minimum length of encrypted record */
921 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
922 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100923 }
924 else
925 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200926 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
928 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100932 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200934 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000935 mac_key_len = mbedtls_md_get_size( md_info );
936 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200939 /*
940 * If HMAC is to be truncated, we shall keep the leftmost bytes,
941 * (rfc 6066 page 13 or rfc 2104 section 4),
942 * so we only need to adjust the length here.
943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000947
948#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
949 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000950 * HMAC implementation which also truncates the key
951 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000952 mac_key_len = transform->maclen;
953#endif
954 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200956
957 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100958 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200960 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200962 transform->minlen = transform->maclen;
963 else
Paul Bakker68884e32013-01-07 18:20:04 +0100964 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965 /*
966 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100967 * 1. if EtM is in use: one block plus MAC
968 * otherwise: * first multiple of blocklen greater than maclen
969 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
972 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100973 {
974 transform->minlen = transform->maclen
975 + cipher_info->block_size;
976 }
977 else
978#endif
979 {
980 transform->minlen = transform->maclen
981 + cipher_info->block_size
982 - transform->maclen % cipher_info->block_size;
983 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
986 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
987 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200988 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100989 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200990#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
992 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
993 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200994 {
995 transform->minlen += transform->ivlen;
996 }
997 else
998#endif
999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001002 }
Paul Bakker68884e32013-01-07 18:20:04 +01001003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 }
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001007 transform->keylen, transform->minlen, transform->ivlen,
1008 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
1010 /*
1011 * Finally setup the cipher contexts, IVs and MAC secrets.
1012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001014 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 key1 = keyblk + mac_key_len * 2;
1017 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakker68884e32013-01-07 18:20:04 +01001019 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001022 /*
1023 * This is not used in TLS v1.1.
1024 */
Paul Bakker48916f92012-09-16 19:57:18 +00001025 iv_copy_len = ( transform->fixed_ivlen ) ?
1026 transform->fixed_ivlen : transform->ivlen;
1027 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1028 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 }
1031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_CLI_C */
1033#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001036 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1037 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Hanno Becker81c7b182017-11-09 18:39:33 +00001039 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001040 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001042 /*
1043 * This is not used in TLS v1.1.
1044 */
Paul Bakker48916f92012-09-16 19:57:18 +00001045 iv_copy_len = ( transform->fixed_ivlen ) ?
1046 transform->fixed_ivlen : transform->ivlen;
1047 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1048 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001049 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_SSL_PROTO_SSL3)
1059 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001060 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001061 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001065 }
1066
Hanno Becker81c7b182017-11-09 18:39:33 +00001067 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1068 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001069 }
1070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1073 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1074 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001075 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001076 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1077 For AEAD-based ciphersuites, there is nothing to do here. */
1078 if( mac_key_len != 0 )
1079 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001080 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc,
1081 mac_enc, mac_key_len );
1082 if( ret != 0 )
1083 return( ret );
1084 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec,
1085 mac_dec, mac_key_len );
1086 if( ret != 0 )
1087 return( ret );
Gilles Peskine039fd122018-03-19 19:06:08 +01001088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001090 else
1091#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001095 }
Paul Bakker68884e32013-01-07 18:20:04 +01001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1098 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001103 transform->iv_enc, transform->iv_dec,
1104 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001105 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001106 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1109 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001110 }
1111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001113
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001114#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1115 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001116 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001117 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1118 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001119 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001120 iv_copy_len );
1121 }
1122#endif
1123
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001124 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128 return( ret );
1129 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001130
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001131 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 cipher_info ) ) != 0 )
1133 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001139 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143 return( ret );
1144 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001147 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001151 return( ret );
1152 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_CIPHER_MODE_CBC)
1155 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1158 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001161 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001162 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1165 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001168 return( ret );
1169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001173 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001176 // Initialize compression
1177 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001179 {
Paul Bakker16770332013-10-11 09:59:44 +02001180 if( ssl->compress_buf == NULL )
1181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001183 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001184 if( ssl->compress_buf == NULL )
1185 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001187 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001189 }
1190 }
1191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001193
Paul Bakker48916f92012-09-16 19:57:18 +00001194 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1195 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001196
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001197 if( deflateInit( &transform->ctx_deflate,
1198 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001199 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1202 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001203 }
1204 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
1209 return( 0 );
1210}
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_SSL_PROTO_SSL3)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001213void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker5121ce52009-01-03 21:22:43 +00001214{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001215 mbedtls_md5_context md5;
1216 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 unsigned char pad_1[48];
1218 unsigned char pad_2[48];
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001222 mbedtls_md5_init( &md5 );
1223 mbedtls_sha1_init( &sha1 );
1224
1225 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1226 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Paul Bakker380da532012-04-18 16:10:25 +00001228 memset( pad_1, 0x36, 48 );
1229 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001231 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1232 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1233 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001235 mbedtls_md5_starts_ret( &md5 );
1236 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1237 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1238 mbedtls_md5_update_ret( &md5, hash, 16 );
1239 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001241 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1242 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1243 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001244
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001245 mbedtls_sha1_starts_ret( &sha1 );
1246 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1247 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1248 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1249 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001253
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001254 mbedtls_md5_free( &md5 );
1255 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001256
Paul Bakker380da532012-04-18 16:10:25 +00001257 return;
1258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001262void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001263{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001264 mbedtls_md5_context md5;
1265 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001268
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001269 mbedtls_md5_init( &md5 );
1270 mbedtls_sha1_init( &sha1 );
1271
1272 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1273 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001274
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001275 mbedtls_md5_finish_ret( &md5, hash );
1276 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001280
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001281 mbedtls_md5_free( &md5 );
1282 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001283
Paul Bakker380da532012-04-18 16:10:25 +00001284 return;
1285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1289#if defined(MBEDTLS_SHA256_C)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001290void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001291{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001292 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001293
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001294 mbedtls_sha256_init( &sha256 );
1295
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001297
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001298 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001299 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001303
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001304 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001305
Paul Bakker380da532012-04-18 16:10:25 +00001306 return;
1307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_SHA512_C)
Rodrigo Dias Correa1a0c7fb2020-11-10 01:38:00 -03001311void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char *hash )
Paul Bakker380da532012-04-18 16:10:25 +00001312{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001313 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001314
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001315 mbedtls_sha512_init( &sha512 );
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001318
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001319 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001320 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001325 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 return;
1328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_SHA512_C */
1330#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1333int 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 +02001334{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001335 unsigned char *p = ssl->handshake->premaster;
1336 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001337 const unsigned char *psk = ssl->conf->psk;
1338 size_t psk_len = ssl->conf->psk_len;
1339
1340 /* If the psk callback was called, use its result */
1341 if( ssl->handshake->psk != NULL )
1342 {
1343 psk = ssl->handshake->psk;
1344 psk_len = ssl->handshake->psk_len;
1345 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001346
1347 /*
1348 * PMS = struct {
1349 * opaque other_secret<0..2^16-1>;
1350 * opaque psk<0..2^16-1>;
1351 * };
1352 * with "other_secret" depending on the particular key exchange
1353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1355 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001356 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001357 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001359
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001360 *(p++) = (unsigned char)( psk_len >> 8 );
1361 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001362
1363 if( end < p || (size_t)( end - p ) < psk_len )
1364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1365
1366 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001367 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001368 }
1369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1371#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1372 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001373 {
1374 /*
1375 * other_secret already set by the ClientKeyExchange message,
1376 * and is 48 bytes long
1377 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001378 if( end - p < 2 )
1379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1380
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001381 *p++ = 0;
1382 *p++ = 48;
1383 p += 48;
1384 }
1385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1387#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1388 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001389 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001390 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001391 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001392
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001393 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001395 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001396 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001399 return( ret );
1400 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001401 *(p++) = (unsigned char)( len >> 8 );
1402 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001403 p += len;
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001406 }
1407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1409#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1410 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001411 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001412 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001413 size_t zlen;
1414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001416 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001417 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001420 return( ret );
1421 }
1422
1423 *(p++) = (unsigned char)( zlen >> 8 );
1424 *(p++) = (unsigned char)( zlen );
1425 p += zlen;
1426
Janos Follath3fbdada2018-08-15 10:26:53 +01001427 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1428 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001429 }
1430 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001435 }
1436
1437 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001438 if( end - p < 2 )
1439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001440
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001441 *(p++) = (unsigned char)( psk_len >> 8 );
1442 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001443
1444 if( end < p || (size_t)( end - p ) < psk_len )
1445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1446
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001447 memcpy( p, psk, psk_len );
1448 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001449
1450 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1451
1452 return( 0 );
1453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001457/*
1458 * SSLv3.0 MAC functions
1459 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001460#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001461static int ssl_mac( mbedtls_md_context_t *md_ctx,
1462 const unsigned char *secret,
1463 const unsigned char *buf, size_t len,
1464 const unsigned char *ctr, int type,
1465 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001466{
1467 unsigned char header[11];
1468 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001469 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1471 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001472 int ret;
Paul Bakker68884e32013-01-07 18:20:04 +01001473
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001474 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001476 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001477 else
Paul Bakker68884e32013-01-07 18:20:04 +01001478 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
1480 memcpy( header, ctr, 8 );
1481 header[ 8] = (unsigned char) type;
1482 header[ 9] = (unsigned char)( len >> 8 );
1483 header[10] = (unsigned char)( len );
1484
Paul Bakker68884e32013-01-07 18:20:04 +01001485 memset( padding, 0x36, padlen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001486 ret = mbedtls_md_starts( md_ctx );
1487 if( ret != 0 )
1488 return( ret );
1489 ret = mbedtls_md_update( md_ctx, secret, md_size );
1490 if( ret != 0 )
1491 return( ret );
1492 ret = mbedtls_md_update( md_ctx, padding, padlen );
1493 if( ret != 0 )
1494 return( ret );
1495 ret = mbedtls_md_update( md_ctx, header, 11 );
1496 if( ret != 0 )
1497 return( ret );
1498 ret = mbedtls_md_update( md_ctx, buf, len );
1499 if( ret != 0 )
1500 return( ret );
1501 ret = mbedtls_md_finish( md_ctx, out );
1502 if( ret != 0 )
1503 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakker68884e32013-01-07 18:20:04 +01001505 memset( padding, 0x5C, padlen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001506 ret = mbedtls_md_starts( md_ctx );
1507 if( ret != 0 )
1508 return( ret );
1509 ret = mbedtls_md_update( md_ctx, secret, md_size );
1510 if( ret != 0 )
1511 return( ret );
1512 ret = mbedtls_md_update( md_ctx, padding, padlen );
1513 if( ret != 0 )
1514 return( ret );
1515 ret = mbedtls_md_update( md_ctx, out, md_size );
1516 if( ret != 0 )
1517 return( ret );
1518 ret = mbedtls_md_finish( md_ctx, out );
1519 if( ret != 0 )
1520 return( ret );
1521
1522 return( 0 );
Paul Bakker5f70b252012-09-13 14:23:06 +00001523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001527 defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001528#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001529#endif
1530
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001531/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001537 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001541 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001545 }
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001550 ssl->out_msg, ssl->out_msglen );
1551
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001553 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001555#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( mode == MBEDTLS_MODE_STREAM ||
1557 ( mode == MBEDTLS_MODE_CBC
1558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1559 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001560#endif
1561 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_SSL_PROTO_SSL3)
1564 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001565 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001566 unsigned char mac[SSL_MAC_MAX_BYTES];
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001567 int ret;
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001568
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001569 ret = ssl_mac( &ssl->transform_out->md_ctx_enc,
1570 ssl->transform_out->mac_enc,
1571 ssl->out_msg, ssl->out_msglen,
1572 ssl->out_ctr, ssl->out_msgtype,
1573 mac );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001574
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001575 if( ret == 0 )
1576 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Gilles Peskine957e3832021-12-10 21:33:21 +01001577 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001578 if( ret != 0 )
1579 {
1580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_mac", ret );
1581 return( ret );
1582 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001583 }
1584 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1587 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1588 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001589 {
Hanno Becker992b6872017-11-09 18:57:39 +00001590 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001591 int ret;
Hanno Becker992b6872017-11-09 18:57:39 +00001592
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001593 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1594 if( ret != 0 )
1595 goto hmac_failed_etm_disabled;
1596 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1597 if( ret != 0 )
1598 goto hmac_failed_etm_disabled;
1599 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1600 if( ret != 0 )
1601 goto hmac_failed_etm_disabled;
1602 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
1603 ssl->out_msg, ssl->out_msglen );
1604 ret = mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
1605 if( ret != 0 )
1606 goto hmac_failed_etm_disabled;
1607 ret = mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1608 if( ret != 0 )
1609 goto hmac_failed_etm_disabled;
Hanno Becker992b6872017-11-09 18:57:39 +00001610
1611 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001612
1613 hmac_failed_etm_disabled:
Gilles Peskine957e3832021-12-10 21:33:21 +01001614 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001615 if( ret != 0 )
1616 {
1617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_hmac_xxx", ret );
1618 return( ret );
1619 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 }
1621 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001622#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001626 }
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 ssl->out_msg + ssl->out_msglen,
1630 ssl->transform_out->maclen );
1631
1632 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001633 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001634 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001635#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 /*
1638 * Encrypt
1639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1641 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001643 int ret;
1644 size_t olen = 0;
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 "including %d bytes of padding",
1648 ssl->out_msglen, 0 ) );
1649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001651 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001652 ssl->transform_out->ivlen,
1653 ssl->out_msg, ssl->out_msglen,
1654 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001657 return( ret );
1658 }
1659
1660 if( ssl->out_msglen != olen )
1661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 }
Paul Bakker68884e32013-01-07 18:20:04 +01001666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001668#if defined(MBEDTLS_GCM_C) || \
1669 defined(MBEDTLS_CCM_C) || \
1670 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001672 mode == MBEDTLS_MODE_CCM ||
1673 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001674 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001675 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001676 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001677 unsigned char *enc_msg;
1678 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001679 unsigned char iv[12];
1680 mbedtls_ssl_transform *transform = ssl->transform_out;
1681 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001683 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001684
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001685 /*
1686 * Prepare additional authenticated data
1687 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001688 memcpy( add_data, ssl->out_ctr, 8 );
1689 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001691 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001692 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1693 add_data[12] = ssl->out_msglen & 0xFF;
1694
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001695 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 /*
1698 * Generate IV
1699 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001700 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1701 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001702 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001703 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1704 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1705 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1706
1707 }
1708 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1709 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001710 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001711 unsigned char i;
1712
1713 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1714
1715 for( i = 0; i < 8; i++ )
1716 iv[i+4] ^= ssl->out_ctr[i];
1717 }
1718 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001719 {
1720 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001723 }
1724
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001725 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1726 iv, transform->ivlen );
1727 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1728 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001729
Paul Bakker68884e32013-01-07 18:20:04 +01001730 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001731 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001732 */
1733 enc_msg = ssl->out_msg;
1734 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001735 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001738 "including 0 bytes of padding",
1739 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001740
Paul Bakker68884e32013-01-07 18:20:04 +01001741 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001742 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001743 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001744 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1745 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001746 add_data, 13,
1747 enc_msg, enc_msglen,
1748 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001749 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001752 return( ret );
1753 }
1754
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001755 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001759 }
1760
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001761 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001768#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001771 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001772 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001773 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001774
Paul Bakker48916f92012-09-16 19:57:18 +00001775 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1776 ssl->transform_out->ivlen;
1777 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 padlen = 0;
1779
1780 for( i = 0; i <= padlen; i++ )
1781 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1782
1783 ssl->out_msglen += padlen + 1;
1784
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001785 enc_msglen = ssl->out_msglen;
1786 enc_msg = ssl->out_msg;
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001789 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001790 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1791 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001794 {
1795 /*
1796 * Generate IV
1797 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001798 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001799 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001800 if( ret != 0 )
1801 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001802
Paul Bakker92be97b2013-01-02 17:30:03 +01001803 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001804 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001805
1806 /*
1807 * Fix pointer positions and message length with added IV
1808 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001809 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001810 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001811 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001816 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001817 ssl->out_msglen, ssl->transform_out->ivlen,
1818 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001821 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001822 ssl->transform_out->ivlen,
1823 enc_msg, enc_msglen,
1824 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001827 return( ret );
1828 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001829
Paul Bakkercca5b812013-08-31 17:40:26 +02001830 if( enc_msglen != olen )
1831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001834 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1837 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001838 {
1839 /*
1840 * Save IV in SSL3 and TLS1
1841 */
1842 memcpy( ssl->transform_out->iv_enc,
1843 ssl->transform_out->cipher_ctx_enc.iv,
1844 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001846#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001849 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001850 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001851 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1852
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001853 /*
1854 * MAC(MAC_write_key, seq_num +
1855 * TLSCipherText.type +
1856 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001857 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001858 * IV + // except for TLS 1.0
1859 * ENC(content + padding + padding_length));
1860 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001861 unsigned char pseudo_hdr[13];
1862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001864
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001865 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1866 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001867 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1868 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001871
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001872 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1873 if( ret != 0 )
1874 goto hmac_failed_etm_enabled;
1875 ret = mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
1876 ssl->out_iv, ssl->out_msglen );
1877 if( ret != 0 )
1878 goto hmac_failed_etm_enabled;
1879 ret = mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
1880 if( ret != 0 )
1881 goto hmac_failed_etm_enabled;
1882 ret = mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1883 if( ret != 0 )
1884 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001885
Hanno Becker3d8c9072018-01-05 16:24:22 +00001886 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1887 ssl->transform_out->maclen );
1888
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001889 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001890 auth_done++;
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001891
1892 hmac_failed_etm_enabled:
Gilles Peskine957e3832021-12-10 21:33:21 +01001893 mbedtls_platform_zeroize( mac, ssl->transform_out->maclen );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01001894 if( ret != 0 )
1895 {
1896 MBEDTLS_SSL_DEBUG_RET( 1, "HMAC calculation failed", ret );
1897 return( ret );
1898 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001899 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001902 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02001903#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001909 /* Make extra sure authentication was performed, exactly once */
1910 if( auth_done != 1 )
1911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001914 }
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 return( 0 );
1919}
1920
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02001921#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001922/*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001923 * Constant-flow conditional memcpy:
1924 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1925 * - otherwise, a no-op,
1926 * but with execution flow independent of the values of c1 and c2.
1927 *
1928 * Use only bit operations to avoid branches that could be used by some
1929 * compilers on some platforms to translate comparison operators.
1930 */
Manuel Pégourié-Gonnard94fd8dc2020-07-28 11:56:05 +02001931static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1932 const unsigned char *src,
1933 size_t len,
1934 size_t c1, size_t c2 )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001935{
1936 /* diff = 0 if c1 == c2, non-zero otherwise */
1937 const size_t diff = c1 ^ c2;
1938
1939 /* MSVC has a warning about unary minus on unsigned integer types,
1940 * but this is well-defined and precisely what we want to do here. */
1941#if defined(_MSC_VER)
1942#pragma warning( push )
1943#pragma warning( disable : 4146 )
1944#endif
1945
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001946 /* diff_msb's most significant bit is equal to c1 != c2 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001947 const size_t diff_msb = ( diff | -diff );
1948
1949 /* diff1 = c1 != c2 */
1950 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1951
1952 /* mask = c1 != c2 ? 0xff : 0x00 */
Manuel Pégourié-Gonnard09ac2972020-07-28 11:57:25 +02001953 const unsigned char mask = (unsigned char) -diff1;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001954
1955#if defined(_MSC_VER)
1956#pragma warning( pop )
1957#endif
1958
1959 /* dst[i] = c1 != c2 ? dst[i] : src[i] */
Manuel Pégourié-Gonnard253b0de2020-07-29 10:04:36 +02001960 size_t i;
1961 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001962 dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask );
1963}
1964
1965/*
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001966 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnarde1b1fff2020-07-29 12:54:04 +02001967 *
1968 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1969 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02001970 */
1971int mbedtls_ssl_cf_hmac(
1972 mbedtls_md_context_t *ctx,
1973 const unsigned char *add_data, size_t add_data_len,
1974 const unsigned char *data, size_t data_len_secret,
1975 size_t min_data_len, size_t max_data_len,
1976 unsigned char *output )
1977{
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001978 /*
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001979 * This function breaks the HMAC abstraction and uses the md_clone()
1980 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001981 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001982 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001983 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001984 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001985 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001986 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1987 * minlen, then cloning the context, and for each byte up to maxlen
1988 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001989 *
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001990 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001991 */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001992 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnard74503bb2020-07-28 11:42:31 +02001993 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1994 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001995 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnarda6c13172020-07-28 11:45:02 +02001996 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02001997 const unsigned char * const okey = ikey + block_size;
1998 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02001999
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002000 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
2001 mbedtls_md_context_t aux;
2002 size_t offset;
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002003 int ret;
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002004
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002005 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002006
2007#define MD_CHK( func_call ) \
2008 do { \
2009 ret = (func_call); \
2010 if( ret != 0 ) \
2011 goto cleanup; \
2012 } while( 0 )
2013
2014 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002015
2016 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
2017 * so we can start directly with the message */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002018 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
2019 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002020
2021 /* For each possible length, compute the hash up to that point */
2022 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002023 {
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002024 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
2025 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002026 /* Keep only the correct inner_hash in the output buffer */
2027 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
2028 offset, data_len_secret );
2029
2030 if( offset < max_data_len )
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002031 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002032 }
2033
Manuel Pégourié-Gonnard7f0d1932021-05-19 10:44:43 +02002034 /* The context needs to finish() before it starts() again */
2035 MD_CHK( mbedtls_md_finish( ctx, aux_out ) );
2036
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002037 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002038 MD_CHK( mbedtls_md_starts( ctx ) );
2039 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
2040 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
2041 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002042
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002043 /* Done, get ready for next time */
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002044 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002045
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002046#undef MD_CHK
2047
2048cleanup:
Manuel Pégourié-Gonnardde02b582020-07-28 11:25:34 +02002049 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard5bb6f3c2020-07-28 11:49:42 +02002050 return( ret );
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002051}
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002052
2053/*
2054 * Constant-flow memcpy from variable position in buffer.
2055 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardab9ec322020-08-25 11:43:10 +02002056 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002057 */
2058void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
2059 const unsigned char *src_base,
2060 size_t offset_secret,
2061 size_t offset_min, size_t offset_max,
2062 size_t len )
2063{
Manuel Pégourié-Gonnardab9ec322020-08-25 11:43:10 +02002064 size_t offset;
2065
2066 for( offset = offset_min; offset <= offset_max; offset++ )
2067 {
2068 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
2069 offset, offset_secret );
2070 }
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002071}
Manuel Pégourié-Gonnard1e941282020-07-28 11:35:39 +02002072#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnardfde75052020-07-28 10:19:45 +02002073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002077 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002078#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002079 size_t padlen = 0, correct = 1;
2080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002084 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002088 }
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002091
Paul Bakker48916f92012-09-16 19:57:18 +00002092 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002095 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2100 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002101 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002102 int ret;
2103 size_t olen = 0;
2104
Paul Bakker68884e32013-01-07 18:20:04 +01002105 padlen = 0;
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002108 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002109 ssl->transform_in->ivlen,
2110 ssl->in_msg, ssl->in_msglen,
2111 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002114 return( ret );
2115 }
2116
2117 if( ssl->in_msglen != olen )
2118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
Paul Bakker68884e32013-01-07 18:20:04 +01002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002125#if defined(MBEDTLS_GCM_C) || \
2126 defined(MBEDTLS_CCM_C) || \
2127 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002129 mode == MBEDTLS_MODE_CCM ||
2130 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002131 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002132 int ret;
2133 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002134 unsigned char *dec_msg;
2135 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002136 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002137 unsigned char iv[12];
2138 mbedtls_ssl_transform *transform = ssl->transform_in;
2139 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002142
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002143 /*
2144 * Compute and update sizes
2145 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002146 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002149 "+ taglen (%d)", ssl->in_msglen,
2150 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002152 }
2153 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2154
Paul Bakker68884e32013-01-07 18:20:04 +01002155 dec_msg = ssl->in_msg;
2156 dec_msg_result = ssl->in_msg;
2157 ssl->in_msglen = dec_msglen;
2158
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002159 /*
2160 * Prepare additional authenticated data
2161 */
Paul Bakker68884e32013-01-07 18:20:04 +01002162 memcpy( add_data, ssl->in_ctr, 8 );
2163 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002165 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002166 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2167 add_data[12] = ssl->in_msglen & 0xFF;
2168
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002169 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002170
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002171 /*
2172 * Prepare IV
2173 */
2174 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2175 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002176 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002177 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2178 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002179
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002180 }
2181 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2182 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002183 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 unsigned char i;
2185
2186 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2187
2188 for( i = 0; i < 8; i++ )
2189 iv[i+4] ^= ssl->in_ctr[i];
2190 }
2191 else
2192 {
2193 /* Reminder if we ever add an AEAD mode with a different size */
2194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2196 }
2197
2198 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002200
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002201 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002202 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002206 add_data, 13,
2207 dec_msg, dec_msglen,
2208 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002209 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2214 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002215
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002216 return( ret );
2217 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002218 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002219
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002220 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002221 {
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 );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002224 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002228#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 {
Paul Bakker45829992013-01-03 14:52:21 +01002231 /*
2232 * Decrypt and check the padding
2233 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002234 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002235 unsigned char *dec_msg;
2236 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002237 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002238 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002239 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 /*
Paul Bakker45829992013-01-03 14:52:21 +01002242 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2245 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002246 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002247#endif
Paul Bakker45829992013-01-03 14:52:21 +01002248
2249 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2250 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002253 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2254 ssl->transform_in->ivlen,
2255 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002257 }
2258
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002259 dec_msglen = ssl->in_msglen;
2260 dec_msg = ssl->in_msg;
2261 dec_msg_result = ssl->in_msg;
2262
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002263 /*
2264 * Authenticate before decrypt if enabled
2265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2267 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002268 {
Hanno Becker992b6872017-11-09 18:57:39 +00002269 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002270 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002273
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002274 dec_msglen -= ssl->transform_in->maclen;
2275 ssl->in_msglen -= ssl->transform_in->maclen;
2276
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002277 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2278 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2279 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2280 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002283
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002284 ret = mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2285 if( ret != 0 )
2286 goto hmac_failed_etm_enabled;
2287 ret = mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
2288 ssl->in_iv, ssl->in_msglen );
2289 if( ret != 0 )
2290 goto hmac_failed_etm_enabled;
2291 ret = mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
2292 if( ret != 0 )
2293 goto hmac_failed_etm_enabled;
2294 ret = mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
2295 if( ret != 0 )
2296 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002299 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002300 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002301 ssl->transform_in->maclen );
2302
Hanno Becker992b6872017-11-09 18:57:39 +00002303 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2304 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307
Gilles Peskine957e3832021-12-10 21:33:21 +01002308 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
2309 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 auth_done++;
Gilles Peskine957e3832021-12-10 21:33:21 +01002312
2313 hmac_failed_etm_enabled:
2314 mbedtls_platform_zeroize( mac_expect, ssl->transform_in->maclen );
2315 if( ret != 0 )
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002316 {
2317 if( ret != MBEDTLS_ERR_SSL_INVALID_MAC )
2318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_hmac_xxx", ret );
Gilles Peskine957e3832021-12-10 21:33:21 +01002319 return( ret );
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002320 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002323
2324 /*
2325 * Check length sanity
2326 */
2327 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002330 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002332 }
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002335 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002336 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002339 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002341 dec_msglen -= ssl->transform_in->ivlen;
2342 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002343
Paul Bakker48916f92012-09-16 19:57:18 +00002344 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002345 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002350 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002351 ssl->transform_in->ivlen,
2352 dec_msg, dec_msglen,
2353 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002356 return( ret );
2357 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002358
Paul Bakkercca5b812013-08-31 17:40:26 +02002359 if( dec_msglen != olen )
2360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002363 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2366 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002367 {
2368 /*
2369 * Save IV in SSL3 and TLS1
2370 */
2371 memcpy( ssl->transform_in->iv_dec,
2372 ssl->transform_in->cipher_ctx_dec.iv,
2373 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002378
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002379 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002380 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SSL_DEBUG_ALL)
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002384 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002385#endif
Paul Bakker45829992013-01-03 14:52:21 +01002386 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002387 correct = 0;
2388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_PROTO_SSL3)
2391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 {
Paul Bakker48916f92012-09-16 19:57:18 +00002393 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_SSL_DEBUG_ALL)
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002398 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002399#endif
Paul Bakker45829992013-01-03 14:52:21 +01002400 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402 }
2403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2405#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2406 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2407 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 /*
Paul Bakker45829992013-01-03 14:52:21 +01002410 * TLSv1+: always check the padding up to the first failure
2411 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002413 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002414 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002415 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002416
Paul Bakker956c9e02013-12-19 14:42:28 +01002417 /*
2418 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002419 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002420 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002421 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002422 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002423 *
2424 * In both cases we reset padding_idx to a safe value (0) to
2425 * prevent out-of-buffer reads.
2426 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002427 correct &= ( padlen <= ssl->in_msglen );
2428 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002429 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002430
2431 padding_idx *= correct;
2432
Angus Grattonb512bc12018-06-19 15:57:50 +10002433 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002434 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002435 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002436 pad_count += real_count *
2437 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2438 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002439
2440 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002443 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002445#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002446 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2450 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002454 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002455
2456 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002458 else
Manuel Pégourié-Gonnarda60d0f22020-07-28 09:55:33 +02002459#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002465#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 * Authenticate if not done yet.
2472 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002474#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002478 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Gilles Peskine957e3832021-12-10 21:33:21 +01002479 int ret = 0;
Paul Bakker1e5369c2013-12-19 16:40:57 +01002480
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002483 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2484 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_SSL_PROTO_SSL3)
2487 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002488 {
Gilles Peskinea89bdf02021-12-10 21:35:10 +01002489 ret = ssl_mac( &ssl->transform_in->md_ctx_dec,
2490 ssl->transform_in->mac_dec,
2491 ssl->in_msg, ssl->in_msglen,
2492 ssl->in_ctr, ssl->in_msgtype,
2493 mac_expect );
2494 if( ret != 0 )
2495 {
2496 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_mac", ret );
2497 return( ret );
2498 }
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002499 memcpy( mac_peer, ssl->in_msg + ssl->in_msglen,
2500 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002501 }
2502 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2504#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2505 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2506 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002507 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002508 unsigned char add_data[13];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002509
2510 /*
2511 * The next two sizes are the minimum and maximum values of
2512 * in_msglen over all padlen values.
2513 *
2514 * They're independent of padlen, since we previously did
2515 * in_msglen -= padlen.
2516 *
2517 * Note that max_len + maclen is never more than the buffer
2518 * length, as we previously did in_msglen -= maclen too.
2519 */
2520 const size_t max_len = ssl->in_msglen + padlen;
2521 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2522
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002523 memcpy( add_data + 0, ssl->in_ctr, 8 );
2524 memcpy( add_data + 8, ssl->in_hdr, 3 );
2525 memcpy( add_data + 11, ssl->in_len, 2 );
2526
2527 ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec,
2528 add_data, sizeof( add_data ),
2529 ssl->in_msg, ssl->in_msglen,
2530 min_len, max_len,
2531 mac_expect );
2532 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02002533 {
Manuel Pégourié-Gonnard368fc652020-07-28 10:43:03 +02002534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
Gilles Peskine957e3832021-12-10 21:33:21 +01002535 goto hmac_failed_etm_disabled;
Gilles Peskine20b44082018-05-29 14:06:49 +02002536 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002537
Manuel Pégourié-Gonnard590b2d92020-08-25 11:18:11 +02002538 mbedtls_ssl_cf_memcpy_offset( mac_peer, ssl->in_msg,
2539 ssl->in_msglen,
2540 min_len, max_len,
2541 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002542 }
2543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2545 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002551#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002552 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002553 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002554#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Manuel Pégourié-Gonnardce45d1a2020-08-25 11:07:25 +02002556 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker992b6872017-11-09 18:57:39 +00002557 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#if defined(MBEDTLS_SSL_DEBUG_ALL)
2560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002561#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002562 correct = 0;
2563 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 auth_done++;
Gilles Peskine957e3832021-12-10 21:33:21 +01002565
2566 hmac_failed_etm_disabled:
2567 mbedtls_platform_zeroize( mac_peer, ssl->transform_in->maclen );
2568 mbedtls_platform_zeroize( mac_expect, ssl->transform_in->maclen );
2569 if( ret != 0 )
2570 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002572
2573 /*
2574 * Finally check the correct flag
2575 */
2576 if( correct == 0 )
2577 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002578#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002579
2580 /* Make extra sure authentication was performed, exactly once */
2581 if( auth_done != 1 )
2582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 if( ssl->in_msglen == 0 )
2588 {
Angus Gratton34817922018-06-19 15:58:22 +10002589#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2590 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2591 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2592 {
2593 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2595 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2596 }
2597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2598
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 ssl->nb_zero++;
2600
2601 /*
2602 * Three or more empty messages may be a DoS attack
2603 * (excessive CPU consumption).
2604 */
2605 if( ssl->nb_zero > 3 )
2606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611 }
2612 else
2613 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002617 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002618 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002619 }
2620 else
2621#endif
2622 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002624 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2625 if( ++ssl->in_ctr[i - 1] != 0 )
2626 break;
2627
2628 /* The loop goes to its end iff the counter is wrapping */
2629 if( i == ssl_ep_len( ssl ) )
2630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2632 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002633 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002634 }
2635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
2638 return( 0 );
2639}
2640
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002641#undef MAC_NONE
2642#undef MAC_PLAINTEXT
2643#undef MAC_CIPHERTEXT
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002646/*
2647 * Compression/decompression functions
2648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002650{
2651 int ret;
2652 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002653 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002654 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002655 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002658
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002659 if( len_pre == 0 )
2660 return( 0 );
2661
Paul Bakker2770fbd2012-07-03 13:30:23 +00002662 memcpy( msg_pre, ssl->out_msg, len_pre );
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002665 ssl->out_msglen ) );
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002668 ssl->out_msg, ssl->out_msglen );
2669
Paul Bakker48916f92012-09-16 19:57:18 +00002670 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2671 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2672 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002673 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002674
Paul Bakker48916f92012-09-16 19:57:18 +00002675 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002676 if( ret != Z_OK )
2677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2679 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002680 }
2681
Angus Grattond8213d02016-05-25 20:56:48 +10002682 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002683 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002686 ssl->out_msglen ) );
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002689 ssl->out_msg, ssl->out_msglen );
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002692
2693 return( 0 );
2694}
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002697{
2698 int ret;
2699 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002700 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002701 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002702 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002706 if( len_pre == 0 )
2707 return( 0 );
2708
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 memcpy( msg_pre, ssl->in_msg, len_pre );
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712 ssl->in_msglen ) );
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002715 ssl->in_msg, ssl->in_msglen );
2716
Paul Bakker48916f92012-09-16 19:57:18 +00002717 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2718 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2719 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002720 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002721 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002722
Paul Bakker48916f92012-09-16 19:57:18 +00002723 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724 if( ret != Z_OK )
2725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2727 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728 }
2729
Angus Grattond8213d02016-05-25 20:56:48 +10002730 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002731 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734 ssl->in_msglen ) );
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002737 ssl->in_msg, ssl->in_msglen );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740
2741 return( 0 );
2742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2746static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_SSL_PROTO_DTLS)
2749static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002750{
2751 /* If renegotiation is not enforced, retransmit until we would reach max
2752 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002753 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002754 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002755 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002756 unsigned char doublings = 1;
2757
2758 while( ratio != 0 )
2759 {
2760 ++doublings;
2761 ratio >>= 1;
2762 }
2763
2764 if( ++ssl->renego_records_seen > doublings )
2765 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002767 return( 0 );
2768 }
2769 }
2770
2771 return( ssl_write_hello_request( ssl ) );
2772}
2773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002777 * Fill the input message buffer by appending data to it.
2778 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002779 *
2780 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2781 * available (from this read and/or a previous one). Otherwise, an error code
2782 * is returned (possibly EOF or WANT_READ).
2783 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002784 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2785 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2786 * since we always read a whole datagram at once.
2787 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002788 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002789 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792{
Paul Bakker23986e52011-04-24 08:57:21 +00002793 int ret;
2794 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002798 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002801 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002803 }
2804
Angus Grattond8213d02016-05-25 20:56:48 +10002805 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002809 }
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002814 uint32_t timeout;
2815
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002816 /* Just to be sure */
2817 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2818 {
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2820 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2822 }
2823
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002824 /*
2825 * The point is, we need to always read a full datagram at once, so we
2826 * sometimes read more then requested, and handle the additional data.
2827 * It could be the rest of the current record (while fetching the
2828 * header) and/or some other records in the same datagram.
2829 */
2830
2831 /*
2832 * Move to the next record in the already read datagram if applicable
2833 */
2834 if( ssl->next_record_offset != 0 )
2835 {
2836 if( ssl->in_left < ssl->next_record_offset )
2837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002840 }
2841
2842 ssl->in_left -= ssl->next_record_offset;
2843
2844 if( ssl->in_left != 0 )
2845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002847 ssl->next_record_offset ) );
2848 memmove( ssl->in_hdr,
2849 ssl->in_hdr + ssl->next_record_offset,
2850 ssl->in_left );
2851 }
2852
2853 ssl->next_record_offset = 0;
2854 }
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002858
2859 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002860 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002861 */
2862 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002865 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002866 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002867
2868 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01002869 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002870 * are not at the beginning of a new record, the caller did something
2871 * wrong.
2872 */
2873 if( ssl->in_left != 0 )
2874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002877 }
2878
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002879 /*
2880 * Don't even try to read if time's out already.
2881 * This avoids by-passing the timer when repeatedly receiving messages
2882 * that will end up being dropped.
2883 */
2884 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002885 {
2886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002887 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002888 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002889 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002890 {
Angus Grattond8213d02016-05-25 20:56:48 +10002891 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002894 timeout = ssl->handshake->retransmit_timeout;
2895 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002896 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002899
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002900 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002901 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2902 timeout );
2903 else
2904 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002907
2908 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002910 }
2911
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002912 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002915 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002918 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002919 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002922 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002923 }
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002928 return( ret );
2929 }
2930
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002931 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002934 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002936 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002937 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002940 return( ret );
2941 }
2942
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002943 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002946 }
2947
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 if( ret < 0 )
2949 return( ret );
2950
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002951 ssl->in_left = ret;
2952 }
2953 else
2954#endif
2955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002957 ssl->in_left, nb_want ) );
2958
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002959 while( ssl->in_left < nb_want )
2960 {
2961 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002962
2963 if( ssl_check_timer( ssl ) != 0 )
2964 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2965 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002966 {
2967 if( ssl->f_recv_timeout != NULL )
2968 {
2969 ret = ssl->f_recv_timeout( ssl->p_bio,
2970 ssl->in_hdr + ssl->in_left, len,
2971 ssl->conf->read_timeout );
2972 }
2973 else
2974 {
2975 ret = ssl->f_recv( ssl->p_bio,
2976 ssl->in_hdr + ssl->in_left, len );
2977 }
2978 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002981 ssl->in_left, nb_want ) );
2982 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002983
2984 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002986
2987 if( ret < 0 )
2988 return( ret );
2989
makise-homura8c3fa632020-08-24 18:33:54 +03002990 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002991 {
Darryl Green11999bb2018-03-13 15:22:58 +00002992 MBEDTLS_SSL_DEBUG_MSG( 1,
2993 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002994 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2996 }
2997
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002998 ssl->in_left += ret;
2999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 }
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
3004 return( 0 );
3005}
3006
3007/*
3008 * Flush any data not yet written
3009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003012 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003013 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 if( ssl->f_send == NULL )
3018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003020 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003022 }
3023
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003024 /* Avoid incrementing counter if data is flushed */
3025 if( ssl->out_left == 0 )
3026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003028 return( 0 );
3029 }
3030
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 while( ssl->out_left > 0 )
3032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3034 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Hanno Becker2b1e3542018-08-06 11:19:13 +01003036 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003037 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
3041 if( ret <= 0 )
3042 return( ret );
3043
makise-homura8c3fa632020-08-24 18:33:54 +03003044 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003045 {
Darryl Green11999bb2018-03-13 15:22:58 +00003046 MBEDTLS_SSL_DEBUG_MSG( 1,
3047 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003048 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3050 }
3051
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 ssl->out_left -= ret;
3053 }
3054
Hanno Becker2b1e3542018-08-06 11:19:13 +01003055#if defined(MBEDTLS_SSL_PROTO_DTLS)
3056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003057 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003058 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003059 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003060 else
3061#endif
3062 {
3063 ssl->out_hdr = ssl->out_buf + 8;
3064 }
3065 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
3069 return( 0 );
3070}
3071
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003072/*
3073 * Functions to handle the DTLS retransmission state machine
3074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003076/*
3077 * Append current handshake message to current outgoing flight
3078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3083 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3084 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003085
3086 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003087 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003088 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003091 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003092 }
3093
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003094 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003095 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003098 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099 }
3100
3101 /* Copy current handshake message with headers */
3102 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3103 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003104 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105 msg->next = NULL;
3106
3107 /* Append to the current flight */
3108 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003109 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003110 else
3111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113 while( cur->next != NULL )
3114 cur = cur->next;
3115 cur->next = msg;
3116 }
3117
Hanno Becker3b235902018-08-06 09:54:53 +01003118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003119 return( 0 );
3120}
3121
3122/*
3123 * Free the current flight of handshake messages
3124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 mbedtls_ssl_flight_item *cur = flight;
3128 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130 while( cur != NULL )
3131 {
3132 next = cur->next;
3133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 mbedtls_free( cur->p );
3135 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136
3137 cur = next;
3138 }
3139}
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3142static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003143#endif
3144
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003145/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003146 * Swap transform_out and out_ctr with the alternative ones
3147 */
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003148static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003149{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003151 unsigned char tmp_out_ctr[8];
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003152#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3153 int ret;
3154#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003155
3156 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003159 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003160 }
3161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003163
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003164 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003165 tmp_transform = ssl->transform_out;
3166 ssl->transform_out = ssl->handshake->alt_transform_out;
3167 ssl->handshake->alt_transform_out = tmp_transform;
3168
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003169 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003170 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3171 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003172 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003173
3174 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003175 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3178 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3183 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003184 }
3185 }
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003186#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
3187
3188 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003189}
3190
3191/*
3192 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003193 */
3194int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3195{
3196 int ret = 0;
3197
3198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3199
3200 ret = mbedtls_ssl_flight_transmit( ssl );
3201
3202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3203
3204 return( ret );
3205}
3206
3207/*
3208 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003209 *
3210 * Need to remember the current message in case flush_output returns
3211 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003212 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003213 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003214int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003215{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003216 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003220 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003222
3223 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003224 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003225 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3226 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003229 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003230
3231 while( ssl->handshake->cur_msg != NULL )
3232 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003233 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003234 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003235
Hanno Beckere1dcb032018-08-17 16:47:58 +01003236 int const is_finished =
3237 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3238 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3239
Hanno Becker04da1892018-08-14 13:22:10 +01003240 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3241 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3242
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003243 /* Swap epochs before sending Finished: we can't do it after
3244 * sending ChangeCipherSpec, in case write returns WANT_READ.
3245 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003246 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003247 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003249 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3250 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003251 }
3252
Hanno Becker67bc7c32018-08-06 11:33:50 +01003253 ret = ssl_get_remaining_payload_in_datagram( ssl );
3254 if( ret < 0 )
3255 return( ret );
3256 max_frag_len = (size_t) ret;
3257
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003258 /* CCS is copied as is, while HS messages may need fragmentation */
3259 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3260 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003261 if( max_frag_len == 0 )
3262 {
3263 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3264 return( ret );
3265
3266 continue;
3267 }
3268
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003269 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003270 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003271 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003272
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003273 /* Update position inside current message */
3274 ssl->handshake->cur_msg_p += cur->len;
3275 }
3276 else
3277 {
3278 const unsigned char * const p = ssl->handshake->cur_msg_p;
3279 const size_t hs_len = cur->len - 12;
3280 const size_t frag_off = p - ( cur->p + 12 );
3281 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003282 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003283
Hanno Beckere1dcb032018-08-17 16:47:58 +01003284 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003285 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003286 if( is_finished )
Andres Amaya Garcia52dbda62018-12-05 21:57:57 +00003287 {
3288 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
3289 return( ret );
3290 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291
Hanno Becker67bc7c32018-08-06 11:33:50 +01003292 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3293 return( ret );
3294
3295 continue;
3296 }
3297 max_hs_frag_len = max_frag_len - 12;
3298
3299 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3300 max_hs_frag_len : rem_len;
3301
3302 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003303 {
3304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003305 (unsigned) cur_hs_frag_len,
3306 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003307 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003308
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003309 /* Messages are stored with handshake headers as if not fragmented,
3310 * copy beginning of headers then fill fragmentation fields.
3311 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3312 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003313
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003314 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3315 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3316 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3317
Hanno Becker67bc7c32018-08-06 11:33:50 +01003318 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3319 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3320 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003321
3322 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3323
Hanno Becker3f7b9732018-08-28 09:53:25 +01003324 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003325 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3326 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003327 ssl->out_msgtype = cur->type;
3328
3329 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003330 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003331 }
3332
3333 /* If done with the current message move to the next one if any */
3334 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3335 {
3336 if( cur->next != NULL )
3337 {
3338 ssl->handshake->cur_msg = cur->next;
3339 ssl->handshake->cur_msg_p = cur->next->p + 12;
3340 }
3341 else
3342 {
3343 ssl->handshake->cur_msg = NULL;
3344 ssl->handshake->cur_msg_p = NULL;
3345 }
3346 }
3347
3348 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003349 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 return( ret );
3353 }
3354 }
3355
Hanno Becker67bc7c32018-08-06 11:33:50 +01003356 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3357 return( ret );
3358
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003359 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3361 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003362 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003365 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3366 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003367
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003369
3370 return( 0 );
3371}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372
3373/*
3374 * To be called when the last message of an incoming flight is received.
3375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003377{
3378 /* We won't need to resend that one any more */
3379 ssl_flight_free( ssl->handshake->flight );
3380 ssl->handshake->flight = NULL;
3381 ssl->handshake->cur_msg = NULL;
3382
3383 /* The next incoming flight will start with this msg_seq */
3384 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3385
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003386 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003387 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003388
Hanno Becker0271f962018-08-16 13:23:47 +01003389 /* Clear future message buffering structure. */
3390 ssl_buffering_free( ssl );
3391
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003392 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003393 ssl_set_timer( ssl, 0 );
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3396 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003399 }
3400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003402}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003403
3404/*
3405 * To be called when the last message of an outgoing flight is send.
3406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003408{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003409 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003410 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3413 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003416 }
3417 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003421
Paul Bakker5121ce52009-01-03 21:22:43 +00003422/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003423 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003424 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003425
3426/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003427 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003428 *
3429 * - fill in handshake headers
3430 * - update handshake checksum
3431 * - DTLS: save message for resending
3432 * - then pass to the record layer
3433 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003434 * DTLS: except for HelloRequest, messages are only queued, and will only be
3435 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003436 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003437 * Inputs:
3438 * - ssl->out_msglen: 4 + actual handshake message len
3439 * (4 is the size of handshake headers for TLS)
3440 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3441 * - ssl->out_msg + 4: the handshake message body
3442 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003443 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003444 * - ssl->out_msglen: the length of the record contents
3445 * (including handshake headers but excluding record headers)
3446 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003448int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003450 int ret;
3451 const size_t hs_len = ssl->out_msglen - 4;
3452 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3455
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003456 /*
3457 * Sanity checks
3458 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003459 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003460 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3461 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003462 /* In SSLv3, the client might send a NoCertificate alert. */
3463#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3464 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3465 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3466 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3467#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3468 {
3469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3471 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003474 /* Whenever we send anything different from a
3475 * HelloRequest we should be in a handshake - double check. */
3476 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3477 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003478 ssl->handshake == NULL )
3479 {
3480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003486 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003491 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003492#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003493
Hanno Beckerb50a2532018-08-06 11:52:54 +01003494 /* Double-check that we did not exceed the bounds
3495 * of the outgoing record buffer.
3496 * This should never fail as the various message
3497 * writing functions must obey the bounds of the
3498 * outgoing record buffer, but better be safe.
3499 *
3500 * Note: We deliberately do not check for the MTU or MFL here.
3501 */
3502 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3503 {
3504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3505 "size %u, maximum %u",
3506 (unsigned) ssl->out_msglen,
3507 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3509 }
3510
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003511 /*
3512 * Fill handshake headers
3513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003516 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3517 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3518 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003519
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003520 /*
3521 * DTLS has additional fields in the Handshake layer,
3522 * between the length field and the actual payload:
3523 * uint16 message_seq;
3524 * uint24 fragment_offset;
3525 * uint24 fragment_length;
3526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003529 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003530 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003531 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3534 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003535 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003536 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3538 }
3539
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003540 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003541 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003542
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003543 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003544 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003545 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003546 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3547 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3548 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003549 }
3550 else
3551 {
3552 ssl->out_msg[4] = 0;
3553 ssl->out_msg[5] = 0;
3554 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003555
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003556 /* Handshake hashes are computed without fragmentation,
3557 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003558 memset( ssl->out_msg + 6, 0x00, 3 );
3559 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003562
Hanno Becker0207e532018-08-28 10:28:28 +01003563 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003564 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3565 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 }
3567
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003568 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003571 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3572 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573 {
3574 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577 return( ret );
3578 }
3579 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003580 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003582 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003583 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003584 {
3585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3586 return( ret );
3587 }
3588 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003589
3590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3591
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003592 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003593}
3594
3595/*
3596 * Record layer functions
3597 */
3598
3599/*
3600 * Write current record.
3601 *
3602 * Uses:
3603 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3604 * - ssl->out_msglen: length of the record content (excl headers)
3605 * - ssl->out_msg: record content
3606 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003607int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003608{
3609 int ret, done = 0;
3610 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003611 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003612
3613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003616 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003618 {
3619 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003622 return( ret );
3623 }
3624
3625 len = ssl->out_msglen;
3626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3630 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 ret = mbedtls_ssl_hw_record_write( ssl );
3635 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3638 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003639 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003640
3641 if( ret == 0 )
3642 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003645 if( !done )
3646 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003647 unsigned i;
3648 size_t protected_record_size;
3649
Paul Bakker05ef8352012-05-08 09:17:57 +00003650 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003652 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003653
Hanno Becker19859472018-08-06 09:40:20 +01003654 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003655 ssl->out_len[0] = (unsigned char)( len >> 8 );
3656 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003657
Paul Bakker48916f92012-09-16 19:57:18 +00003658 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003659 {
3660 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003663 return( ret );
3664 }
3665
3666 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003667 ssl->out_len[0] = (unsigned char)( len >> 8 );
3668 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003669 }
3670
Hanno Becker2b1e3542018-08-06 11:19:13 +01003671 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3672
3673#if defined(MBEDTLS_SSL_PROTO_DTLS)
3674 /* In case of DTLS, double-check that we don't exceed
3675 * the remaining space in the datagram. */
3676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3677 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003678 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003679 if( ret < 0 )
3680 return( ret );
3681
3682 if( protected_record_size > (size_t) ret )
3683 {
3684 /* Should never happen */
3685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3686 }
3687 }
3688#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003691 "version = [%d:%d], msglen = %d",
3692 ssl->out_hdr[0], ssl->out_hdr[1],
3693 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003696 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003697
3698 ssl->out_left += protected_record_size;
3699 ssl->out_hdr += protected_record_size;
3700 ssl_update_out_pointers( ssl, ssl->transform_out );
3701
Hanno Becker04484622018-08-06 09:49:38 +01003702 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3703 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3704 break;
3705
3706 /* The loop goes to its end iff the counter is wrapping */
3707 if( i == ssl_ep_len( ssl ) )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3710 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 }
3713
Hanno Becker67bc7c32018-08-06 11:33:50 +01003714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3716 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003717 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003718 size_t remaining;
3719 ret = ssl_get_remaining_payload_in_datagram( ssl );
3720 if( ret < 0 )
3721 {
3722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3723 ret );
3724 return( ret );
3725 }
3726
3727 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003728 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003729 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003730 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003731 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732 else
3733 {
Hanno Becker513815a2018-08-20 11:56:09 +01003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735 }
3736 }
3737#endif /* MBEDTLS_SSL_PROTO_DTLS */
3738
3739 if( ( flush == SSL_FORCE_FLUSH ) &&
3740 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 return( ret );
3744 }
3745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
3748 return( 0 );
3749}
3750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003752
3753static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3754{
3755 if( ssl->in_msglen < ssl->in_hslen ||
3756 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3757 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3758 {
3759 return( 1 );
3760 }
3761 return( 0 );
3762}
Hanno Becker44650b72018-08-16 12:51:11 +01003763
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003764static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003765{
3766 return( ( ssl->in_msg[9] << 16 ) |
3767 ( ssl->in_msg[10] << 8 ) |
3768 ssl->in_msg[11] );
3769}
3770
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003771static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003772{
3773 return( ( ssl->in_msg[6] << 16 ) |
3774 ( ssl->in_msg[7] << 8 ) |
3775 ssl->in_msg[8] );
3776}
3777
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003778static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003779{
3780 uint32_t msg_len, frag_off, frag_len;
3781
3782 msg_len = ssl_get_hs_total_len( ssl );
3783 frag_off = ssl_get_hs_frag_off( ssl );
3784 frag_len = ssl_get_hs_frag_len( ssl );
3785
3786 if( frag_off > msg_len )
3787 return( -1 );
3788
3789 if( frag_len > msg_len - frag_off )
3790 return( -1 );
3791
3792 if( frag_len + 12 > ssl->in_msglen )
3793 return( -1 );
3794
3795 return( 0 );
3796}
3797
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003798/*
3799 * Mark bits in bitmask (used for DTLS HS reassembly)
3800 */
3801static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3802{
3803 unsigned int start_bits, end_bits;
3804
3805 start_bits = 8 - ( offset % 8 );
3806 if( start_bits != 8 )
3807 {
3808 size_t first_byte_idx = offset / 8;
3809
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003810 /* Special case */
3811 if( len <= start_bits )
3812 {
3813 for( ; len != 0; len-- )
3814 mask[first_byte_idx] |= 1 << ( start_bits - len );
3815
3816 /* Avoid potential issues with offset or len becoming invalid */
3817 return;
3818 }
3819
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003820 offset += start_bits; /* Now offset % 8 == 0 */
3821 len -= start_bits;
3822
3823 for( ; start_bits != 0; start_bits-- )
3824 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3825 }
3826
3827 end_bits = len % 8;
3828 if( end_bits != 0 )
3829 {
3830 size_t last_byte_idx = ( offset + len ) / 8;
3831
3832 len -= end_bits; /* Now len % 8 == 0 */
3833
3834 for( ; end_bits != 0; end_bits-- )
3835 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3836 }
3837
3838 memset( mask + offset / 8, 0xFF, len / 8 );
3839}
3840
3841/*
3842 * Check that bitmask is full
3843 */
3844static int ssl_bitmask_check( unsigned char *mask, size_t len )
3845{
3846 size_t i;
3847
3848 for( i = 0; i < len / 8; i++ )
3849 if( mask[i] != 0xFF )
3850 return( -1 );
3851
3852 for( i = 0; i < len % 8; i++ )
3853 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3854 return( -1 );
3855
3856 return( 0 );
3857}
3858
Hanno Becker56e205e2018-08-16 09:06:12 +01003859/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003860static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003861 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003862{
Hanno Becker56e205e2018-08-16 09:06:12 +01003863 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003864
Hanno Becker56e205e2018-08-16 09:06:12 +01003865 alloc_len = 12; /* Handshake header */
3866 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003867
Hanno Beckerd07df862018-08-16 09:14:58 +01003868 if( add_bitmap )
3869 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003870
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003871 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003872}
Hanno Becker56e205e2018-08-16 09:06:12 +01003873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003875
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003876static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003877{
3878 return( ( ssl->in_msg[1] << 16 ) |
3879 ( ssl->in_msg[2] << 8 ) |
3880 ssl->in_msg[3] );
3881}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003882
Simon Butcher99000142016-10-13 17:21:01 +01003883int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003888 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003890 }
3891
Hanno Becker12555c62018-08-16 12:47:53 +01003892 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003895 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003896 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003900 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003901 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003902 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003903
Hanno Becker44650b72018-08-16 12:51:11 +01003904 if( ssl_check_hs_header( ssl ) != 0 )
3905 {
3906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3907 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3908 }
3909
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003910 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003911 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3912 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3913 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3914 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003915 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003916 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3917 {
3918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3919 recv_msg_seq,
3920 ssl->handshake->in_msg_seq ) );
3921 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3922 }
3923
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003924 /* Retransmit only on last message from previous flight, to avoid
3925 * too many retransmissions.
3926 * Besides, No sane server ever retransmits HelloVerifyRequest */
3927 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003931 "message_seq = %d, start_of_flight = %d",
3932 recv_msg_seq,
3933 ssl->handshake->in_flight_start_seq ) );
3934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003938 return( ret );
3939 }
3940 }
3941 else
3942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003944 "message_seq = %d, expected = %d",
3945 recv_msg_seq,
3946 ssl->handshake->in_msg_seq ) );
3947 }
3948
Hanno Becker90333da2017-10-10 11:27:13 +01003949 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003950 }
3951 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003952
Hanno Becker6d97ef52018-08-16 13:09:04 +01003953 /* Message reassembly is handled alongside buffering of future
3954 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003955 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003956 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003957 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003960 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003961 }
3962 }
3963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003965 /* With TLS we don't handle fragmentation (for now) */
3966 if( ssl->in_msglen < ssl->in_hslen )
3967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3969 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003970 }
3971
Simon Butcher99000142016-10-13 17:21:01 +01003972 return( 0 );
3973}
3974
3975void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3976{
Hanno Becker0271f962018-08-16 13:23:47 +01003977 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003978
Hanno Becker0271f962018-08-16 13:23:47 +01003979 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003980 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003981 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003982 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003983
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003984 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003987 ssl->handshake != NULL )
3988 {
Hanno Becker0271f962018-08-16 13:23:47 +01003989 unsigned offset;
3990 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991
Hanno Becker0271f962018-08-16 13:23:47 +01003992 /* Increment handshake sequence number */
3993 hs->in_msg_seq++;
3994
3995 /*
3996 * Clear up handshake buffering and reassembly structure.
3997 */
3998
3999 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004000 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004001
4002 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004003 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4004 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004005 offset++, hs_buf++ )
4006 {
4007 *hs_buf = *(hs_buf + 1);
4008 }
4009
4010 /* Create a fresh last entry */
4011 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004012 }
4013#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004014}
4015
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004016/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004017 * DTLS anti-replay: RFC 6347 4.1.2.6
4018 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004019 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4020 * Bit n is set iff record number in_window_top - n has been seen.
4021 *
4022 * Usually, in_window_top is the last record number seen and the lsb of
4023 * in_window is set. The only exception is the initial state (record number 0
4024 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4027static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004028{
4029 ssl->in_window_top = 0;
4030 ssl->in_window = 0;
4031}
4032
4033static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4034{
4035 return( ( (uint64_t) buf[0] << 40 ) |
4036 ( (uint64_t) buf[1] << 32 ) |
4037 ( (uint64_t) buf[2] << 24 ) |
4038 ( (uint64_t) buf[3] << 16 ) |
4039 ( (uint64_t) buf[4] << 8 ) |
4040 ( (uint64_t) buf[5] ) );
4041}
4042
4043/*
4044 * Return 0 if sequence number is acceptable, -1 otherwise
4045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004047{
4048 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4049 uint64_t bit;
4050
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004051 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004052 return( 0 );
4053
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004054 if( rec_seqnum > ssl->in_window_top )
4055 return( 0 );
4056
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004057 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004058
4059 if( bit >= 64 )
4060 return( -1 );
4061
4062 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4063 return( -1 );
4064
4065 return( 0 );
4066}
4067
4068/*
4069 * Update replay window on new validated record
4070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004072{
4073 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4074
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004075 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004076 return;
4077
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004078 if( rec_seqnum > ssl->in_window_top )
4079 {
4080 /* Update window_top and the contents of the window */
4081 uint64_t shift = rec_seqnum - ssl->in_window_top;
4082
4083 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004084 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004085 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004086 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004087 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004088 ssl->in_window |= 1;
4089 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004090
4091 ssl->in_window_top = rec_seqnum;
4092 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004093 else
4094 {
4095 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004096 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004097
4098 if( bit < 64 ) /* Always true, but be extra sure */
4099 ssl->in_window |= (uint64_t) 1 << bit;
4100 }
4101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004103
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004104#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004105/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004106static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4107
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004108/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004109 * Without any SSL context, check if a datagram looks like a ClientHello with
4110 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004111 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004112 *
4113 * - if cookie is valid, return 0
4114 * - if ClientHello looks superficially valid but cookie is not,
4115 * fill obuf and set olen, then
4116 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4117 * - otherwise return a specific error code
4118 */
4119static int ssl_check_dtls_clihlo_cookie(
4120 mbedtls_ssl_cookie_write_t *f_cookie_write,
4121 mbedtls_ssl_cookie_check_t *f_cookie_check,
4122 void *p_cookie,
4123 const unsigned char *cli_id, size_t cli_id_len,
4124 const unsigned char *in, size_t in_len,
4125 unsigned char *obuf, size_t buf_len, size_t *olen )
4126{
4127 size_t sid_len, cookie_len;
4128 unsigned char *p;
4129
4130 if( f_cookie_write == NULL || f_cookie_check == NULL )
4131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4132
4133 /*
4134 * Structure of ClientHello with record and handshake headers,
4135 * and expected values. We don't need to check a lot, more checks will be
4136 * done when actually parsing the ClientHello - skipping those checks
4137 * avoids code duplication and does not make cookie forging any easier.
4138 *
4139 * 0-0 ContentType type; copied, must be handshake
4140 * 1-2 ProtocolVersion version; copied
4141 * 3-4 uint16 epoch; copied, must be 0
4142 * 5-10 uint48 sequence_number; copied
4143 * 11-12 uint16 length; (ignored)
4144 *
4145 * 13-13 HandshakeType msg_type; (ignored)
4146 * 14-16 uint24 length; (ignored)
4147 * 17-18 uint16 message_seq; copied
4148 * 19-21 uint24 fragment_offset; copied, must be 0
4149 * 22-24 uint24 fragment_length; (ignored)
4150 *
4151 * 25-26 ProtocolVersion client_version; (ignored)
4152 * 27-58 Random random; (ignored)
4153 * 59-xx SessionID session_id; 1 byte len + sid_len content
4154 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4155 * ...
4156 *
4157 * Minimum length is 61 bytes.
4158 */
4159 if( in_len < 61 ||
4160 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4161 in[3] != 0 || in[4] != 0 ||
4162 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4163 {
4164 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4165 }
4166
4167 sid_len = in[59];
4168 if( sid_len > in_len - 61 )
4169 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4170
4171 cookie_len = in[60 + sid_len];
4172 if( cookie_len > in_len - 60 )
4173 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4174
4175 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4176 cli_id, cli_id_len ) == 0 )
4177 {
4178 /* Valid cookie */
4179 return( 0 );
4180 }
4181
4182 /*
4183 * If we get here, we've got an invalid cookie, let's prepare HVR.
4184 *
4185 * 0-0 ContentType type; copied
4186 * 1-2 ProtocolVersion version; copied
4187 * 3-4 uint16 epoch; copied
4188 * 5-10 uint48 sequence_number; copied
4189 * 11-12 uint16 length; olen - 13
4190 *
4191 * 13-13 HandshakeType msg_type; hello_verify_request
4192 * 14-16 uint24 length; olen - 25
4193 * 17-18 uint16 message_seq; copied
4194 * 19-21 uint24 fragment_offset; copied
4195 * 22-24 uint24 fragment_length; olen - 25
4196 *
4197 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4198 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4199 *
4200 * Minimum length is 28.
4201 */
4202 if( buf_len < 28 )
4203 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4204
4205 /* Copy most fields and adapt others */
4206 memcpy( obuf, in, 25 );
4207 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4208 obuf[25] = 0xfe;
4209 obuf[26] = 0xff;
4210
4211 /* Generate and write actual cookie */
4212 p = obuf + 28;
4213 if( f_cookie_write( p_cookie,
4214 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4215 {
4216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4217 }
4218
4219 *olen = p - obuf;
4220
4221 /* Go back and fill length fields */
4222 obuf[27] = (unsigned char)( *olen - 28 );
4223
4224 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4225 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4226 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4227
4228 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4229 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4230
4231 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4232}
4233
4234/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004235 * Handle possible client reconnect with the same UDP quadruplet
4236 * (RFC 6347 Section 4.2.8).
4237 *
4238 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4239 * that looks like a ClientHello.
4240 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004241 * - if the input looks like a ClientHello without cookies,
4242 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004243 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004244 * - if the input looks like a ClientHello with a valid cookie,
4245 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004246 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004247 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004248 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004249 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004250 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4251 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004252 */
4253static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4254{
4255 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004256 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004257
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004258 ret = ssl_check_dtls_clihlo_cookie(
4259 ssl->conf->f_cookie_write,
4260 ssl->conf->f_cookie_check,
4261 ssl->conf->p_cookie,
4262 ssl->cli_id, ssl->cli_id_len,
4263 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004264 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004265
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004266 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4267
4268 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004269 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004270 int send_ret;
4271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
4272 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
4273 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08004274 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004275 * If the error is permanent we'll catch it later,
4276 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004277 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4278 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
4279 (void) send_ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004280
4281 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004282 }
4283
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004284 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004285 {
Manuel Pégourié-Gonnardb08a3342020-03-31 12:31:24 +02004286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004287 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4288 {
4289 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4290 return( ret );
4291 }
4292
4293 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004294 }
4295
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004296 return( ret );
4297}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004298#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004299
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004300/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004301 * ContentType type;
4302 * ProtocolVersion version;
4303 * uint16 epoch; // DTLS only
4304 * uint48 sequence_number; // DTLS only
4305 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004306 *
4307 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004308 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004309 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4310 *
4311 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004312 * 1. proceed with the record if this function returns 0
4313 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4314 * 3. return CLIENT_RECONNECT if this function return that value
4315 * 4. drop the whole datagram if this function returns anything else.
4316 * Point 2 is needed when the peer is resending, and we have already received
4317 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004320{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004321 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 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 +02004324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004326 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004331 ssl->in_msgtype,
4332 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004334 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4336 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4337 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4338 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004341
4342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004343 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4344 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004345 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4346#endif /* MBEDTLS_SSL_PROTO_DTLS */
4347 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4348 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004351 }
4352
4353 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004354 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4357 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 }
4359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004360 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4363 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 }
4365
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004366 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004367 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004368 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4371 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004372 }
4373
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004374 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004375 * DTLS-related tests.
4376 * Check epoch before checking length constraint because
4377 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4378 * message gets duplicated before the corresponding Finished message,
4379 * the second ChangeCipherSpec should be discarded because it belongs
4380 * to an old epoch, but not because its length is shorter than
4381 * the minimum record length for packets using the new record transform.
4382 * Note that these two kinds of failures are handled differently,
4383 * as an unexpected record is silently skipped but an invalid
4384 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004385 */
4386#if defined(MBEDTLS_SSL_PROTO_DTLS)
4387 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4388 {
4389 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4390
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004391 /* Check epoch (and sequence number) with DTLS */
4392 if( rec_epoch != ssl->in_epoch )
4393 {
4394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4395 "expected %d, received %d",
4396 ssl->in_epoch, rec_epoch ) );
4397
4398#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4399 /*
4400 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4401 * access the first byte of record content (handshake type), as we
4402 * have an active transform (possibly iv_len != 0), so use the
4403 * fact that the record header len is 13 instead.
4404 */
4405 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4406 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4407 rec_epoch == 0 &&
4408 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4409 ssl->in_left > 13 &&
4410 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4411 {
4412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4413 "from the same port" ) );
4414 return( ssl_handle_possible_reconnect( ssl ) );
4415 }
4416 else
4417#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004418 {
4419 /* Consider buffering the record. */
4420 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4421 {
4422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4423 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4424 }
4425
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004426 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004427 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004428 }
4429
4430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4431 /* Replay detection only works for the current epoch */
4432 if( rec_epoch == ssl->in_epoch &&
4433 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4436 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4437 }
4438#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004439
Hanno Becker52c6dc62017-05-26 16:07:36 +01004440 /* Drop unexpected ApplicationData records,
4441 * except at the beginning of renegotiations */
4442 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4443 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4444#if defined(MBEDTLS_SSL_RENEGOTIATION)
4445 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4446 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4447#endif
4448 )
4449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4451 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4452 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004453 }
4454#endif /* MBEDTLS_SSL_PROTO_DTLS */
4455
Hanno Becker52c6dc62017-05-26 16:07:36 +01004456
4457 /* Check length against bounds of the current transform and version */
4458 if( ssl->transform_in == NULL )
4459 {
4460 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004461 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4464 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4465 }
4466 }
4467 else
4468 {
4469 if( ssl->in_msglen < ssl->transform_in->minlen )
4470 {
4471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4472 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4473 }
4474
4475#if defined(MBEDTLS_SSL_PROTO_SSL3)
4476 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004477 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004478 {
4479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4480 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4481 }
4482#endif
4483#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4484 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4485 /*
4486 * TLS encrypted messages can have up to 256 bytes of padding
4487 */
4488 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4489 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004490 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004491 {
4492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4494 }
4495#endif
4496 }
4497
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004498 return( 0 );
4499}
Paul Bakker5121ce52009-01-03 21:22:43 +00004500
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004501/*
4502 * If applicable, decrypt (and decompress) record content
4503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004505{
4506 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4509 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4512 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 ret = mbedtls_ssl_hw_record_read( ssl );
4517 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4520 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004521 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004522
4523 if( ret == 0 )
4524 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004527 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004528 {
4529 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 return( ret );
4533 }
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 ssl->in_msg, ssl->in_msglen );
4537
Angus Grattond8213d02016-05-25 20:56:48 +10004538 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4541 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 }
4543 }
4544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004546 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004548 {
4549 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004552 return( ret );
4553 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004554 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004558 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004561 }
4562#endif
4563
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004564 return( 0 );
4565}
4566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004568
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004569/*
4570 * Read a record.
4571 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004572 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4573 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4574 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004575 */
Hanno Becker1097b342018-08-15 14:09:41 +01004576
4577/* Helper functions for mbedtls_ssl_read_record(). */
4578static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004579static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4580static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004581
Hanno Becker327c93b2018-08-15 13:56:18 +01004582int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004583 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004584{
4585 int ret;
4586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004589 if( ssl->keep_current_message == 0 )
4590 {
4591 do {
Simon Butcher99000142016-10-13 17:21:01 +01004592
Hanno Becker26994592018-08-15 14:14:59 +01004593 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004594 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004595 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004596
Hanno Beckere74d5562018-08-15 14:26:08 +01004597 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004598 {
Hanno Becker40f50842018-08-15 14:48:01 +01004599#if defined(MBEDTLS_SSL_PROTO_DTLS)
4600 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004601
Hanno Becker40f50842018-08-15 14:48:01 +01004602 /* We only check for buffered messages if the
4603 * current datagram is fully consumed. */
4604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004605 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004606 {
Hanno Becker40f50842018-08-15 14:48:01 +01004607 if( ssl_load_buffered_message( ssl ) == 0 )
4608 have_buffered = 1;
4609 }
4610
4611 if( have_buffered == 0 )
4612#endif /* MBEDTLS_SSL_PROTO_DTLS */
4613 {
4614 ret = ssl_get_next_record( ssl );
4615 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4616 continue;
4617
4618 if( ret != 0 )
4619 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004620 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004621 return( ret );
4622 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004623 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004624 }
4625
4626 ret = mbedtls_ssl_handle_message_type( ssl );
4627
Hanno Becker40f50842018-08-15 14:48:01 +01004628#if defined(MBEDTLS_SSL_PROTO_DTLS)
4629 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4630 {
4631 /* Buffer future message */
4632 ret = ssl_buffer_message( ssl );
4633 if( ret != 0 )
4634 return( ret );
4635
4636 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4637 }
4638#endif /* MBEDTLS_SSL_PROTO_DTLS */
4639
Hanno Becker90333da2017-10-10 11:27:13 +01004640 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4641 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004642
4643 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004644 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004645 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004646 return( ret );
4647 }
4648
Hanno Becker327c93b2018-08-15 13:56:18 +01004649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004650 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004651 {
4652 mbedtls_ssl_update_handshake_status( ssl );
4653 }
Simon Butcher99000142016-10-13 17:21:01 +01004654 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004655 else
Simon Butcher99000142016-10-13 17:21:01 +01004656 {
Hanno Becker02f59072018-08-15 14:00:24 +01004657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004658 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004659 }
4660
4661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4662
4663 return( 0 );
4664}
4665
Hanno Becker40f50842018-08-15 14:48:01 +01004666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004667static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004668{
Hanno Becker40f50842018-08-15 14:48:01 +01004669 if( ssl->in_left > ssl->next_record_offset )
4670 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004671
Hanno Becker40f50842018-08-15 14:48:01 +01004672 return( 0 );
4673}
4674
4675static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4676{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004677 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004678 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004679 int ret = 0;
4680
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004681 if( hs == NULL )
4682 return( -1 );
4683
Hanno Beckere00ae372018-08-20 09:39:42 +01004684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4685
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004686 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4687 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4688 {
4689 /* Check if we have seen a ChangeCipherSpec before.
4690 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004691 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004692 {
4693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4694 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004695 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004696 }
4697
Hanno Becker39b8bc92018-08-28 17:17:13 +01004698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004699 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4700 ssl->in_msglen = 1;
4701 ssl->in_msg[0] = 1;
4702
4703 /* As long as they are equal, the exact value doesn't matter. */
4704 ssl->in_left = 0;
4705 ssl->next_record_offset = 0;
4706
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004707 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708 goto exit;
4709 }
Hanno Becker37f95322018-08-16 13:55:32 +01004710
Hanno Beckerb8f50142018-08-28 10:01:34 +01004711#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004712 /* Debug only */
4713 {
4714 unsigned offset;
4715 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4716 {
4717 hs_buf = &hs->buffering.hs[offset];
4718 if( hs_buf->is_valid == 1 )
4719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4721 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004722 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004723 }
4724 }
4725 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004726#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004727
4728 /* Check if we have buffered and/or fully reassembled the
4729 * next handshake message. */
4730 hs_buf = &hs->buffering.hs[0];
4731 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4732 {
4733 /* Synthesize a record containing the buffered HS message. */
4734 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4735 ( hs_buf->data[2] << 8 ) |
4736 hs_buf->data[3];
4737
4738 /* Double-check that we haven't accidentally buffered
4739 * a message that doesn't fit into the input buffer. */
4740 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4741 {
4742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4744 }
4745
4746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4747 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4748 hs_buf->data, msg_len + 12 );
4749
4750 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4751 ssl->in_hslen = msg_len + 12;
4752 ssl->in_msglen = msg_len + 12;
4753 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4754
4755 ret = 0;
4756 goto exit;
4757 }
4758 else
4759 {
4760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4761 hs->in_msg_seq ) );
4762 }
4763
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004764 ret = -1;
4765
4766exit:
4767
4768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4769 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004770}
4771
Hanno Beckera02b0b42018-08-21 17:20:27 +01004772static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4773 size_t desired )
4774{
4775 int offset;
4776 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4778 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004779
Hanno Becker01315ea2018-08-21 17:22:17 +01004780 /* Get rid of future records epoch first, if such exist. */
4781 ssl_free_buffered_record( ssl );
4782
4783 /* Check if we have enough space available now. */
4784 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4785 hs->buffering.total_bytes_buffered ) )
4786 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004788 return( 0 );
4789 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004790
Hanno Becker4f432ad2018-08-28 10:02:32 +01004791 /* We don't have enough space to buffer the next expected handshake
4792 * message. Remove buffers used for future messages to gain space,
4793 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004794 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4795 offset >= 0; offset-- )
4796 {
4797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4798 offset ) );
4799
Hanno Beckerb309b922018-08-23 13:18:05 +01004800 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004801
4802 /* Check if we have enough space available now. */
4803 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4804 hs->buffering.total_bytes_buffered ) )
4805 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004807 return( 0 );
4808 }
4809 }
4810
4811 return( -1 );
4812}
4813
Hanno Becker40f50842018-08-15 14:48:01 +01004814static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4815{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004816 int ret = 0;
4817 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4818
4819 if( hs == NULL )
4820 return( 0 );
4821
4822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4823
4824 switch( ssl->in_msgtype )
4825 {
4826 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004828
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004829 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004830 break;
4831
4832 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004833 {
4834 unsigned recv_msg_seq_offset;
4835 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4836 mbedtls_ssl_hs_buffer *hs_buf;
4837 size_t msg_len = ssl->in_hslen - 12;
4838
4839 /* We should never receive an old handshake
4840 * message - double-check nonetheless. */
4841 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4842 {
4843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4845 }
4846
4847 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4848 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4849 {
4850 /* Silently ignore -- message too far in the future */
4851 MBEDTLS_SSL_DEBUG_MSG( 2,
4852 ( "Ignore future HS message with sequence number %u, "
4853 "buffering window %u - %u",
4854 recv_msg_seq, ssl->handshake->in_msg_seq,
4855 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4856
4857 goto exit;
4858 }
4859
4860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4861 recv_msg_seq, recv_msg_seq_offset ) );
4862
4863 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4864
4865 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004866 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004867 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004868 size_t reassembly_buf_sz;
4869
Hanno Becker37f95322018-08-16 13:55:32 +01004870 hs_buf->is_fragmented =
4871 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4872
4873 /* We copy the message back into the input buffer
4874 * after reassembly, so check that it's not too large.
4875 * This is an implementation-specific limitation
4876 * and not one from the standard, hence it is not
4877 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004878 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004879 {
4880 /* Ignore message */
4881 goto exit;
4882 }
4883
Hanno Beckere0b150f2018-08-21 15:51:03 +01004884 /* Check if we have enough space to buffer the message. */
4885 if( hs->buffering.total_bytes_buffered >
4886 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4887 {
4888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4890 }
4891
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004892 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4893 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004894
4895 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4896 hs->buffering.total_bytes_buffered ) )
4897 {
4898 if( recv_msg_seq_offset > 0 )
4899 {
4900 /* If we can't buffer a future message because
4901 * of space limitations -- ignore. */
4902 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",
4903 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4904 (unsigned) hs->buffering.total_bytes_buffered ) );
4905 goto exit;
4906 }
Hanno Beckere1801392018-08-21 16:51:05 +01004907 else
4908 {
4909 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",
4910 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4911 (unsigned) hs->buffering.total_bytes_buffered ) );
4912 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004913
Hanno Beckera02b0b42018-08-21 17:20:27 +01004914 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004915 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004916 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",
4917 (unsigned) msg_len,
4918 (unsigned) reassembly_buf_sz,
4919 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004920 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004921 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4922 goto exit;
4923 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004924 }
4925
4926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4927 msg_len ) );
4928
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004929 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4930 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004931 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004932 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004933 goto exit;
4934 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004935 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004936
4937 /* Prepare final header: copy msg_type, length and message_seq,
4938 * then add standardised fragment_offset and fragment_length */
4939 memcpy( hs_buf->data, ssl->in_msg, 6 );
4940 memset( hs_buf->data + 6, 0, 3 );
4941 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4942
4943 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004944
4945 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004946 }
4947 else
4948 {
4949 /* Make sure msg_type and length are consistent */
4950 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4951 {
4952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4953 /* Ignore */
4954 goto exit;
4955 }
4956 }
4957
Hanno Becker4422bbb2018-08-20 09:40:19 +01004958 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004959 {
4960 size_t frag_len, frag_off;
4961 unsigned char * const msg = hs_buf->data + 12;
4962
4963 /*
4964 * Check and copy current fragment
4965 */
4966
4967 /* Validation of header fields already done in
4968 * mbedtls_ssl_prepare_handshake_record(). */
4969 frag_off = ssl_get_hs_frag_off( ssl );
4970 frag_len = ssl_get_hs_frag_len( ssl );
4971
4972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4973 frag_off, frag_len ) );
4974 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4975
4976 if( hs_buf->is_fragmented )
4977 {
4978 unsigned char * const bitmask = msg + msg_len;
4979 ssl_bitmask_set( bitmask, frag_off, frag_len );
4980 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4981 msg_len ) == 0 );
4982 }
4983 else
4984 {
4985 hs_buf->is_complete = 1;
4986 }
4987
4988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4989 hs_buf->is_complete ? "" : "not yet " ) );
4990 }
4991
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004992 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004993 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004994
4995 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004996 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004997 break;
4998 }
4999
5000exit:
5001
5002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5003 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005004}
5005#endif /* MBEDTLS_SSL_PROTO_DTLS */
5006
Hanno Becker1097b342018-08-15 14:09:41 +01005007static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005008{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005009 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005010 * Consume last content-layer message and potentially
5011 * update in_msglen which keeps track of the contents'
5012 * consumption state.
5013 *
5014 * (1) Handshake messages:
5015 * Remove last handshake message, move content
5016 * and adapt in_msglen.
5017 *
5018 * (2) Alert messages:
5019 * Consume whole record content, in_msglen = 0.
5020 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005021 * (3) Change cipher spec:
5022 * Consume whole record content, in_msglen = 0.
5023 *
5024 * (4) Application data:
5025 * Don't do anything - the record layer provides
5026 * the application data as a stream transport
5027 * and consumes through mbedtls_ssl_read only.
5028 *
5029 */
5030
5031 /* Case (1): Handshake messages */
5032 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005033 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005034 /* Hard assertion to be sure that no application data
5035 * is in flight, as corrupting ssl->in_msglen during
5036 * ssl->in_offt != NULL is fatal. */
5037 if( ssl->in_offt != NULL )
5038 {
5039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5041 }
5042
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005043 /*
5044 * Get next Handshake message in the current record
5045 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005046
Hanno Becker4a810fb2017-05-24 16:27:30 +01005047 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005048 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005049 * current handshake content: If DTLS handshake
5050 * fragmentation is used, that's the fragment
5051 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005052 * size here is faulty and should be changed at
5053 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005054 * (2) While it doesn't seem to cause problems, one
5055 * has to be very careful not to assume that in_hslen
5056 * is always <= in_msglen in a sensible communication.
5057 * Again, it's wrong for DTLS handshake fragmentation.
5058 * The following check is therefore mandatory, and
5059 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005060 * Additionally, ssl->in_hslen might be arbitrarily out of
5061 * bounds after handling a DTLS message with an unexpected
5062 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005063 */
5064 if( ssl->in_hslen < ssl->in_msglen )
5065 {
5066 ssl->in_msglen -= ssl->in_hslen;
5067 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5068 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005069
Hanno Becker4a810fb2017-05-24 16:27:30 +01005070 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5071 ssl->in_msg, ssl->in_msglen );
5072 }
5073 else
5074 {
5075 ssl->in_msglen = 0;
5076 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005077
Hanno Becker4a810fb2017-05-24 16:27:30 +01005078 ssl->in_hslen = 0;
5079 }
5080 /* Case (4): Application data */
5081 else if( ssl->in_offt != NULL )
5082 {
5083 return( 0 );
5084 }
5085 /* Everything else (CCS & Alerts) */
5086 else
5087 {
5088 ssl->in_msglen = 0;
5089 }
5090
Hanno Becker1097b342018-08-15 14:09:41 +01005091 return( 0 );
5092}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005093
Hanno Beckere74d5562018-08-15 14:26:08 +01005094static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5095{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005096 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005097 return( 1 );
5098
5099 return( 0 );
5100}
5101
Hanno Becker5f066e72018-08-16 14:56:31 +01005102#if defined(MBEDTLS_SSL_PROTO_DTLS)
5103
5104static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5105{
5106 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5107 if( hs == NULL )
5108 return;
5109
Hanno Becker01315ea2018-08-21 17:22:17 +01005110 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005111 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005112 hs->buffering.total_bytes_buffered -=
5113 hs->buffering.future_record.len;
5114
5115 mbedtls_free( hs->buffering.future_record.data );
5116 hs->buffering.future_record.data = NULL;
5117 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005118}
5119
5120static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5121{
5122 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5123 unsigned char * rec;
5124 size_t rec_len;
5125 unsigned rec_epoch;
5126
5127 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5128 return( 0 );
5129
5130 if( hs == NULL )
5131 return( 0 );
5132
Hanno Becker5f066e72018-08-16 14:56:31 +01005133 rec = hs->buffering.future_record.data;
5134 rec_len = hs->buffering.future_record.len;
5135 rec_epoch = hs->buffering.future_record.epoch;
5136
5137 if( rec == NULL )
5138 return( 0 );
5139
Hanno Becker4cb782d2018-08-20 11:19:05 +01005140 /* Only consider loading future records if the
5141 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005142 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005143 return( 0 );
5144
Hanno Becker5f066e72018-08-16 14:56:31 +01005145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5146
5147 if( rec_epoch != ssl->in_epoch )
5148 {
5149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5150 goto exit;
5151 }
5152
5153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5154
5155 /* Double-check that the record is not too large */
5156 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5157 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5158 {
5159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5161 }
5162
5163 memcpy( ssl->in_hdr, rec, rec_len );
5164 ssl->in_left = rec_len;
5165 ssl->next_record_offset = 0;
5166
5167 ssl_free_buffered_record( ssl );
5168
5169exit:
5170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5171 return( 0 );
5172}
5173
5174static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5175{
5176 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5177 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005178 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005179
5180 /* Don't buffer future records outside handshakes. */
5181 if( hs == NULL )
5182 return( 0 );
5183
5184 /* Only buffer handshake records (we are only interested
5185 * in Finished messages). */
5186 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5187 return( 0 );
5188
5189 /* Don't buffer more than one future epoch record. */
5190 if( hs->buffering.future_record.data != NULL )
5191 return( 0 );
5192
Hanno Becker01315ea2018-08-21 17:22:17 +01005193 /* Don't buffer record if there's not enough buffering space remaining. */
5194 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5195 hs->buffering.total_bytes_buffered ) )
5196 {
5197 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",
5198 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5199 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005200 return( 0 );
5201 }
5202
Hanno Becker5f066e72018-08-16 14:56:31 +01005203 /* Buffer record */
5204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5205 ssl->in_epoch + 1 ) );
5206 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5207 rec_hdr_len + ssl->in_msglen );
5208
5209 /* ssl_parse_record_header() only considers records
5210 * of the next epoch as candidates for buffering. */
5211 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005212 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005213
5214 hs->buffering.future_record.data =
5215 mbedtls_calloc( 1, hs->buffering.future_record.len );
5216 if( hs->buffering.future_record.data == NULL )
5217 {
5218 /* If we run out of RAM trying to buffer a
5219 * record from the next epoch, just ignore. */
5220 return( 0 );
5221 }
5222
Hanno Becker01315ea2018-08-21 17:22:17 +01005223 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005224
Hanno Becker01315ea2018-08-21 17:22:17 +01005225 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005226 return( 0 );
5227}
5228
5229#endif /* MBEDTLS_SSL_PROTO_DTLS */
5230
Hanno Beckere74d5562018-08-15 14:26:08 +01005231static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005232{
5233 int ret;
5234
Hanno Becker5f066e72018-08-16 14:56:31 +01005235#if defined(MBEDTLS_SSL_PROTO_DTLS)
5236 /* We might have buffered a future record; if so,
5237 * and if the epoch matches now, load it.
5238 * On success, this call will set ssl->in_left to
5239 * the length of the buffered record, so that
5240 * the calls to ssl_fetch_input() below will
5241 * essentially be no-ops. */
5242 ret = ssl_load_buffered_record( ssl );
5243 if( ret != 0 )
5244 return( ret );
5245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005250 return( ret );
5251 }
5252
5253 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5257 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005258 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005259 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5260 {
5261 ret = ssl_buffer_future_record( ssl );
5262 if( ret != 0 )
5263 return( ret );
5264
5265 /* Fall through to handling of unexpected records */
5266 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5267 }
5268
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005269 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5270 {
5271 /* Skip unexpected record (but not whole datagram) */
5272 ssl->next_record_offset = ssl->in_msglen
5273 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005274
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5276 "(header)" ) );
5277 }
5278 else
5279 {
5280 /* Skip invalid record and the rest of the datagram */
5281 ssl->next_record_offset = 0;
5282 ssl->in_left = 0;
5283
5284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5285 "(header)" ) );
5286 }
5287
5288 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005289 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005290 }
5291#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005292 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005293 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294
5295 /*
5296 * Read and optionally decrypt the message contents
5297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5299 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005302 return( ret );
5303 }
5304
5305 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005310 if( ssl->next_record_offset < ssl->in_left )
5311 {
5312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5313 }
5314 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005315 else
5316#endif
5317 ssl->in_left = 0;
5318
5319 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005323 {
5324 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5326 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005327 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005328 /* Except when waiting for Finished as a bad mac here
5329 * probably means something went wrong in the handshake
5330 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5331 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5332 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5333 {
5334#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5335 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5336 {
5337 mbedtls_ssl_send_alert_message( ssl,
5338 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5339 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5340 }
5341#endif
5342 return( ret );
5343 }
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005346 if( ssl->conf->badmac_limit != 0 &&
5347 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5350 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005351 }
5352#endif
5353
Hanno Becker4a810fb2017-05-24 16:27:30 +01005354 /* As above, invalid records cause
5355 * dismissal of the whole datagram. */
5356
5357 ssl->next_record_offset = 0;
5358 ssl->in_left = 0;
5359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005361 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005362 }
5363
5364 return( ret );
5365 }
5366 else
5367#endif
5368 {
5369 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5371 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 mbedtls_ssl_send_alert_message( ssl,
5374 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5375 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005376 }
5377#endif
5378 return( ret );
5379 }
5380 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005381
Simon Butcher99000142016-10-13 17:21:01 +01005382 return( 0 );
5383}
5384
5385int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5386{
5387 int ret;
5388
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005389 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005390 * Handle particular types of records
5391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 {
Simon Butcher99000142016-10-13 17:21:01 +01005394 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5395 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005396 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 }
5399
Hanno Beckere678eaa2018-08-21 14:57:46 +01005400 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005401 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005402 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005403 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5405 ssl->in_msglen ) );
5406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005407 }
5408
Hanno Beckere678eaa2018-08-21 14:57:46 +01005409 if( ssl->in_msg[0] != 1 )
5410 {
5411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5412 ssl->in_msg[0] ) );
5413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5414 }
5415
5416#if defined(MBEDTLS_SSL_PROTO_DTLS)
5417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5418 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5419 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5420 {
5421 if( ssl->handshake == NULL )
5422 {
5423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5424 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5425 }
5426
5427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5428 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5429 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005430#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005431 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005434 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005435 if( ssl->in_msglen != 2 )
5436 {
5437 /* Note: Standard allows for more than one 2 byte alert
5438 to be packed in a single message, but Mbed TLS doesn't
5439 currently support this. */
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5441 ssl->in_msglen ) );
5442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5443 }
5444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 ssl->in_msg[0], ssl->in_msg[1] ) );
5447
5448 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005449 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005454 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 }
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5459 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5462 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005464
5465#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5466 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5467 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5468 {
Hanno Becker90333da2017-10-10 11:27:13 +01005469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005470 /* Will be handled when trying to parse ServerHello */
5471 return( 0 );
5472 }
5473#endif
5474
5475#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5476 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5477 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5478 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5479 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5480 {
5481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5482 /* Will be handled in mbedtls_ssl_parse_certificate() */
5483 return( 0 );
5484 }
5485#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5486
5487 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005488 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005489 }
5490
Hanno Beckerc76c6192017-06-06 10:03:17 +01005491#if defined(MBEDTLS_SSL_PROTO_DTLS)
5492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5493 ssl->handshake != NULL &&
5494 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5495 {
5496 ssl_handshake_wrapup_free_hs_transform( ssl );
5497 }
5498#endif
5499
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 return( 0 );
5501}
5502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005504{
5505 int ret;
5506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5509 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005510 {
5511 return( ret );
5512 }
5513
5514 return( 0 );
5515}
5516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005518 unsigned char level,
5519 unsigned char message )
5520{
5521 int ret;
5522
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005523 if( ssl == NULL || ssl->conf == NULL )
5524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005530 ssl->out_msglen = 2;
5531 ssl->out_msg[0] = level;
5532 ssl->out_msg[1] = message;
5533
Hanno Becker67bc7c32018-08-06 11:33:50 +01005534 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005537 return( ret );
5538 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005540
5541 return( 0 );
5542}
5543
Paul Bakker5121ce52009-01-03 21:22:43 +00005544/*
5545 * Handshake functions
5546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5548 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5549 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5550 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5551 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5552 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5553 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005554/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5562 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005563 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5564 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005567 ssl->state++;
5568 return( 0 );
5569 }
5570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005573}
5574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5582 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005583 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5584 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005587 ssl->state++;
5588 return( 0 );
5589 }
5590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005593}
Gilles Peskinef9828522017-05-03 12:28:43 +02005594
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005595#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005596/* Some certificate support -> implement write and parse */
5597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005601 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 const mbedtls_x509_crt *crt;
5603 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5608 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005609 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5610 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005613 ssl->state++;
5614 return( 0 );
5615 }
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005618 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 {
5620 if( ssl->client_auth == 0 )
5621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 ssl->state++;
5624 return( 0 );
5625 }
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 /*
5629 * If using SSLv3 and got no cert, send an Alert message
5630 * (otherwise an empty Certificate message will be sent).
5631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5633 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 {
5635 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5637 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5638 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 goto write_msg;
5642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645#endif /* MBEDTLS_SSL_CLI_C */
5646#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5652 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005653 }
5654 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005658
5659 /*
5660 * 0 . 0 handshake type
5661 * 1 . 3 handshake length
5662 * 4 . 6 length of all certs
5663 * 7 . 9 length of cert. 1
5664 * 10 . n-1 peer certificate
5665 * n . n+2 length of cert. 2
5666 * n+3 . ... upper level cert, etc.
5667 */
5668 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005670
Paul Bakker29087132010-03-21 21:03:34 +00005671 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 {
5673 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005674 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005677 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 }
5680
5681 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5682 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5683 ssl->out_msg[i + 2] = (unsigned char)( n );
5684
5685 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5686 i += n; crt = crt->next;
5687 }
5688
5689 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5690 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5691 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5692
5693 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5695 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005696
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005697#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005698write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005699#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005700
5701 ssl->state++;
5702
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005703 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005706 return( ret );
5707 }
5708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005710
Paul Bakkered27a042013-04-18 22:46:23 +02005711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005712}
5713
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005714/*
5715 * Once the certificate message is read, parse it into a cert chain and
5716 * perform basic checks, but leave actual verification to the caller
5717 */
5718static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005719{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005720 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005721 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005722 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724#if defined(MBEDTLS_SSL_SRV_C)
5725#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 /*
5727 * Check if the client sent an empty certificate
5728 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005729 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005731 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005732 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5734 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5735 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005739 /* The client was asked for a certificate but didn't send
5740 one. The client should know what's going on, so we
5741 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005742 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005743 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 }
5745 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5749 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5754 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5755 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5756 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005759
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005760 /* The client was asked for a certificate but didn't send
5761 one. The client should know what's going on, so we
5762 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005763 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005764 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 }
5766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5768 MBEDTLS_SSL_PROTO_TLS1_2 */
5769#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5775 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005777 }
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5780 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005783 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5784 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 }
5787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005789
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005792 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005793 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005794
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005795 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005799 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 }
5803
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005804 /* In case we tried to reuse a session but it failed */
5805 if( ssl->session_negotiate->peer_cert != NULL )
5806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5808 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005809 }
5810
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005811 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005813 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5817 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 }
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005822
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005823 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005824
5825 while( i < ssl->in_hslen )
5826 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005827 if ( i + 3 > ssl->in_hslen ) {
5828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5829 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5830 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5831 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 if( ssl->in_msg[i] != 0 )
5834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5837 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 }
5840
5841 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5842 | (unsigned int) ssl->in_msg[i + 2];
5843 i += 3;
5844
5845 if( n < 128 || i + n > ssl->in_hslen )
5846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 }
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005854 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005855 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005857 case 0: /*ok*/
5858 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5859 /* Ignore certificate with an unknown algorithm: maybe a
5860 prior certificate was already trusted. */
5861 break;
5862
5863 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5864 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5865 goto crt_parse_der_failed;
5866
5867 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5868 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5869 goto crt_parse_der_failed;
5870
5871 default:
5872 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5873 crt_parse_der_failed:
5874 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 return( ret );
5877 }
5878
5879 i += n;
5880 }
5881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005884 /*
5885 * On client, make sure the server cert doesn't change during renego to
5886 * avoid "triple handshake" attack: https://secure-resumption.com/
5887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005889 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005891 {
5892 if( ssl->session->peer_cert == NULL )
5893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5896 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005898 }
5899
5900 if( ssl->session->peer_cert->raw.len !=
5901 ssl->session_negotiate->peer_cert->raw.len ||
5902 memcmp( ssl->session->peer_cert->raw.p,
5903 ssl->session_negotiate->peer_cert->raw.p,
5904 ssl->session->peer_cert->raw.len ) != 0 )
5905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005907 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5908 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005910 }
5911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005913
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005914 return( 0 );
5915}
5916
5917int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5918{
5919 int ret;
5920 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5921 ssl->transform_negotiate->ciphersuite_info;
5922#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5923 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5924 ? ssl->handshake->sni_authmode
5925 : ssl->conf->authmode;
5926#else
5927 const int authmode = ssl->conf->authmode;
5928#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005929 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005930
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5932
5933 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5934 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5935 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5936 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5937 {
5938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5939 ssl->state++;
5940 return( 0 );
5941 }
5942
5943#if defined(MBEDTLS_SSL_SRV_C)
5944 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5945 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5946 {
5947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5948 ssl->state++;
5949 return( 0 );
5950 }
5951
5952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5953 authmode == MBEDTLS_SSL_VERIFY_NONE )
5954 {
5955 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005957
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005958 ssl->state++;
5959 return( 0 );
5960 }
5961#endif
5962
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005963#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5964 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005965 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005966 {
5967 goto crt_verify;
5968 }
5969#endif
5970
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005971 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005972 {
5973 /* mbedtls_ssl_read_record may have sent an alert already. We
5974 let it decide whether to alert. */
5975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5976 return( ret );
5977 }
5978
5979 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5980 {
5981#if defined(MBEDTLS_SSL_SRV_C)
5982 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5983 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5984 {
5985 ret = 0;
5986 }
5987#endif
5988
5989 ssl->state++;
5990 return( ret );
5991 }
5992
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005993#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5994 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005995 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005996
5997crt_verify:
5998 if( ssl->handshake->ecrs_enabled)
5999 rs_ctx = &ssl->handshake->ecrs_ctx;
6000#endif
6001
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006002 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006004 mbedtls_x509_crt *ca_chain;
6005 mbedtls_x509_crl *ca_crl;
6006
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006007#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006008 if( ssl->handshake->sni_ca_chain != NULL )
6009 {
6010 ca_chain = ssl->handshake->sni_ca_chain;
6011 ca_crl = ssl->handshake->sni_ca_crl;
6012 }
6013 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006014#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006015 {
6016 ca_chain = ssl->conf->ca_chain;
6017 ca_crl = ssl->conf->ca_crl;
6018 }
6019
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006020 /*
6021 * Main check: verify certificate
6022 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006023 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006024 ssl->session_negotiate->peer_cert,
6025 ca_chain, ca_crl,
6026 ssl->conf->cert_profile,
6027 ssl->hostname,
6028 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006029 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
6031 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006034 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006035
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006036#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6037 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006038 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006039#endif
6040
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006041 /*
6042 * Secondary checks: always done, but change 'ret' only if it was 0
6043 */
6044
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006045#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006048
6049 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006051 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006052 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006053 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006056 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006058 }
6059 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006060#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006063 ciphersuite_info,
6064 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006065 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006068 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006070 }
6071
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006072 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6073 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6074 * with details encoded in the verification flags. All other kinds
6075 * of error codes, including those from the user provided f_vrfy
6076 * functions, are treated as fatal and lead to a failure of
6077 * ssl_parse_certificate even if verification was optional. */
6078 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6079 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6080 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6081 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006082 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006083 }
6084
6085 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6086 {
6087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6088 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6089 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006090
6091 if( ret != 0 )
6092 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006093 uint8_t alert;
6094
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006095 /* The certificate may have been rejected for several reasons.
6096 Pick one and send the corresponding alert. Which alert to send
6097 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006098 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6099 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6100 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6101 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6102 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6103 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6104 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6105 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6106 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6107 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6108 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6109 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6110 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6111 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6112 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6113 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6114 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6115 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6116 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6117 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006118 else
6119 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6121 alert );
6122 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006123
Hanno Beckere6706e62017-05-15 16:05:15 +01006124#if defined(MBEDTLS_DEBUG_C)
6125 if( ssl->session_negotiate->verify_result != 0 )
6126 {
6127 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6128 ssl->session_negotiate->verify_result ) );
6129 }
6130 else
6131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6133 }
6134#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 }
6136
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006137 ssl->state++;
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140
6141 return( ret );
6142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6144 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6145 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6146 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6147 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6148 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6149 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006152{
6153 int ret;
6154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006158 ssl->out_msglen = 1;
6159 ssl->out_msg[0] = 1;
6160
Paul Bakker5121ce52009-01-03 21:22:43 +00006161 ssl->state++;
6162
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006163 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 return( ret );
6167 }
6168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170
6171 return( 0 );
6172}
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006175{
6176 int ret;
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006179
Hanno Becker327c93b2018-08-15 13:56:18 +01006180 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 return( ret );
6184 }
6185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006189 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6190 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 }
6193
Hanno Beckere678eaa2018-08-21 14:57:46 +01006194 /* CCS records are only accepted if they have length 1 and content '1',
6195 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006196
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006197 /*
6198 * Switch to our negotiated transform and session parameters for inbound
6199 * data.
6200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006202 ssl->transform_in = ssl->transform_negotiate;
6203 ssl->session_in = ssl->session_negotiate;
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006209 ssl_dtls_replay_reset( ssl );
6210#endif
6211
6212 /* Increment epoch */
6213 if( ++ssl->in_epoch == 0 )
6214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006216 /* This is highly unlikely to happen for legitimate reasons, so
6217 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006219 }
6220 }
6221 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006223 memset( ssl->in_ctr, 0, 8 );
6224
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006225 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6228 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006233 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6234 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006236 }
6237 }
6238#endif
6239
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 ssl->state++;
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243
6244 return( 0 );
6245}
6246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6248 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006249{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006250 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6253 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6254 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006255 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006256 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6259#if defined(MBEDTLS_SHA512_C)
6260 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006261 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6262 else
6263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if defined(MBEDTLS_SHA256_C)
6265 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006266 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006267 else
6268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006272 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006273 }
Paul Bakker380da532012-04-18 16:10:25 +00006274}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6279 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006280 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6281 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6284#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006285 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006288 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006291}
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006294 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6297 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006298 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6299 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6302#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006303 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006306 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006309}
6310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6312 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6313static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006314 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006315{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006316 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6317 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006318}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006319#endif
Paul Bakker380da532012-04-18 16:10:25 +00006320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6322#if defined(MBEDTLS_SHA256_C)
6323static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006324 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006325{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006326 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006327}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006328#endif
Paul Bakker380da532012-04-18 16:10:25 +00006329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#if defined(MBEDTLS_SHA512_C)
6331static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006332 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006333{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006334 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006335}
Paul Bakker769075d2012-11-24 11:26:46 +01006336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006340static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006342{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006343 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006344 mbedtls_md5_context md5;
6345 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006346
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 unsigned char padbuf[48];
6348 unsigned char md5sum[16];
6349 unsigned char sha1sum[20];
6350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006352 if( !session )
6353 session = ssl->session;
6354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006356
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006357 mbedtls_md5_init( &md5 );
6358 mbedtls_sha1_init( &sha1 );
6359
6360 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6361 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006362
6363 /*
6364 * SSLv3:
6365 * hash =
6366 * MD5( master + pad2 +
6367 * MD5( handshake + sender + master + pad1 ) )
6368 * + SHA1( master + pad2 +
6369 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006370 */
6371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006373 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6374 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006378 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6379 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006383 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006384
Paul Bakker1ef83d62012-04-11 12:09:53 +00006385 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006387 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6388 mbedtls_md5_update_ret( &md5, session->master, 48 );
6389 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6390 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006391
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006392 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6393 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6394 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6395 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006396
Paul Bakker1ef83d62012-04-11 12:09:53 +00006397 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006399 mbedtls_md5_starts_ret( &md5 );
6400 mbedtls_md5_update_ret( &md5, session->master, 48 );
6401 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6402 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6403 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006405 mbedtls_sha1_starts_ret( &sha1 );
6406 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6407 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6408 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6409 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006413 mbedtls_md5_free( &md5 );
6414 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006416 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6417 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6418 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006425static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006428 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006429 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006430 mbedtls_md5_context md5;
6431 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006432 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006435 if( !session )
6436 session = ssl->session;
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006439
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006440 mbedtls_md5_init( &md5 );
6441 mbedtls_sha1_init( &sha1 );
6442
6443 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6444 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006445
Paul Bakker1ef83d62012-04-11 12:09:53 +00006446 /*
6447 * TLSv1:
6448 * hash = PRF( master, finished_label,
6449 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6450 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006453 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6454 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006455#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006458 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6459 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006460#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006463 ? "client finished"
6464 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006466 mbedtls_md5_finish_ret( &md5, padbuf );
6467 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006468
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006469 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006470 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006474 mbedtls_md5_free( &md5 );
6475 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006476
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006477 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6484#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006485static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006487{
6488 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006489 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006490 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006491 unsigned char padbuf[32];
6492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006494 if( !session )
6495 session = ssl->session;
6496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006497 mbedtls_sha256_init( &sha256 );
6498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006500
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006501 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006502
6503 /*
6504 * TLSv1.2:
6505 * hash = PRF( master, finished_label,
6506 * Hash( handshake ) )[0.11]
6507 */
6508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509#if !defined(MBEDTLS_SHA256_ALT)
6510 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006511 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006512#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006515 ? "client finished"
6516 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006518 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006519
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006520 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006521 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006525 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006527 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533#if defined(MBEDTLS_SHA512_C)
Rodrigo Dias Correa671600c2020-11-25 00:42:28 -03006534
Paul Bakkerca4ab492012-04-18 14:23:57 +00006535static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006537{
6538 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006539 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006540 mbedtls_sha512_context sha512;
Rodrigo Dias Correa671600c2020-11-25 00:42:28 -03006541 unsigned char padbuf[48];
Paul Bakkerca4ab492012-04-18 14:23:57 +00006542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006544 if( !session )
6545 session = ssl->session;
6546
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006547 mbedtls_sha512_init( &sha512 );
6548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006550
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006551 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006552
6553 /*
6554 * TLSv1.2:
6555 * hash = PRF( master, finished_label,
6556 * Hash( handshake ) )[0.11]
6557 */
6558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006560 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6561 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006562#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006565 ? "client finished"
6566 : "server finished";
Shawn Carey69c5f5d2021-05-13 09:16:16 -04006567 /* mbedtls_sha512_finish_ret's output parameter is declared as a
6568 * 64-byte buffer, but sice we're using SHA-384, we know that the
6569 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
6570 * about it.
6571 */
6572#if defined(__GNUC__) && __GNUC__ >= 11
6573#pragma GCC diagnostic push
6574#pragma GCC diagnostic ignored "-Wstringop-overflow"
6575#endif
6576 mbedtls_sha512_finish_ret( &sha512, padbuf );
6577#if defined(__GNUC__) && __GNUC__ >= 11
6578#pragma GCC diagnostic pop
6579#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006580
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006581 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006582 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006585
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006586 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006587
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006588 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592#endif /* MBEDTLS_SHA512_C */
6593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006598
6599 /*
6600 * Free our handshake params
6601 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006602 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006604 ssl->handshake = NULL;
6605
6606 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006607 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006608 */
6609 if( ssl->transform )
6610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 mbedtls_ssl_transform_free( ssl->transform );
6612 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006613 }
6614 ssl->transform = ssl->transform_negotiate;
6615 ssl->transform_negotiate = NULL;
6616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006618}
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006621{
6622 int resume = ssl->handshake->resume;
6623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626#if defined(MBEDTLS_SSL_RENEGOTIATION)
6627 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006630 ssl->renego_records_seen = 0;
6631 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006632#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006633
6634 /*
6635 * Free the previous session and switch in the current one
6636 */
Paul Bakker0a597072012-09-25 21:55:46 +00006637 if( ssl->session )
6638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006640 /* RFC 7366 3.1: keep the EtM state */
6641 ssl->session_negotiate->encrypt_then_mac =
6642 ssl->session->encrypt_then_mac;
6643#endif
6644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 mbedtls_ssl_session_free( ssl->session );
6646 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006647 }
6648 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006649 ssl->session_negotiate = NULL;
6650
Paul Bakker0a597072012-09-25 21:55:46 +00006651 /*
6652 * Add cache entry
6653 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006654 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006655 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006656 resume == 0 )
6657 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006658 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006660 }
Paul Bakker0a597072012-09-25 21:55:46 +00006661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006664 ssl->handshake->flight != NULL )
6665 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006666 /* Cancel handshake timer */
6667 ssl_set_timer( ssl, 0 );
6668
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006669 /* Keep last flight around in case we need to resend it:
6670 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006672 }
6673 else
6674#endif
6675 ssl_handshake_wrapup_free_hs_transform( ssl );
6676
Paul Bakker48916f92012-09-16 19:57:18 +00006677 ssl->state++;
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006680}
6681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006683{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006684 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006687
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006688 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006689
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006690 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006691
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006692 /*
6693 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6694 * may define some other value. Currently (early 2016), no defined
6695 * ciphersuite does this (and this is unlikely to change as activity has
6696 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006701 ssl->verify_data_len = hash_len;
6702 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006703#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006704
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6707 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006708
6709 /*
6710 * In case of session resuming, invert the client and server
6711 * ChangeCipherSpec messages order.
6712 */
Paul Bakker0a597072012-09-25 21:55:46 +00006713 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006723 }
6724 else
6725 ssl->state++;
6726
Paul Bakker48916f92012-09-16 19:57:18 +00006727 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006728 * Switch to our negotiated transform and session parameters for outbound
6729 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006735 {
6736 unsigned char i;
6737
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006738 /* Remember current epoch settings for resending */
6739 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006740 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006741
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006742 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006743 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006744
6745 /* Increment epoch */
6746 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006747 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006748 break;
6749
6750 /* The loop goes to its end iff the counter is wrapping */
6751 if( i == 0 )
6752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6754 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006755 }
6756 }
6757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006759 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006761 ssl->transform_out = ssl->transform_negotiate;
6762 ssl->session_out = ssl->session_negotiate;
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6765 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6770 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006771 }
6772 }
6773#endif
6774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006778#endif
6779
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006780 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 return( ret );
6784 }
6785
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006786#if defined(MBEDTLS_SSL_PROTO_DTLS)
6787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6788 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6789 {
6790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6791 return( ret );
6792 }
6793#endif
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796
6797 return( 0 );
6798}
6799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006801#define SSL_MAX_HASH_LEN 36
6802#else
6803#define SSL_MAX_HASH_LEN 12
6804#endif
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807{
Paul Bakker23986e52011-04-24 08:57:21 +00006808 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006809 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006810 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
Gilles Peskine7cab4992021-12-13 14:38:40 +01006814 /* There is currently no ciphersuite using another length with TLS 1.2 */
6815#if defined(MBEDTLS_SSL_PROTO_SSL3)
6816 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6817 hash_len = 36;
6818 else
6819#endif
6820 hash_len = 12;
6821
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006822 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Hanno Becker327c93b2018-08-15 13:56:18 +01006824 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006827 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 }
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6834 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006835 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
6836 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006837 }
6838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6840 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6844 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006845 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
6846 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 }
6848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006850 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Dave Rodgman459a4612021-06-28 21:58:56 +01006854 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006855 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
6856 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006857 }
6858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006860 ssl->verify_data_len = hash_len;
6861 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006862#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006863
Paul Bakker0a597072012-09-25 21:55:46 +00006864 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006874 }
6875 else
6876 ssl->state++;
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006881#endif
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006884
Gilles Peskinecd9e7512021-12-13 12:36:15 +01006885exit:
6886 mbedtls_platform_zeroize( buf, hash_len );
6887 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006888}
6889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6895 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6896 mbedtls_md5_init( &handshake->fin_md5 );
6897 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006898 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6899 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6902#if defined(MBEDTLS_SHA256_C)
6903 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006904 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#if defined(MBEDTLS_SHA512_C)
6907 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006908 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006911
6912 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006913
6914#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6915 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6916 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6917#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919#if defined(MBEDTLS_DHM_C)
6920 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#if defined(MBEDTLS_ECDH_C)
6923 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006924#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006925#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006926 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006927#if defined(MBEDTLS_SSL_CLI_C)
6928 handshake->ecjpake_cache = NULL;
6929 handshake->ecjpake_cache_len = 0;
6930#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006931#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006932
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006933#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006934 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006935#endif
6936
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006937#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6938 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6939#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006940}
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6947 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 mbedtls_md_init( &transform->md_ctx_enc );
6950 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006951}
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006956}
6957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006959{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006960 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006961 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006963 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006965 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006966 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006967
6968 /*
6969 * Either the pointers are now NULL or cleared properly and can be freed.
6970 * Now allocate missing structures.
6971 */
6972 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006973 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006974 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006975 }
Paul Bakker48916f92012-09-16 19:57:18 +00006976
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006977 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006978 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006979 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006980 }
Paul Bakker48916f92012-09-16 19:57:18 +00006981
Paul Bakker82788fb2014-10-20 13:59:19 +02006982 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006983 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006984 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006985 }
Paul Bakker48916f92012-09-16 19:57:18 +00006986
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006987 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006988 if( ssl->handshake == NULL ||
6989 ssl->transform_negotiate == NULL ||
6990 ssl->session_negotiate == NULL )
6991 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 mbedtls_free( ssl->handshake );
6995 mbedtls_free( ssl->transform_negotiate );
6996 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006997
6998 ssl->handshake = NULL;
6999 ssl->transform_negotiate = NULL;
7000 ssl->session_negotiate = NULL;
7001
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007002 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007003 }
7004
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007005 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007007 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007008 ssl_handshake_params_init( ssl->handshake );
7009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007011 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7012 {
7013 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007014
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007015 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7016 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7017 else
7018 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007019
7020 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007021 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007022#endif
7023
Paul Bakker48916f92012-09-16 19:57:18 +00007024 return( 0 );
7025}
7026
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007027#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007028/* Dummy cookie callbacks for defaults */
7029static int ssl_cookie_write_dummy( void *ctx,
7030 unsigned char **p, unsigned char *end,
7031 const unsigned char *cli_id, size_t cli_id_len )
7032{
7033 ((void) ctx);
7034 ((void) p);
7035 ((void) end);
7036 ((void) cli_id);
7037 ((void) cli_id_len);
7038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007040}
7041
7042static int ssl_cookie_check_dummy( void *ctx,
7043 const unsigned char *cookie, size_t cookie_len,
7044 const unsigned char *cli_id, size_t cli_id_len )
7045{
7046 ((void) ctx);
7047 ((void) cookie);
7048 ((void) cookie_len);
7049 ((void) cli_id);
7050 ((void) cli_id_len);
7051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007053}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007054#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007055
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007056/* Once ssl->out_hdr as the address of the beginning of the
7057 * next outgoing record is set, deduce the other pointers.
7058 *
7059 * Note: For TLS, we save the implicit record sequence number
7060 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7061 * and the caller has to make sure there's space for this.
7062 */
7063
7064static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7065 mbedtls_ssl_transform *transform )
7066{
7067#if defined(MBEDTLS_SSL_PROTO_DTLS)
7068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7069 {
7070 ssl->out_ctr = ssl->out_hdr + 3;
7071 ssl->out_len = ssl->out_hdr + 11;
7072 ssl->out_iv = ssl->out_hdr + 13;
7073 }
7074 else
7075#endif
7076 {
7077 ssl->out_ctr = ssl->out_hdr - 8;
7078 ssl->out_len = ssl->out_hdr + 3;
7079 ssl->out_iv = ssl->out_hdr + 5;
7080 }
7081
7082 /* Adjust out_msg to make space for explicit IV, if used. */
7083 if( transform != NULL &&
7084 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7085 {
7086 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7087 }
7088 else
7089 ssl->out_msg = ssl->out_iv;
7090}
7091
7092/* Once ssl->in_hdr as the address of the beginning of the
7093 * next incoming record is set, deduce the other pointers.
7094 *
7095 * Note: For TLS, we save the implicit record sequence number
7096 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7097 * and the caller has to make sure there's space for this.
7098 */
7099
7100static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7101 mbedtls_ssl_transform *transform )
7102{
7103#if defined(MBEDTLS_SSL_PROTO_DTLS)
7104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7105 {
7106 ssl->in_ctr = ssl->in_hdr + 3;
7107 ssl->in_len = ssl->in_hdr + 11;
7108 ssl->in_iv = ssl->in_hdr + 13;
7109 }
7110 else
7111#endif
7112 {
7113 ssl->in_ctr = ssl->in_hdr - 8;
7114 ssl->in_len = ssl->in_hdr + 3;
7115 ssl->in_iv = ssl->in_hdr + 5;
7116 }
7117
7118 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7119 if( transform != NULL &&
7120 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7121 {
7122 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7123 }
7124 else
7125 ssl->in_msg = ssl->in_iv;
7126}
7127
Paul Bakker5121ce52009-01-03 21:22:43 +00007128/*
7129 * Initialize an SSL context
7130 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007131void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7132{
7133 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7134}
7135
7136/*
7137 * Setup an SSL context
7138 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007139
7140static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7141{
7142 /* Set the incoming and outgoing record pointers. */
7143#if defined(MBEDTLS_SSL_PROTO_DTLS)
7144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7145 {
7146 ssl->out_hdr = ssl->out_buf;
7147 ssl->in_hdr = ssl->in_buf;
7148 }
7149 else
7150#endif /* MBEDTLS_SSL_PROTO_DTLS */
7151 {
7152 ssl->out_hdr = ssl->out_buf + 8;
7153 ssl->in_hdr = ssl->in_buf + 8;
7154 }
7155
7156 /* Derive other internal pointers. */
7157 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7158 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7159}
7160
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007161int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007162 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007163{
Paul Bakker48916f92012-09-16 19:57:18 +00007164 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007165
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007166 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007167
7168 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007169 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007170 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007171
7172 /* Set to NULL in case of an error condition */
7173 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007174
Angus Grattond8213d02016-05-25 20:56:48 +10007175 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7176 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 {
Angus Grattond8213d02016-05-25 20:56:48 +10007178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007179 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007180 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007181 }
7182
7183 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7184 if( ssl->out_buf == NULL )
7185 {
7186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007187 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007188 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007189 }
7190
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007191 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007192
Paul Bakker48916f92012-09-16 19:57:18 +00007193 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007194 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007197
7198error:
7199 mbedtls_free( ssl->in_buf );
7200 mbedtls_free( ssl->out_buf );
7201
7202 ssl->conf = NULL;
7203
7204 ssl->in_buf = NULL;
7205 ssl->out_buf = NULL;
7206
7207 ssl->in_hdr = NULL;
7208 ssl->in_ctr = NULL;
7209 ssl->in_len = NULL;
7210 ssl->in_iv = NULL;
7211 ssl->in_msg = NULL;
7212
7213 ssl->out_hdr = NULL;
7214 ssl->out_ctr = NULL;
7215 ssl->out_len = NULL;
7216 ssl->out_iv = NULL;
7217 ssl->out_msg = NULL;
7218
k-stachowiak9f7798e2018-07-31 16:52:32 +02007219 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007220}
7221
7222/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007223 * Reset an initialized and used SSL context for re-use while retaining
7224 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007225 *
7226 * If partial is non-zero, keep data in the input buffer and client ID.
7227 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007228 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007229static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007230{
Paul Bakker48916f92012-09-16 19:57:18 +00007231 int ret;
7232
Hanno Becker7e772132018-08-10 12:38:21 +01007233#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7234 !defined(MBEDTLS_SSL_SRV_C)
7235 ((void) partial);
7236#endif
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007239
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007240 /* Cancel any possibly running timer */
7241 ssl_set_timer( ssl, 0 );
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_RENEGOTIATION)
7244 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007245 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007246
7247 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7249 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007252
Paul Bakker7eb013f2011-10-06 12:37:39 +00007253 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007254 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007255
7256 ssl->in_msgtype = 0;
7257 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007259 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007260 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007263 ssl_dtls_replay_reset( ssl );
7264#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007265
7266 ssl->in_hslen = 0;
7267 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007268
7269 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007270
7271 ssl->out_msgtype = 0;
7272 ssl->out_msglen = 0;
7273 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7275 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007276 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007277#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007278
Hanno Becker19859472018-08-06 09:40:20 +01007279 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7280
Paul Bakker48916f92012-09-16 19:57:18 +00007281 ssl->transform_in = NULL;
7282 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007283
Hanno Becker78640902018-08-13 16:35:15 +01007284 ssl->session_in = NULL;
7285 ssl->session_out = NULL;
7286
Angus Grattond8213d02016-05-25 20:56:48 +10007287 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007288
7289#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007290 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007291#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7292 {
7293 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007294 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007295 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7298 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7301 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7304 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007305 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007306 }
7307#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007308
Paul Bakker48916f92012-09-16 19:57:18 +00007309 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 mbedtls_ssl_transform_free( ssl->transform );
7312 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007313 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007314 }
Paul Bakker48916f92012-09-16 19:57:18 +00007315
Paul Bakkerc0463502013-02-14 11:19:38 +01007316 if( ssl->session )
7317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 mbedtls_ssl_session_free( ssl->session );
7319 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007320 ssl->session = NULL;
7321 }
7322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007324 ssl->alpn_chosen = NULL;
7325#endif
7326
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007327#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007328#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007329 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007330#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007331 {
7332 mbedtls_free( ssl->cli_id );
7333 ssl->cli_id = NULL;
7334 ssl->cli_id_len = 0;
7335 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007336#endif
7337
Paul Bakker48916f92012-09-16 19:57:18 +00007338 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7339 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007340
7341 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007342}
7343
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007344/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007345 * Reset an initialized and used SSL context for re-use while retaining
7346 * all application-set variables, function pointers and data.
7347 */
7348int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7349{
7350 return( ssl_session_reset_int( ssl, 0 ) );
7351}
7352
7353/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007354 * SSL set accessors
7355 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007356void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007357{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007358 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007359}
7360
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007361void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007362{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007363 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007364}
7365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007367void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007369 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007370}
7371#endif
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007374void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007376 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007377}
7378#endif
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007381
Hanno Becker1841b0a2018-08-24 11:13:57 +01007382void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7383 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007384{
7385 ssl->disable_datagram_packing = !allow_packing;
7386}
7387
7388void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7389 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007391 conf->hs_timeout_min = min;
7392 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007393}
7394#endif
7395
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007396void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007397{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007398 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007399}
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007402void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007403 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007404 void *p_vrfy )
7405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007406 conf->f_vrfy = f_vrfy;
7407 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007410
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007411void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007412 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007413 void *p_rng )
7414{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007415 conf->f_rng = f_rng;
7416 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007417}
7418
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007419void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007420 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007421 void *p_dbg )
7422{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007423 conf->f_dbg = f_dbg;
7424 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007425}
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007428 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007429 mbedtls_ssl_send_t *f_send,
7430 mbedtls_ssl_recv_t *f_recv,
7431 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007432{
7433 ssl->p_bio = p_bio;
7434 ssl->f_send = f_send;
7435 ssl->f_recv = f_recv;
7436 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007437}
7438
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007439#if defined(MBEDTLS_SSL_PROTO_DTLS)
7440void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7441{
7442 ssl->mtu = mtu;
7443}
7444#endif
7445
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007446void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007447{
7448 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007449}
7450
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007451void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7452 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007453 mbedtls_ssl_set_timer_t *f_set_timer,
7454 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007455{
7456 ssl->p_timer = p_timer;
7457 ssl->f_set_timer = f_set_timer;
7458 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007459
7460 /* Make sure we start with no timer running */
7461 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007462}
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007465void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007466 void *p_cache,
7467 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7468 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007470 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007471 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007472 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_CLI_C)
7477int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007478{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007479 int ret;
7480
7481 if( ssl == NULL ||
7482 session == NULL ||
7483 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007484 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007487 }
7488
7489 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7490 return( ret );
7491
Paul Bakker0a597072012-09-25 21:55:46 +00007492 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007493
7494 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007498void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007499 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007501 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7502 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7503 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7504 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007505}
7506
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007507void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007508 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007509 int major, int minor )
7510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007512 return;
7513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007515 return;
7516
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007517 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007518}
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007521void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007522 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007523{
7524 conf->cert_profile = profile;
7525}
7526
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007527/* Append a new keycert entry to a (possibly empty) list */
7528static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7529 mbedtls_x509_crt *cert,
7530 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007531{
niisato8ee24222018-06-25 19:05:48 +09007532 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007533
niisato8ee24222018-06-25 19:05:48 +09007534 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7535 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007536 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007537
niisato8ee24222018-06-25 19:05:48 +09007538 new_cert->cert = cert;
7539 new_cert->key = key;
7540 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007541
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007542 /* Update head is the list was null, else add to the end */
7543 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007544 {
niisato8ee24222018-06-25 19:05:48 +09007545 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007546 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007547 else
7548 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007549 mbedtls_ssl_key_cert *cur = *head;
7550 while( cur->next != NULL )
7551 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007552 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007553 }
7554
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007555 return( 0 );
7556}
7557
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007558int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007559 mbedtls_x509_crt *own_cert,
7560 mbedtls_pk_context *pk_key )
7561{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007562 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007563}
7564
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007565void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007566 mbedtls_x509_crt *ca_chain,
7567 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007568{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007569 conf->ca_chain = ca_chain;
7570 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007573
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007574#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7575int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7576 mbedtls_x509_crt *own_cert,
7577 mbedtls_pk_context *pk_key )
7578{
7579 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7580 own_cert, pk_key ) );
7581}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007582
7583void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7584 mbedtls_x509_crt *ca_chain,
7585 mbedtls_x509_crl *ca_crl )
7586{
7587 ssl->handshake->sni_ca_chain = ca_chain;
7588 ssl->handshake->sni_ca_crl = ca_crl;
7589}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007590
7591void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7592 int authmode )
7593{
7594 ssl->handshake->sni_authmode = authmode;
7595}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007596#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7597
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007598#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007599/*
7600 * Set EC J-PAKE password for current handshake
7601 */
7602int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7603 const unsigned char *pw,
7604 size_t pw_len )
7605{
7606 mbedtls_ecjpake_role role;
7607
Janos Follath8eb64132016-06-03 15:40:57 +01007608 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7610
7611 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7612 role = MBEDTLS_ECJPAKE_SERVER;
7613 else
7614 role = MBEDTLS_ECJPAKE_CLIENT;
7615
7616 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7617 role,
7618 MBEDTLS_MD_SHA256,
7619 MBEDTLS_ECP_DP_SECP256R1,
7620 pw, pw_len ) );
7621}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007622#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007625int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007626 const unsigned char *psk, size_t psk_len,
7627 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007628{
Paul Bakker6db455e2013-09-18 17:29:31 +02007629 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007634
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007635 /* Identity len will be encoded on two bytes */
7636 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007637 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007638 {
7639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7640 }
7641
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007642 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007643 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007644 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007645
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007646 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007647 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007648 conf->psk_len = 0;
7649 }
7650 if( conf->psk_identity != NULL )
7651 {
7652 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007653 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007654 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007655 }
7656
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007657 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7658 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007659 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007660 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007661 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007662 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007663 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007664 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007665 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007666
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007667 conf->psk_len = psk_len;
7668 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007669
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007670 memcpy( conf->psk, psk, conf->psk_len );
7671 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007672
7673 return( 0 );
7674}
7675
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007676int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7677 const unsigned char *psk, size_t psk_len )
7678{
7679 if( psk == NULL || ssl->handshake == NULL )
7680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7681
7682 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7684
7685 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007686 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007687 mbedtls_platform_zeroize( ssl->handshake->psk,
7688 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007689 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007690 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007691 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007692
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007693 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007694 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007695
7696 ssl->handshake->psk_len = psk_len;
7697 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7698
7699 return( 0 );
7700}
7701
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007702void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007704 size_t),
7705 void *p_psk )
7706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007707 conf->f_psk = f_psk;
7708 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007711
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007712#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007713
7714#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007715int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007716{
7717 int ret;
7718
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007719 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7720 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7721 {
7722 mbedtls_mpi_free( &conf->dhm_P );
7723 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007726
7727 return( 0 );
7728}
Hanno Becker470a8c42017-10-04 15:28:46 +01007729#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Hanno Beckera90658f2017-10-04 15:29:08 +01007731int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7732 const unsigned char *dhm_P, size_t P_len,
7733 const unsigned char *dhm_G, size_t G_len )
7734{
7735 int ret;
7736
7737 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7738 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7739 {
7740 mbedtls_mpi_free( &conf->dhm_P );
7741 mbedtls_mpi_free( &conf->dhm_G );
7742 return( ret );
7743 }
7744
7745 return( 0 );
7746}
Paul Bakker5121ce52009-01-03 21:22:43 +00007747
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007748int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007749{
7750 int ret;
7751
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007752 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7753 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7754 {
7755 mbedtls_mpi_free( &conf->dhm_P );
7756 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007757 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007758 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007759
7760 return( 0 );
7761}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007762#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007763
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007764#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7765/*
7766 * Set the minimum length for Diffie-Hellman parameters
7767 */
7768void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7769 unsigned int bitlen )
7770{
7771 conf->dhm_min_bitlen = bitlen;
7772}
7773#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7774
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007775#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007776/*
7777 * Set allowed/preferred hashes for handshake signatures
7778 */
7779void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7780 const int *hashes )
7781{
7782 conf->sig_hashes = hashes;
7783}
Hanno Becker947194e2017-04-07 13:25:49 +01007784#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007785
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007786#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007787/*
7788 * Set the allowed elliptic curves
7789 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007790void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007791 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007792{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007793 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007794}
Hanno Becker947194e2017-04-07 13:25:49 +01007795#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007796
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007797#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007799{
Hanno Becker947194e2017-04-07 13:25:49 +01007800 /* Initialize to suppress unnecessary compiler warning */
7801 size_t hostname_len = 0;
7802
7803 /* Check if new hostname is valid before
7804 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007805 if( hostname != NULL )
7806 {
7807 hostname_len = strlen( hostname );
7808
7809 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7811 }
7812
7813 /* Now it's clear that we will overwrite the old hostname,
7814 * so we can free it safely */
7815
7816 if( ssl->hostname != NULL )
7817 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007818 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007819 mbedtls_free( ssl->hostname );
7820 }
7821
7822 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007823
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007825 {
7826 ssl->hostname = NULL;
7827 }
7828 else
7829 {
7830 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007831 if( ssl->hostname == NULL )
7832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007833
Hanno Becker947194e2017-04-07 13:25:49 +01007834 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007835
Hanno Becker947194e2017-04-07 13:25:49 +01007836 ssl->hostname[hostname_len] = '\0';
7837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007838
7839 return( 0 );
7840}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007841#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007843#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007844void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007846 const unsigned char *, size_t),
7847 void *p_sni )
7848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007849 conf->f_sni = f_sni;
7850 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007855int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007856{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007857 size_t cur_len, tot_len;
7858 const char **p;
7859
7860 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007861 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7862 * MUST NOT be truncated."
7863 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007864 */
7865 tot_len = 0;
7866 for( p = protos; *p != NULL; p++ )
7867 {
7868 cur_len = strlen( *p );
7869 tot_len += cur_len;
7870
Ronald Cron157cffe2020-04-23 16:41:44 +02007871 if( ( cur_len == 0 ) ||
7872 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
7873 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007875 }
7876
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007877 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007878
7879 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007880}
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007883{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007884 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007887
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007888void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->max_major_ver = major;
7891 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007892}
7893
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007894void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007895{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007896 conf->min_major_ver = major;
7897 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007898}
7899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007901void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007902{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007903 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007904}
7905#endif
7906
Janos Follath088ce432017-04-10 12:42:31 +01007907#if defined(MBEDTLS_SSL_SRV_C)
7908void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7909 char cert_req_ca_list )
7910{
7911 conf->cert_req_ca_list = cert_req_ca_list;
7912}
7913#endif
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007916void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007917{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007918 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007919}
7920#endif
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007923void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007925 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007926}
7927#endif
7928
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007929#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007930void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007932 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007933}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007934#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007937int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007940 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007943 }
7944
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007945 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007946
7947 return( 0 );
7948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007952void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007953{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007954 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007959void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007960{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007961 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007962}
7963#endif
7964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007965void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007967 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007971void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007973 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007974}
7975
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007976void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007978 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007979}
7980
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007981void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007982 const unsigned char period[8] )
7983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007984 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007989#if defined(MBEDTLS_SSL_CLI_C)
7990void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007991{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007992 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007993}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007994#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007995
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007996#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007997void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7998 mbedtls_ssl_ticket_write_t *f_ticket_write,
7999 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8000 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008001{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008002 conf->f_ticket_write = f_ticket_write;
8003 conf->f_ticket_parse = f_ticket_parse;
8004 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008005}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008008
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008009#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8010void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8011 mbedtls_ssl_export_keys_t *f_export_keys,
8012 void *p_export_keys )
8013{
8014 conf->f_export_keys = f_export_keys;
8015 conf->p_export_keys = p_export_keys;
8016}
8017#endif
8018
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008019#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008020void mbedtls_ssl_conf_async_private_cb(
8021 mbedtls_ssl_config *conf,
8022 mbedtls_ssl_async_sign_t *f_async_sign,
8023 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8024 mbedtls_ssl_async_resume_t *f_async_resume,
8025 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008026 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008027{
8028 conf->f_async_sign_start = f_async_sign;
8029 conf->f_async_decrypt_start = f_async_decrypt;
8030 conf->f_async_resume = f_async_resume;
8031 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008032 conf->p_async_config_data = async_config_data;
8033}
8034
Gilles Peskine8f97af72018-04-26 11:46:10 +02008035void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8036{
8037 return( conf->p_async_config_data );
8038}
8039
Gilles Peskine1febfef2018-04-30 11:54:39 +02008040void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008041{
8042 if( ssl->handshake == NULL )
8043 return( NULL );
8044 else
8045 return( ssl->handshake->user_async_ctx );
8046}
8047
Gilles Peskine1febfef2018-04-30 11:54:39 +02008048void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008049 void *ctx )
8050{
8051 if( ssl->handshake != NULL )
8052 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008053}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008054#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008055
Paul Bakker5121ce52009-01-03 21:22:43 +00008056/*
8057 * SSL get accessors
8058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008060{
8061 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8062}
8063
Hanno Becker8b170a02017-10-10 11:51:19 +01008064int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8065{
8066 /*
8067 * Case A: We're currently holding back
8068 * a message for further processing.
8069 */
8070
8071 if( ssl->keep_current_message == 1 )
8072 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008074 return( 1 );
8075 }
8076
8077 /*
8078 * Case B: Further records are pending in the current datagram.
8079 */
8080
8081#if defined(MBEDTLS_SSL_PROTO_DTLS)
8082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8083 ssl->in_left > ssl->next_record_offset )
8084 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008086 return( 1 );
8087 }
8088#endif /* MBEDTLS_SSL_PROTO_DTLS */
8089
8090 /*
8091 * Case C: A handshake message is being processed.
8092 */
8093
Hanno Becker8b170a02017-10-10 11:51:19 +01008094 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8095 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008097 return( 1 );
8098 }
8099
8100 /*
8101 * Case D: An application data message is being processed
8102 */
8103 if( ssl->in_offt != NULL )
8104 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008106 return( 1 );
8107 }
8108
8109 /*
8110 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008111 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008112 * we implement support for multiple alerts in single records.
8113 */
8114
8115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8116 return( 0 );
8117}
8118
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008119uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008120{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008121 if( ssl->session != NULL )
8122 return( ssl->session->verify_result );
8123
8124 if( ssl->session_negotiate != NULL )
8125 return( ssl->session_negotiate->verify_result );
8126
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008127 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008128}
8129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008131{
Paul Bakker926c8e42013-03-06 10:23:34 +01008132 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008133 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008136}
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008142 {
8143 switch( ssl->minor_ver )
8144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008146 return( "DTLSv1.0" );
8147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008149 return( "DTLSv1.2" );
8150
8151 default:
8152 return( "unknown (DTLS)" );
8153 }
8154 }
8155#endif
8156
Paul Bakker43ca69c2011-01-15 17:35:19 +00008157 switch( ssl->minor_ver )
8158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008160 return( "SSLv3.0" );
8161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008163 return( "TLSv1.0" );
8164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008166 return( "TLSv1.1" );
8167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008169 return( "TLSv1.2" );
8170
Paul Bakker43ca69c2011-01-15 17:35:19 +00008171 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008172 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008173 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008174}
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008177{
Hanno Becker3136ede2018-08-17 15:28:19 +01008178 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008180 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008181
Hanno Becker78640902018-08-13 16:35:15 +01008182 if( transform == NULL )
8183 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_ZLIB_SUPPORT)
8186 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008188#endif
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 case MBEDTLS_MODE_GCM:
8193 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008194 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008196 transform_expansion = transform->minlen;
8197 break;
8198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008200
8201 block_size = mbedtls_cipher_get_block_size(
8202 &transform->cipher_ctx_enc );
8203
Hanno Becker3136ede2018-08-17 15:28:19 +01008204 /* Expansion due to the addition of the MAC. */
8205 transform_expansion += transform->maclen;
8206
8207 /* Expansion due to the addition of CBC padding;
8208 * Theoretically up to 256 bytes, but we never use
8209 * more than the block size of the underlying cipher. */
8210 transform_expansion += block_size;
8211
8212 /* For TLS 1.1 or higher, an explicit IV is added
8213 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008214#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8215 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008216 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008217#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008218
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008219 break;
8220
8221 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008224 }
8225
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008226 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008227}
8228
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008229#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8230size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8231{
8232 size_t max_len;
8233
8234 /*
8235 * Assume mfl_code is correct since it was checked when set
8236 */
Angus Grattond8213d02016-05-25 20:56:48 +10008237 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008238
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008239 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008240 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008241 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008242 {
Angus Grattond8213d02016-05-25 20:56:48 +10008243 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008244 }
8245
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008246 /* During a handshake, use the value being negotiated */
8247 if( ssl->session_negotiate != NULL &&
8248 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8249 {
8250 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8251 }
8252
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008253 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008254}
8255#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8256
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008257#if defined(MBEDTLS_SSL_PROTO_DTLS)
8258static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8259{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008260 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8262 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8263 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8264 return ( 0 );
8265
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008266 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8267 return( ssl->mtu );
8268
8269 if( ssl->mtu == 0 )
8270 return( ssl->handshake->mtu );
8271
8272 return( ssl->mtu < ssl->handshake->mtu ?
8273 ssl->mtu : ssl->handshake->mtu );
8274}
8275#endif /* MBEDTLS_SSL_PROTO_DTLS */
8276
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008277int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8278{
8279 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8280
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008281#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8282 !defined(MBEDTLS_SSL_PROTO_DTLS)
8283 (void) ssl;
8284#endif
8285
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008286#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8287 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8288
8289 if( max_len > mfl )
8290 max_len = mfl;
8291#endif
8292
8293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008294 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008295 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008296 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008297 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8298 const size_t overhead = (size_t) ret;
8299
8300 if( ret < 0 )
8301 return( ret );
8302
8303 if( mtu <= overhead )
8304 {
8305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8306 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8307 }
8308
8309 if( max_len > mtu - overhead )
8310 max_len = mtu - overhead;
8311 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008312#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008313
Hanno Becker0defedb2018-08-10 12:35:02 +01008314#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8315 !defined(MBEDTLS_SSL_PROTO_DTLS)
8316 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008317#endif
8318
8319 return( (int) max_len );
8320}
8321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_X509_CRT_PARSE_C)
8323const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008324{
8325 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008326 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008327
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008328 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#if defined(MBEDTLS_SSL_CLI_C)
8333int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008334{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008335 if( ssl == NULL ||
8336 dst == NULL ||
8337 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008338 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008341 }
8342
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008343 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008346
Paul Bakker5121ce52009-01-03 21:22:43 +00008347/*
Paul Bakker1961b702013-01-25 14:49:24 +01008348 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008353
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008354 if( ssl == NULL || ssl->conf == NULL )
8355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008364#endif
8365
Paul Bakker1961b702013-01-25 14:49:24 +01008366 return( ret );
8367}
8368
8369/*
8370 * Perform the SSL handshake
8371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008373{
8374 int ret = 0;
8375
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008376 if( ssl == NULL || ssl->conf == NULL )
8377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008384
8385 if( ret != 0 )
8386 break;
8387 }
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008390
8391 return( ret );
8392}
8393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394#if defined(MBEDTLS_SSL_RENEGOTIATION)
8395#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008396/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008397 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008400{
8401 int ret;
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008404
8405 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8407 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008408
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008409 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008410 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008412 return( ret );
8413 }
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008416
8417 return( 0 );
8418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008420
8421/*
8422 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 * - any side: calling mbedtls_ssl_renegotiate(),
8424 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8425 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008426 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008427 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008431{
8432 int ret;
8433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008435
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008436 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8437 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008438
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008439 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8440 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008444 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008445 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008446 ssl->handshake->out_msg_seq = 1;
8447 else
8448 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008449 }
8450#endif
8451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8453 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008458 return( ret );
8459 }
8460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008462
8463 return( 0 );
8464}
8465
8466/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008467 * Renegotiate current connection on client,
8468 * or request renegotiation on server
8469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008473
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008474 if( ssl == NULL || ssl->conf == NULL )
8475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008478 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008479 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008485
8486 /* Did we already try/start sending HelloRequest? */
8487 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008489
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008490 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008495 /*
8496 * On client, either start the renegotiation process or,
8497 * if already in progress, continue the handshake
8498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008503
8504 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008507 return( ret );
8508 }
8509 }
8510 else
8511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008515 return( ret );
8516 }
8517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008519
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008520 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008521}
8522
8523/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008524 * Check record counters and renegotiate if they're above the limit.
8525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008527{
Andres AG2196c7f2016-12-15 17:01:16 +00008528 size_t ep_len = ssl_ep_len( ssl );
8529 int in_ctr_cmp;
8530 int out_ctr_cmp;
8531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8533 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008534 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008535 {
8536 return( 0 );
8537 }
8538
Andres AG2196c7f2016-12-15 17:01:16 +00008539 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8540 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008541 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008542 ssl->conf->renego_period + ep_len, 8 - ep_len );
8543
8544 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008545 {
8546 return( 0 );
8547 }
8548
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008553
8554/*
8555 * Receive application data decrypted from the SSL layer
8556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008558{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008559 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008560 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008561
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008562 if( ssl == NULL || ssl->conf == NULL )
8563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008571 return( ret );
8572
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008573 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008574 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008575 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008576 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008577 return( ret );
8578 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008579 }
8580#endif
8581
Hanno Becker4a810fb2017-05-24 16:27:30 +01008582 /*
8583 * Check if renegotiation is necessary and/or handshake is
8584 * in process. If yes, perform/continue, and fall through
8585 * if an unexpected packet is received while the client
8586 * is waiting for the ServerHello.
8587 *
8588 * (There is no equivalent to the last condition on
8589 * the server-side as it is not treated as within
8590 * a handshake while waiting for the ClientHello
8591 * after a renegotiation request.)
8592 */
8593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008595 ret = ssl_check_ctr_renegotiate( ssl );
8596 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8597 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008600 return( ret );
8601 }
8602#endif
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008607 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8608 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008611 return( ret );
8612 }
8613 }
8614
Hanno Beckere41158b2017-10-23 13:30:32 +01008615 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008616 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008617 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008618 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008619 if( ssl->f_get_timer != NULL &&
8620 ssl->f_get_timer( ssl->p_timer ) == -1 )
8621 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008622 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008623 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008624
Hanno Becker327c93b2018-08-15 13:56:18 +01008625 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008626 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008627 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8628 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008629
Hanno Becker4a810fb2017-05-24 16:27:30 +01008630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8631 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008632 }
8633
8634 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008636 {
8637 /*
8638 * OpenSSL sends empty messages to randomize the IV
8639 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008640 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008643 return( 0 );
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008646 return( ret );
8647 }
8648 }
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008653
Hanno Becker4a810fb2017-05-24 16:27:30 +01008654 /*
8655 * - For client-side, expect SERVER_HELLO_REQUEST.
8656 * - For server-side, expect CLIENT_HELLO.
8657 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8658 */
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008661 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008663 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008666
8667 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008670 {
8671 continue;
8672 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008675 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008676#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008677
Hanno Becker4a810fb2017-05-24 16:27:30 +01008678#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008683
8684 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008686 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008687 {
8688 continue;
8689 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008692 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008693#endif /* MBEDTLS_SSL_SRV_C */
8694
Hanno Becker21df7f92017-10-17 11:03:26 +01008695#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008696 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008697 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8698 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8699 ssl->conf->allow_legacy_renegotiation ==
8700 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8701 {
8702 /*
8703 * Accept renegotiation request
8704 */
Paul Bakker48916f92012-09-16 19:57:18 +00008705
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008706 /* DTLS clients need to know renego is server-initiated */
8707#if defined(MBEDTLS_SSL_PROTO_DTLS)
8708 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8709 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8710 {
8711 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8712 }
8713#endif
8714 ret = ssl_start_renegotiation( ssl );
8715 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8716 ret != 0 )
8717 {
8718 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8719 return( ret );
8720 }
8721 }
8722 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008723#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008724 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008725 /*
8726 * Refuse renegotiation
8727 */
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#if defined(MBEDTLS_SSL_PROTO_SSL3)
8732 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008733 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008734 /* SSLv3 does not have a "no_renegotiation" warning, so
8735 we send a fatal alert and abort the connection. */
8736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8737 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8738 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008739 }
8740 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8742#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8743 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8744 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8747 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8748 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008749 {
8750 return( ret );
8751 }
Paul Bakker48916f92012-09-16 19:57:18 +00008752 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8755 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008759 }
Paul Bakker48916f92012-09-16 19:57:18 +00008760 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008761
Hanno Becker90333da2017-10-10 11:27:13 +01008762 /* At this point, we don't know whether the renegotiation has been
8763 * completed or not. The cases to consider are the following:
8764 * 1) The renegotiation is complete. In this case, no new record
8765 * has been read yet.
8766 * 2) The renegotiation is incomplete because the client received
8767 * an application data record while awaiting the ServerHello.
8768 * 3) The renegotiation is incomplete because the client received
8769 * a non-handshake, non-application data message while awaiting
8770 * the ServerHello.
8771 * In each of these case, looping will be the proper action:
8772 * - For 1), the next iteration will read a new record and check
8773 * if it's application data.
8774 * - For 2), the loop condition isn't satisfied as application data
8775 * is present, hence continue is the same as break
8776 * - For 3), the loop condition is satisfied and read_record
8777 * will re-deliver the message that was held back by the client
8778 * when expecting the ServerHello.
8779 */
8780 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008781 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008782#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008784 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008785 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008786 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008787 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008790 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008792 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008793 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8798 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008801 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008802 }
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8807 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008808 }
8809
8810 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008811
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008812 /* We're going to return something now, cancel timer,
8813 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008815 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008816
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008818 /* If we requested renego but received AppData, resend HelloRequest.
8819 * Do it now, after setting in_offt, to avoid taking this branch
8820 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008824 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008825 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008828 return( ret );
8829 }
8830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008832#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008833 }
8834
8835 n = ( len < ssl->in_msglen )
8836 ? len : ssl->in_msglen;
8837
8838 memcpy( buf, ssl->in_offt, n );
8839 ssl->in_msglen -= n;
8840
gabor-mezei-arm3eaa1032020-07-15 10:55:00 +02008841 /* Zeroising the plaintext buffer to erase unused application data
8842 from the memory. */
8843 mbedtls_platform_zeroize( ssl->in_offt, n );
8844
Paul Bakker5121ce52009-01-03 21:22:43 +00008845 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008846 {
8847 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008848 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008849 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008851 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008852 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008853 /* more data available */
8854 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Paul Bakker23986e52011-04-24 08:57:21 +00008859 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008860}
8861
8862/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008863 * Send application data to be encrypted by the SSL layer, taking care of max
8864 * fragment length and buffer size.
8865 *
8866 * According to RFC 5246 Section 6.2.1:
8867 *
8868 * Zero-length fragments of Application data MAY be sent as they are
8869 * potentially useful as a traffic analysis countermeasure.
8870 *
8871 * Therefore, it is possible that the input message length is 0 and the
8872 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008873 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008874static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008875 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008877 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8878 const size_t max_len = (size_t) ret;
8879
8880 if( ret < 0 )
8881 {
8882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8883 return( ret );
8884 }
8885
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008886 if( len > max_len )
8887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008889 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008892 "maximum fragment length: %d > %d",
8893 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008895 }
8896 else
8897#endif
8898 len = max_len;
8899 }
Paul Bakker887bd502011-06-08 13:10:54 +00008900
Paul Bakker5121ce52009-01-03 21:22:43 +00008901 if( ssl->out_left != 0 )
8902 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008903 /*
8904 * The user has previously tried to send the data and
8905 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8906 * written. In this case, we expect the high-level write function
8907 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008912 return( ret );
8913 }
8914 }
Paul Bakker887bd502011-06-08 13:10:54 +00008915 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008916 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008917 /*
8918 * The user is trying to send a message the first time, so we need to
8919 * copy the data into the internal buffers and setup the data structure
8920 * to keep track of partial writes
8921 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008922 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008924 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008925
Hanno Becker67bc7c32018-08-06 11:33:50 +01008926 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008929 return( ret );
8930 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008931 }
8932
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008933 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008934}
8935
8936/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008937 * Write application data, doing 1/n-1 splitting if necessary.
8938 *
8939 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008940 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008941 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008944static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008945 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008946{
8947 int ret;
8948
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008949 if( ssl->conf->cbc_record_splitting ==
8950 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008951 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8953 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8954 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008955 {
8956 return( ssl_write_real( ssl, buf, len ) );
8957 }
8958
8959 if( ssl->split_done == 0 )
8960 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008961 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008962 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008963 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008964 }
8965
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008966 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8967 return( ret );
8968 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008969
8970 return( ret + 1 );
8971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008973
8974/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008975 * Write application data (public-facing wrapper)
8976 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008977int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008978{
8979 int ret;
8980
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008982
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008983 if( ssl == NULL || ssl->conf == NULL )
8984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8985
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008986#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008987 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8988 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008989 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008990 return( ret );
8991 }
8992#endif
8993
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008994 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008995 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008996 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008997 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008999 return( ret );
9000 }
9001 }
9002
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009003#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009004 ret = ssl_write_split( ssl, buf, len );
9005#else
9006 ret = ssl_write_real( ssl, buf, len );
9007#endif
9008
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009010
9011 return( ret );
9012}
9013
9014/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009015 * Notify the peer that the connection is being closed
9016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009018{
9019 int ret;
9020
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009021 if( ssl == NULL || ssl->conf == NULL )
9022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009025
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009026 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9032 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9033 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009036 return( ret );
9037 }
9038 }
9039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009041
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009042 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009043}
9044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009046{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009047 if( transform == NULL )
9048 return;
9049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009051 deflateEnd( &transform->ctx_deflate );
9052 inflateEnd( &transform->ctx_inflate );
9053#endif
9054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9056 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058 mbedtls_md_free( &transform->md_ctx_enc );
9059 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009060
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009061 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009062}
9063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064#if defined(MBEDTLS_X509_CRT_PARSE_C)
9065static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009068
9069 while( cur != NULL )
9070 {
9071 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009073 cur = next;
9074 }
9075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009077
Hanno Becker0271f962018-08-16 13:23:47 +01009078#if defined(MBEDTLS_SSL_PROTO_DTLS)
9079
9080static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9081{
9082 unsigned offset;
9083 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9084
9085 if( hs == NULL )
9086 return;
9087
Hanno Becker283f5ef2018-08-24 09:34:47 +01009088 ssl_free_buffered_record( ssl );
9089
Hanno Becker0271f962018-08-16 13:23:47 +01009090 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009091 ssl_buffering_free_slot( ssl, offset );
9092}
9093
9094static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9095 uint8_t slot )
9096{
9097 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9098 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009099
9100 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9101 return;
9102
Hanno Beckere605b192018-08-21 15:59:07 +01009103 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009104 {
Hanno Beckere605b192018-08-21 15:59:07 +01009105 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009106 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009107 mbedtls_free( hs_buf->data );
9108 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009109 }
9110}
9111
9112#endif /* MBEDTLS_SSL_PROTO_DTLS */
9113
Gilles Peskine9b562d52018-04-25 20:32:43 +02009114void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009115{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009116 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9117
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009118 if( handshake == NULL )
9119 return;
9120
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009121#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9122 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9123 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009124 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009125 handshake->async_in_progress = 0;
9126 }
9127#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9128
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009129#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9130 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9131 mbedtls_md5_free( &handshake->fin_md5 );
9132 mbedtls_sha1_free( &handshake->fin_sha1 );
9133#endif
9134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9135#if defined(MBEDTLS_SHA256_C)
9136 mbedtls_sha256_free( &handshake->fin_sha256 );
9137#endif
9138#if defined(MBEDTLS_SHA512_C)
9139 mbedtls_sha512_free( &handshake->fin_sha512 );
9140#endif
9141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#if defined(MBEDTLS_DHM_C)
9144 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#if defined(MBEDTLS_ECDH_C)
9147 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009148#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009149#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009150 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009151#if defined(MBEDTLS_SSL_CLI_C)
9152 mbedtls_free( handshake->ecjpake_cache );
9153 handshake->ecjpake_cache = NULL;
9154 handshake->ecjpake_cache_len = 0;
9155#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009156#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009157
Janos Follath4ae5c292016-02-10 11:27:43 +00009158#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9159 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009160 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009162#endif
9163
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009164#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9165 if( handshake->psk != NULL )
9166 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009167 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009168 mbedtls_free( handshake->psk );
9169 }
9170#endif
9171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9173 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009174 /*
9175 * Free only the linked list wrapper, not the keys themselves
9176 * since the belong to the SNI callback
9177 */
9178 if( handshake->sni_key_cert != NULL )
9179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009181
9182 while( cur != NULL )
9183 {
9184 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009186 cur = next;
9187 }
9188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009190
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009191#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009192 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009193#endif
9194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195#if defined(MBEDTLS_SSL_PROTO_DTLS)
9196 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009197 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009198 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009199#endif
9200
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009201 mbedtls_platform_zeroize( handshake,
9202 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009203}
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009206{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009207 if( session == NULL )
9208 return;
9209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009211 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213 mbedtls_x509_crt_free( session->peer_cert );
9214 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009215 }
Paul Bakkered27a042013-04-18 22:46:23 +02009216#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009217
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009218#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009220#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009221
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009222 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009223}
9224
Paul Bakker5121ce52009-01-03 21:22:43 +00009225/*
9226 * Free an SSL context
9227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009229{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009230 if( ssl == NULL )
9231 return;
9232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009234
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009235 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009236 {
Angus Grattond8213d02016-05-25 20:56:48 +10009237 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009239 }
9240
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009241 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009242 {
Angus Grattond8213d02016-05-25 20:56:48 +10009243 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009245 }
9246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009247#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009248 if( ssl->compress_buf != NULL )
9249 {
Angus Grattond8213d02016-05-25 20:56:48 +10009250 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009252 }
9253#endif
9254
Paul Bakker48916f92012-09-16 19:57:18 +00009255 if( ssl->transform )
9256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 mbedtls_ssl_transform_free( ssl->transform );
9258 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009259 }
9260
9261 if( ssl->handshake )
9262 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009263 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9265 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267 mbedtls_free( ssl->handshake );
9268 mbedtls_free( ssl->transform_negotiate );
9269 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009270 }
9271
Paul Bakkerc0463502013-02-14 11:19:38 +01009272 if( ssl->session )
9273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 mbedtls_ssl_session_free( ssl->session );
9275 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009276 }
9277
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009279 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009280 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009281 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009283 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009284#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9287 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9290 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009291 }
9292#endif
9293
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009294#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009296#endif
9297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009299
Paul Bakker86f04f42013-02-14 11:20:09 +01009300 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009301 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009302}
9303
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009304/*
9305 * Initialze mbedtls_ssl_config
9306 */
9307void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9308{
9309 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9310}
9311
Simon Butcherc97b6972015-12-27 23:48:17 +00009312#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009313static int ssl_preset_default_hashes[] = {
9314#if defined(MBEDTLS_SHA512_C)
9315 MBEDTLS_MD_SHA512,
9316 MBEDTLS_MD_SHA384,
9317#endif
9318#if defined(MBEDTLS_SHA256_C)
9319 MBEDTLS_MD_SHA256,
9320 MBEDTLS_MD_SHA224,
9321#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009322#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009323 MBEDTLS_MD_SHA1,
9324#endif
9325 MBEDTLS_MD_NONE
9326};
Simon Butcherc97b6972015-12-27 23:48:17 +00009327#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009328
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009329static int ssl_preset_suiteb_ciphersuites[] = {
9330 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9331 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9332 0
9333};
9334
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009335#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009336static int ssl_preset_suiteb_hashes[] = {
9337 MBEDTLS_MD_SHA256,
9338 MBEDTLS_MD_SHA384,
9339 MBEDTLS_MD_NONE
9340};
9341#endif
9342
9343#if defined(MBEDTLS_ECP_C)
9344static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +01009345#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009346 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009347#endif
9348#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009349 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +01009350#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009351 MBEDTLS_ECP_DP_NONE
9352};
9353#endif
9354
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009355/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009356 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009357 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009358int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009359 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009360{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009361#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009362 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009363#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009364
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009365 /* Use the functions here so that they are covered in tests,
9366 * but otherwise access member directly for efficiency */
9367 mbedtls_ssl_conf_endpoint( conf, endpoint );
9368 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009369
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009370 /*
9371 * Things that are common to all presets
9372 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009373#if defined(MBEDTLS_SSL_CLI_C)
9374 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9375 {
9376 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9377#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9378 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9379#endif
9380 }
9381#endif
9382
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009383#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009384 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009385#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009386
9387#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9388 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9389#endif
9390
9391#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9392 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9393#endif
9394
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009395#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9396 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9397#endif
9398
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009399#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009400 conf->f_cookie_write = ssl_cookie_write_dummy;
9401 conf->f_cookie_check = ssl_cookie_check_dummy;
9402#endif
9403
9404#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9405 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9406#endif
9407
Janos Follath088ce432017-04-10 12:42:31 +01009408#if defined(MBEDTLS_SSL_SRV_C)
9409 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9410#endif
9411
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009412#if defined(MBEDTLS_SSL_PROTO_DTLS)
9413 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9414 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9415#endif
9416
9417#if defined(MBEDTLS_SSL_RENEGOTIATION)
9418 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009419 memset( conf->renego_period, 0x00, 2 );
9420 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009421#endif
9422
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009423#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9424 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9425 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009426 const unsigned char dhm_p[] =
9427 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9428 const unsigned char dhm_g[] =
9429 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9430
Hanno Beckera90658f2017-10-04 15:29:08 +01009431 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9432 dhm_p, sizeof( dhm_p ),
9433 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009434 {
9435 return( ret );
9436 }
9437 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009438#endif
9439
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009440 /*
9441 * Preset-specific defaults
9442 */
9443 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009444 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009445 /*
9446 * NSA Suite B
9447 */
9448 case MBEDTLS_SSL_PRESET_SUITEB:
9449 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9450 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9451 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9452 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9453
9454 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9455 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9456 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9457 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9458 ssl_preset_suiteb_ciphersuites;
9459
9460#if defined(MBEDTLS_X509_CRT_PARSE_C)
9461 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009462#endif
9463
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009464#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009465 conf->sig_hashes = ssl_preset_suiteb_hashes;
9466#endif
9467
9468#if defined(MBEDTLS_ECP_C)
9469 conf->curve_list = ssl_preset_suiteb_curves;
9470#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009471 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009472
9473 /*
9474 * Default
9475 */
9476 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009477 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9478 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9479 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9480 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9481 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9482 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9483 MBEDTLS_SSL_MIN_MINOR_VERSION :
9484 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009485 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9486 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9487
9488#if defined(MBEDTLS_SSL_PROTO_DTLS)
9489 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9490 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9491#endif
9492
9493 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9494 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9495 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9496 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9497 mbedtls_ssl_list_ciphersuites();
9498
9499#if defined(MBEDTLS_X509_CRT_PARSE_C)
9500 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9501#endif
9502
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009503#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009504 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009505#endif
9506
9507#if defined(MBEDTLS_ECP_C)
9508 conf->curve_list = mbedtls_ecp_grp_id_list();
9509#endif
9510
9511#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9512 conf->dhm_min_bitlen = 1024;
9513#endif
9514 }
9515
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009516 return( 0 );
9517}
9518
9519/*
9520 * Free mbedtls_ssl_config
9521 */
9522void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9523{
9524#if defined(MBEDTLS_DHM_C)
9525 mbedtls_mpi_free( &conf->dhm_P );
9526 mbedtls_mpi_free( &conf->dhm_G );
9527#endif
9528
9529#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9530 if( conf->psk != NULL )
9531 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009532 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009533 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009534 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009535 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009536 }
9537
9538 if( conf->psk_identity != NULL )
9539 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009540 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009541 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009542 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009543 conf->psk_identity_len = 0;
9544 }
9545#endif
9546
9547#if defined(MBEDTLS_X509_CRT_PARSE_C)
9548 ssl_key_cert_free( conf->key_cert );
9549#endif
9550
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009551 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009552}
9553
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009554#if defined(MBEDTLS_PK_C) && \
9555 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009556/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#if defined(MBEDTLS_RSA_C)
9562 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9563 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#if defined(MBEDTLS_ECDSA_C)
9566 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9567 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009570}
9571
Hanno Becker7e5437a2017-04-28 17:15:26 +01009572unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9573{
9574 switch( type ) {
9575 case MBEDTLS_PK_RSA:
9576 return( MBEDTLS_SSL_SIG_RSA );
9577 case MBEDTLS_PK_ECDSA:
9578 case MBEDTLS_PK_ECKEY:
9579 return( MBEDTLS_SSL_SIG_ECDSA );
9580 default:
9581 return( MBEDTLS_SSL_SIG_ANON );
9582 }
9583}
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009586{
9587 switch( sig )
9588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#if defined(MBEDTLS_RSA_C)
9590 case MBEDTLS_SSL_SIG_RSA:
9591 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593#if defined(MBEDTLS_ECDSA_C)
9594 case MBEDTLS_SSL_SIG_ECDSA:
9595 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009596#endif
9597 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009599 }
9600}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009601#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009602
Hanno Becker7e5437a2017-04-28 17:15:26 +01009603#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9604 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9605
9606/* Find an entry in a signature-hash set matching a given hash algorithm. */
9607mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9608 mbedtls_pk_type_t sig_alg )
9609{
9610 switch( sig_alg )
9611 {
9612 case MBEDTLS_PK_RSA:
9613 return( set->rsa );
9614 case MBEDTLS_PK_ECDSA:
9615 return( set->ecdsa );
9616 default:
9617 return( MBEDTLS_MD_NONE );
9618 }
9619}
9620
9621/* Add a signature-hash-pair to a signature-hash set */
9622void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9623 mbedtls_pk_type_t sig_alg,
9624 mbedtls_md_type_t md_alg )
9625{
9626 switch( sig_alg )
9627 {
9628 case MBEDTLS_PK_RSA:
9629 if( set->rsa == MBEDTLS_MD_NONE )
9630 set->rsa = md_alg;
9631 break;
9632
9633 case MBEDTLS_PK_ECDSA:
9634 if( set->ecdsa == MBEDTLS_MD_NONE )
9635 set->ecdsa = md_alg;
9636 break;
9637
9638 default:
9639 break;
9640 }
9641}
9642
9643/* Allow exactly one hash algorithm for each signature. */
9644void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9645 mbedtls_md_type_t md_alg )
9646{
9647 set->rsa = md_alg;
9648 set->ecdsa = md_alg;
9649}
9650
9651#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9652 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9653
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009654/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009655 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009658{
9659 switch( hash )
9660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661#if defined(MBEDTLS_MD5_C)
9662 case MBEDTLS_SSL_HASH_MD5:
9663 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009664#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#if defined(MBEDTLS_SHA1_C)
9666 case MBEDTLS_SSL_HASH_SHA1:
9667 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SHA256_C)
9670 case MBEDTLS_SSL_HASH_SHA224:
9671 return( MBEDTLS_MD_SHA224 );
9672 case MBEDTLS_SSL_HASH_SHA256:
9673 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675#if defined(MBEDTLS_SHA512_C)
9676 case MBEDTLS_SSL_HASH_SHA384:
9677 return( MBEDTLS_MD_SHA384 );
9678 case MBEDTLS_SSL_HASH_SHA512:
9679 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009680#endif
9681 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009683 }
9684}
9685
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009686/*
9687 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9688 */
9689unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9690{
9691 switch( md )
9692 {
9693#if defined(MBEDTLS_MD5_C)
9694 case MBEDTLS_MD_MD5:
9695 return( MBEDTLS_SSL_HASH_MD5 );
9696#endif
9697#if defined(MBEDTLS_SHA1_C)
9698 case MBEDTLS_MD_SHA1:
9699 return( MBEDTLS_SSL_HASH_SHA1 );
9700#endif
9701#if defined(MBEDTLS_SHA256_C)
9702 case MBEDTLS_MD_SHA224:
9703 return( MBEDTLS_SSL_HASH_SHA224 );
9704 case MBEDTLS_MD_SHA256:
9705 return( MBEDTLS_SSL_HASH_SHA256 );
9706#endif
9707#if defined(MBEDTLS_SHA512_C)
9708 case MBEDTLS_MD_SHA384:
9709 return( MBEDTLS_SSL_HASH_SHA384 );
9710 case MBEDTLS_MD_SHA512:
9711 return( MBEDTLS_SSL_HASH_SHA512 );
9712#endif
9713 default:
9714 return( MBEDTLS_SSL_HASH_NONE );
9715 }
9716}
9717
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009718#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009719/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009720 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009721 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009722 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009723int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009726
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009727 if( ssl->conf->curve_list == NULL )
9728 return( -1 );
9729
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009730 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009731 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009732 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009733
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009734 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009735}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009736#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009737
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009738#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009739/*
9740 * Check if a hash proposed by the peer is in our list.
9741 * Return 0 if we're willing to use it, -1 otherwise.
9742 */
9743int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9744 mbedtls_md_type_t md )
9745{
9746 const int *cur;
9747
9748 if( ssl->conf->sig_hashes == NULL )
9749 return( -1 );
9750
9751 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9752 if( *cur == (int) md )
9753 return( 0 );
9754
9755 return( -1 );
9756}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009757#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_X509_CRT_PARSE_C)
9760int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9761 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009762 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009763 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009764{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009765 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009767 int usage = 0;
9768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009770 const char *ext_oid;
9771 size_t ext_len;
9772#endif
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9775 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009776 ((void) cert);
9777 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009778 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009779#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9782 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009783 {
9784 /* Server part of the key exchange */
9785 switch( ciphersuite->key_exchange )
9786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 case MBEDTLS_KEY_EXCHANGE_RSA:
9788 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009789 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009790 break;
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9793 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9794 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9795 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009796 break;
9797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9799 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009800 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009801 break;
9802
9803 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 case MBEDTLS_KEY_EXCHANGE_NONE:
9805 case MBEDTLS_KEY_EXCHANGE_PSK:
9806 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9807 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009808 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009809 usage = 0;
9810 }
9811 }
9812 else
9813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9815 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009816 }
9817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009819 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009820 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009821 ret = -1;
9822 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009823#else
9824 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9828 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9831 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009832 }
9833 else
9834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9836 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009837 }
9838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009840 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009841 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009842 ret = -1;
9843 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009845
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009846 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009849
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009850/*
9851 * Convert version numbers to/from wire format
9852 * and, for DTLS, to/from TLS equivalent.
9853 *
9854 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009855 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009856 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9857 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009860 unsigned char ver[2] )
9861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_SSL_PROTO_DTLS)
9863 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009866 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9867
9868 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9869 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9870 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009871 else
9872#else
9873 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009874#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009875 {
9876 ver[0] = (unsigned char) major;
9877 ver[1] = (unsigned char) minor;
9878 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009879}
9880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009882 const unsigned char ver[2] )
9883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884#if defined(MBEDTLS_SSL_PROTO_DTLS)
9885 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009886 {
9887 *major = 255 - ver[0] + 2;
9888 *minor = 255 - ver[1] + 1;
9889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009891 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9892 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009893 else
9894#else
9895 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009896#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009897 {
9898 *major = ver[0];
9899 *minor = ver[1];
9900 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009901}
9902
Simon Butcher99000142016-10-13 17:21:01 +01009903int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9904{
9905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9906 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9907 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9908
9909 switch( md )
9910 {
9911#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9912#if defined(MBEDTLS_MD5_C)
9913 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009914 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009915#endif
9916#if defined(MBEDTLS_SHA1_C)
9917 case MBEDTLS_SSL_HASH_SHA1:
9918 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9919 break;
9920#endif
9921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9922#if defined(MBEDTLS_SHA512_C)
9923 case MBEDTLS_SSL_HASH_SHA384:
9924 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9925 break;
9926#endif
9927#if defined(MBEDTLS_SHA256_C)
9928 case MBEDTLS_SSL_HASH_SHA256:
9929 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9930 break;
9931#endif
9932 default:
9933 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9934 }
9935
9936 return 0;
9937#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9938 (void) ssl;
9939 (void) md;
9940
9941 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9942#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9943}
9944
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009945#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9946 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9947int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9948 unsigned char *output,
9949 unsigned char *data, size_t data_len )
9950{
9951 int ret = 0;
9952 mbedtls_md5_context mbedtls_md5;
9953 mbedtls_sha1_context mbedtls_sha1;
9954
9955 mbedtls_md5_init( &mbedtls_md5 );
9956 mbedtls_sha1_init( &mbedtls_sha1 );
9957
9958 /*
9959 * digitally-signed struct {
9960 * opaque md5_hash[16];
9961 * opaque sha_hash[20];
9962 * };
9963 *
9964 * md5_hash
9965 * MD5(ClientHello.random + ServerHello.random
9966 * + ServerParams);
9967 * sha_hash
9968 * SHA(ClientHello.random + ServerHello.random
9969 * + ServerParams);
9970 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009971 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009972 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009974 goto exit;
9975 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009976 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009977 ssl->handshake->randbytes, 64 ) ) != 0 )
9978 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009980 goto exit;
9981 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009982 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009983 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009985 goto exit;
9986 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009987 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009988 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009990 goto exit;
9991 }
9992
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009993 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009994 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009996 goto exit;
9997 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009998 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009999 ssl->handshake->randbytes, 64 ) ) != 0 )
10000 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010002 goto exit;
10003 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010004 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010005 data_len ) ) != 0 )
10006 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010008 goto exit;
10009 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010010 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010011 output + 16 ) ) != 0 )
10012 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010014 goto exit;
10015 }
10016
10017exit:
10018 mbedtls_md5_free( &mbedtls_md5 );
10019 mbedtls_sha1_free( &mbedtls_sha1 );
10020
10021 if( ret != 0 )
10022 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10023 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10024
10025 return( ret );
10026
10027}
10028#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10029 MBEDTLS_SSL_PROTO_TLS1_1 */
10030
10031#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10032 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10033int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010034 unsigned char *hash, size_t *hashlen,
10035 unsigned char *data, size_t data_len,
10036 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010037{
10038 int ret = 0;
10039 mbedtls_md_context_t ctx;
10040 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010041 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010042
10043 mbedtls_md_init( &ctx );
10044
10045 /*
10046 * digitally-signed struct {
10047 * opaque client_random[32];
10048 * opaque server_random[32];
10049 * ServerDHParams params;
10050 * };
10051 */
10052 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10053 {
10054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10055 goto exit;
10056 }
10057 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10058 {
10059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10060 goto exit;
10061 }
10062 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10063 {
10064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10065 goto exit;
10066 }
10067 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10068 {
10069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10070 goto exit;
10071 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010072 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010073 {
10074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10075 goto exit;
10076 }
10077
10078exit:
10079 mbedtls_md_free( &ctx );
10080
10081 if( ret != 0 )
10082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10083 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10084
10085 return( ret );
10086}
10087#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10088 MBEDTLS_SSL_PROTO_TLS1_2 */
10089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#endif /* MBEDTLS_SSL_TLS_C */